[ 
https://issues.apache.org/jira/browse/BEAM-9468?focusedWorklogId=410578&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-410578
 ]

ASF GitHub Bot logged work on BEAM-9468:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 26/Mar/20 20:07
            Start Date: 26/Mar/20 20:07
    Worklog Time Spent: 10m 
      Work Description: jaketf commented on pull request #11151: [BEAM-9468]  
Hl7v2 io
URL: https://github.com/apache/beam/pull/11151#discussion_r398858533
 
 

 ##########
 File path: 
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/healthcare/HttpHealthcareApiClient.java
 ##########
 @@ -0,0 +1,452 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.sdk.io.gcp.healthcare;
+
+import com.google.api.client.http.HttpHeaders;
+import com.google.api.client.http.HttpRequest;
+import com.google.api.client.http.HttpRequestInitializer;
+import com.google.api.client.http.javanet.NetHttpTransport;
+import com.google.api.client.json.JsonFactory;
+import com.google.api.client.json.gson.GsonFactory;
+import com.google.api.client.util.ArrayMap;
+import com.google.api.services.healthcare.v1alpha2.CloudHealthcare;
+import 
com.google.api.services.healthcare.v1alpha2.CloudHealthcare.Projects.Locations.Datasets.Hl7V2Stores.Messages;
+import com.google.api.services.healthcare.v1alpha2.CloudHealthcareScopes;
+import com.google.api.services.healthcare.v1alpha2.model.CreateMessageRequest;
+import com.google.api.services.healthcare.v1alpha2.model.Empty;
+import com.google.api.services.healthcare.v1alpha2.model.Hl7V2Store;
+import com.google.api.services.healthcare.v1alpha2.model.HttpBody;
+import com.google.api.services.healthcare.v1alpha2.model.IngestMessageRequest;
+import com.google.api.services.healthcare.v1alpha2.model.IngestMessageResponse;
+import com.google.api.services.healthcare.v1alpha2.model.ListMessagesResponse;
+import com.google.api.services.healthcare.v1alpha2.model.Message;
+import 
com.google.api.services.healthcare.v1alpha2.model.SearchResourcesRequest;
+import com.google.auth.oauth2.GoogleCredentials;
+import java.io.IOException;
+import java.io.Serializable;
+import java.net.URI;
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.Spliterator;
+import java.util.Spliterators;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
+import javax.annotation.Nullable;
+import org.apache.beam.sdk.extensions.gcp.util.RetryHttpRequestInitializer;
+import 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Strings;
+
+/**
+ * A client that talks to the Cloud Healthcare API through HTTP requests. This 
client is created
+ * mainly to encapsulate the unserializable dependencies, since most generated 
classes are not
+ * serializable in the HTTP client.
+ */
+public class HttpHealthcareApiClient<T> implements HealthcareApiClient, 
Serializable {
+
+  private transient CloudHealthcare client;
+
+  /**
+   * Instantiates a new Http healthcare api client.
+   *
+   * @throws IOException the io exception
+   */
+  public HttpHealthcareApiClient() throws IOException {
+    initClient();
+  }
+
+  /**
+   * Instantiates a new Http healthcare api client.
+   *
+   * @param client the client
+   * @throws IOException the io exception
+   */
+  public HttpHealthcareApiClient(CloudHealthcare client) throws IOException {
+    this.client = client;
+    initClient();
+  }
+
+  @VisibleForTesting
+  static <T, X extends Collection<T>> Stream<T> 
flattenIteratorCollectionsToStream(
+      Iterator<X> iterator) {
+    Spliterator<Collection<T>> spliterator = 
Spliterators.spliteratorUnknownSize(iterator, 0);
+    return StreamSupport.stream(spliterator, 
false).flatMap(Collection::stream);
+  }
+
+  public JsonFactory getJsonFactory() {
+    return this.client.getJsonFactory();
+  }
+
+  @Override
+  public ListMessagesResponse makeHL7v2ListRequest(
+      String hl7v2Store, @Nullable String filter, @Nullable String pageToken) 
throws IOException {
+
+    Messages.List baseRequest =
+        client
+            .projects()
+            .locations()
+            .datasets()
+            .hl7V2Stores()
+            .messages()
+            .list(hl7v2Store)
+            .setPageToken(pageToken);
+
+    if (Strings.isNullOrEmpty(filter)) {
+      return baseRequest.execute();
+    } else {
+      return baseRequest.setFilter(filter).execute();
+    }
+  }
+
+  /**
+   * Gets message id page iterator.
+   *
+   * @param hl7v2Store the HL7v2 store
+   * @return the message id page iterator
+   * @throws IOException the io exception
+   */
+  @Override
+  public Stream<String> getHL7v2MessageIDStream(String hl7v2Store) throws 
IOException {
+    return getHL7v2MessageIDStream(hl7v2Store, null);
+  }
+
+  /**
+   * Get a {@link Stream} of message IDs from flattening the pages of a new 
{@link
+   * HL7v2MessageIDPages}.
+   *
+   * @param hl7v2Store the HL7v2 store
+   * @param filter the filter
+   * @return the message id Stream
+   * @throws IOException the io exception
+   */
+  @Override
+  public Stream<String> getHL7v2MessageIDStream(String hl7v2Store, @Nullable 
String filter)
+      throws IOException {
+    Iterator<List<String>> iterator = new HL7v2MessageIDPages(this, 
hl7v2Store, filter).iterator();
+    return flattenIteratorCollectionsToStream(iterator);
+  }
+
+  /**
+   * Gets HL7v2 message.
+   *
+   * @param msgName the msg name
+   * @return the message
+   * @throws IOException the io exception
+   * @throws ParseException the parse exception
+   */
+  @Override
+  public Message getHL7v2Message(String msgName) throws IOException {
+    Message msg =
+        
client.projects().locations().datasets().hl7V2Stores().messages().get(msgName).execute();
+    if (msg == null) {
+      throw new IOException(String.format("Couldn't find message: %s.", 
msgName));
+    }
+    return msg;
+  }
+
+  @Override
+  public Empty deleteHL7v2Message(String msgName) throws IOException {
+    return client
+        .projects()
+        .locations()
+        .datasets()
+        .hl7V2Stores()
+        .messages()
+        .delete(msgName)
+        .execute();
+  }
+
+  /**
+   * Gets HL7v2 store.
+   *
+   * @param storeName the store name
+   * @return the HL7v2 store
+   * @throws IOException the io exception
+   */
+  @Override
+  public Hl7V2Store getHL7v2Store(String storeName) throws IOException {
+    return 
client.projects().locations().datasets().hl7V2Stores().get(storeName).execute();
+  }
+
+  @Override
+  public IngestMessageResponse ingestHL7v2Message(String hl7v2Store, Message 
msg)
+      throws IOException {
+    IngestMessageRequest ingestMessageRequest = new IngestMessageRequest();
+    ingestMessageRequest.setMessage(msg);
+    return client
+        .projects()
+        .locations()
+        .datasets()
+        .hl7V2Stores()
+        .messages()
+        .ingest(hl7v2Store, ingestMessageRequest)
+        .execute();
+  }
 
 Review comment:
   yes 
[here](https://github.com/apache/beam/pull/11151/files/cac03ec30c77e6bd45954e659b875d0378370892#diff-3ab3eba677d52f20f1a688b1017adf84R274)
 we extend 
[`RetryHttpRequestInitializer`](https://beam.apache.org/releases/javadoc/2.13.0/org/apache/beam/sdk/extensions/gcp/util/RetryHttpRequestInitializer.html)
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 410578)
    Time Spent: 6h 20m  (was: 6h 10m)

> Add Google Cloud Healthcare API IO Connectors
> ---------------------------------------------
>
>                 Key: BEAM-9468
>                 URL: https://issues.apache.org/jira/browse/BEAM-9468
>             Project: Beam
>          Issue Type: New Feature
>          Components: io-java-gcp
>            Reporter: Jacob Ferriero
>            Assignee: Jacob Ferriero
>            Priority: Minor
>          Time Spent: 6h 20m
>  Remaining Estimate: 0h
>
> Add IO Transforms for the HL7v2, FHIR and DICOM stores in the [Google Cloud 
> Healthcare API|https://cloud.google.com/healthcare/docs/]
> HL7v2IO
> FHIRIO
> DICOMĀ 



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to