Skip to content

Commit 014c64a

Browse files
committed
address review comments
1 parent 66a6c18 commit 014c64a

File tree

5 files changed

+157
-125
lines changed

5 files changed

+157
-125
lines changed

healthcare/api-client/v1/hl7v2/hl7v2_messages.py

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2020 Google LLC All Rights Reserved.
1+
# Copyright 2018 Google LLC All Rights Reserved.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
1717
import os
1818

1919
from googleapiclient import discovery
20-
from googleapiclient.errors import HttpError
2120

2221

2322
# [START healthcare_get_client]
@@ -28,9 +27,7 @@ def get_client():
2827
api_version = 'v1'
2928
service_name = 'healthcare'
3029

31-
return discovery.build(
32-
service_name,
33-
api_version)
30+
return discovery.build(service_name, api_version)
3431
# [END healthcare_get_client]
3532

3633

@@ -55,12 +52,9 @@ def create_hl7v2_message(
5552
request = client.projects().locations().datasets().hl7V2Stores(
5653
).messages().create(parent=hl7v2_store_name, body=hl7v2_message_content)
5754

58-
try:
59-
response = request.execute()
60-
print('Created HL7v2 message from file: {}'.format(hl7v2_message_file))
61-
return response
62-
except HttpError as e:
63-
print('Error, HL7v2 message not created: {}'.format(e))
55+
response = request.execute()
56+
print('Created HL7v2 message from file: {}'.format(hl7v2_message_file))
57+
return response
6458
# [END healthcare_create_hl7v2_message]
6559

6660

@@ -80,12 +74,9 @@ def delete_hl7v2_message(
8074
request = client.projects().locations().datasets(
8175
).hl7V2Stores().messages().delete(name=hl7v2_message)
8276

83-
try:
84-
response = request.execute()
85-
print('Deleted HL7v2 message with ID: {}'.format(hl7v2_message_id))
86-
return response
87-
except HttpError as e:
88-
print('Error, HL7v2 message not deleted: {}'.format(e))
77+
response = request.execute()
78+
print('Deleted HL7v2 message with ID: {}'.format(hl7v2_message_id))
79+
return response
8980
# [END healthcare_delete_hl7v2_message]
9081

9182

@@ -145,13 +136,9 @@ def ingest_hl7v2_message(
145136
request = client.projects().locations().datasets().hl7V2Stores(
146137
).messages().ingest(parent=hl7v2_store_name, body=hl7v2_message_content)
147138

148-
try:
149-
response = request.execute()
150-
print('Ingested HL7v2 message from file: {}'.format(
151-
hl7v2_message_file))
152-
return response
153-
except HttpError as e:
154-
print('Error, HL7v2 message not ingested: {}'.format(e))
139+
response = request.execute()
140+
print('Ingested HL7v2 message from file: {}'.format(hl7v2_message_file))
141+
return response
155142
# [END healthcare_ingest_hl7v2_message]
156143

157144

@@ -205,16 +192,13 @@ def patch_hl7v2_message(
205192
request = client.projects().locations().datasets().hl7V2Stores().messages(
206193
).patch(name=hl7v2_message_name, updateMask='labels', body=patch)
207194

208-
try:
209-
response = request.execute()
210-
print(
211-
'Patched HL7v2 message {} with labels:\n\t{}: {}'.format(
212-
hl7v2_message_id,
213-
label_key,
214-
label_value))
215-
return response
216-
except HttpError as e:
217-
print('Error, HL7v2 message not patched: {}'.format(e))
195+
response = request.execute()
196+
print(
197+
'Patched HL7v2 message {} with labels:\n\t{}: {}'.format(
198+
hl7v2_message_id,
199+
label_key,
200+
label_value))
201+
return response
218202
# [END healthcare_patch_hl7v2_message]
219203

220204

healthcare/api-client/v1/hl7v2/hl7v2_messages_test.py

Lines changed: 117 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2020 Google LLC All Rights Reserved.
1+
# Copyright 2018 Google LLC All Rights Reserved.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -17,10 +17,11 @@
1717
import sys
1818
import uuid
1919

20-
from gcp_devrel.testing import eventually_consistent
20+
from googleapiclient.errors import HttpError
21+
from retrying import retry
2122

2223
# Add datasets for bootstrapping datasets for testing
23-
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'datasets')) # noqa
24+
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'datasets')) # noqa
2425
import datasets
2526
import hl7v2_stores
2627
import hl7v2_messages
@@ -35,37 +36,100 @@
3536
label_value = 'TRUE'
3637

3738

38-
@pytest.fixture(scope='module')
39-
def test_dataset():
40-
dataset = datasets.create_dataset(
41-
project_id,
42-
cloud_region,
43-
dataset_id)
44-
45-
yield dataset
39+
def retry_if_server_exception(exception):
40+
return isinstance(exception, (HttpError))
4641

47-
# Clean up
48-
datasets.delete_dataset(
49-
project_id,
50-
cloud_region,
51-
dataset_id)
5242

43+
@pytest.fixture(scope="module")
44+
def test_dataset():
45+
@retry(
46+
wait_exponential_multiplier=1000,
47+
wait_exponential_max=10000,
48+
stop_max_attempt_number=10,
49+
retry_on_exception=retry_if_server_exception)
50+
def create():
51+
try:
52+
datasets.create_dataset(project_id, cloud_region, dataset_id)
53+
except HttpError as err:
54+
# We ignore 409 conflict here, because we know it's most
55+
# likely the first request failed on the client side, but
56+
# the creation suceeded on the server side.
57+
if err.resp.status == 409:
58+
print(
59+
'Got exception {} while creating dataset'.format(
60+
err.resp.status))
61+
else:
62+
raise
63+
create()
64+
65+
yield
5366

54-
@pytest.fixture(scope='module')
67+
# Clean up
68+
@retry(
69+
wait_exponential_multiplier=1000,
70+
wait_exponential_max=10000,
71+
stop_max_attempt_number=10,
72+
retry_on_exception=retry_if_server_exception)
73+
def clean_up():
74+
try:
75+
datasets.delete_dataset(project_id, cloud_region, dataset_id)
76+
except HttpError as err:
77+
# The API returns 403 when the dataset doesn't exist.
78+
if err.resp.status == 404 or err.resp.status == 403:
79+
print(
80+
'Got exception {} while deleting dataset'.format(
81+
err.resp.status))
82+
else:
83+
raise
84+
85+
clean_up()
86+
87+
88+
@pytest.fixture(scope="module")
5589
def test_hl7v2_store():
56-
hl7v2_store = hl7v2_stores.create_hl7v2_store(
57-
project_id,
58-
cloud_region,
59-
dataset_id,
60-
hl7v2_store_id)
61-
62-
yield hl7v2_store
90+
@retry(
91+
wait_exponential_multiplier=1000,
92+
wait_exponential_max=10000,
93+
stop_max_attempt_number=10,
94+
retry_on_exception=retry_if_server_exception)
95+
def create():
96+
try:
97+
hl7v2_stores.create_hl7v2_store(
98+
project_id, cloud_region, dataset_id, hl7v2_store_id)
99+
except HttpError as err:
100+
# We ignore 409 conflict here, because we know it's most
101+
# likely the first request failed on the client side, but
102+
# the creation suceeded on the server side.
103+
if err.resp.status == 409:
104+
print(
105+
'Got exception {} while creating HL7v2 store'.format(
106+
err.resp.status))
107+
else:
108+
raise
109+
create()
110+
111+
yield
63112

64-
hl7v2_stores.delete_hl7v2_store(
65-
project_id,
66-
cloud_region,
67-
dataset_id,
68-
hl7v2_store_id)
113+
# Clean up
114+
@retry(
115+
wait_exponential_multiplier=1000,
116+
wait_exponential_max=10000,
117+
stop_max_attempt_number=10,
118+
retry_on_exception=retry_if_server_exception)
119+
def clean_up():
120+
try:
121+
hl7v2_stores.delete_hl7v2_store(
122+
project_id, cloud_region, dataset_id, hl7v2_store_id)
123+
except HttpError as err:
124+
# The API returns 403 when the HL7v2 store doesn't exist.
125+
if err.resp.status == 404 or err.resp.status == 403:
126+
print(
127+
'Got exception {} while deleting HL7v2 store'.format(
128+
err.resp.status))
129+
else:
130+
raise
131+
132+
clean_up()
69133

70134

71135
def test_CRUD_hl7v2_message(test_dataset, test_hl7v2_store, capsys):
@@ -76,16 +140,15 @@ def test_CRUD_hl7v2_message(test_dataset, test_hl7v2_store, capsys):
76140
hl7v2_store_id,
77141
hl7v2_message_file)
78142

79-
hl7v2_messages_list = hl7v2_messages.list_hl7v2_messages(
80-
project_id,
81-
cloud_region,
82-
dataset_id,
83-
hl7v2_store_id)
143+
hl7v2_messages_list = hl7v2_messages.list_hl7v2_messages(
144+
project_id,
145+
cloud_region,
146+
dataset_id,
147+
hl7v2_store_id)
84148

85-
assert len(hl7v2_messages_list) > 0
86-
hl7v2_message_name = hl7v2_messages_list[0].get('name')
87-
nonlocal hl7v2_message_id
88-
hl7v2_message_id = hl7v2_message_name.split('/', 9)[9]
149+
assert len(hl7v2_messages_list) > 0
150+
hl7v2_message_name = hl7v2_messages_list[0].get('name')
151+
hl7v2_message_id = hl7v2_message_name.split('/', 9)[9]
89152

90153
hl7v2_messages.get_hl7v2_message(
91154
project_id,
@@ -117,16 +180,15 @@ def test_ingest_hl7v2_message(test_dataset, test_hl7v2_store, capsys):
117180
hl7v2_store_id,
118181
hl7v2_message_file)
119182

120-
hl7v2_messages_list = hl7v2_messages.list_hl7v2_messages(
121-
project_id,
122-
cloud_region,
123-
dataset_id,
124-
hl7v2_store_id)
183+
hl7v2_messages_list = hl7v2_messages.list_hl7v2_messages(
184+
project_id,
185+
cloud_region,
186+
dataset_id,
187+
hl7v2_store_id)
125188

126-
assert len(hl7v2_messages_list) > 0
127-
hl7v2_message_name = hl7v2_messages_list[0].get('name')
128-
nonlocal hl7v2_message_id
129-
hl7v2_message_id = hl7v2_message_name.split('/', 9)[9]
189+
assert len(hl7v2_messages_list) > 0
190+
hl7v2_message_name = hl7v2_messages_list[0].get('name')
191+
hl7v2_message_id = hl7v2_message_name.split('/', 9)[9]
130192

131193
hl7v2_messages.get_hl7v2_message(
132194
project_id,
@@ -158,16 +220,15 @@ def test_patch_hl7v2_message(test_dataset, test_hl7v2_store, capsys):
158220
hl7v2_store_id,
159221
hl7v2_message_file)
160222

161-
hl7v2_messages_list = hl7v2_messages.list_hl7v2_messages(
162-
project_id,
163-
cloud_region,
164-
dataset_id,
165-
hl7v2_store_id)
223+
hl7v2_messages_list = hl7v2_messages.list_hl7v2_messages(
224+
project_id,
225+
cloud_region,
226+
dataset_id,
227+
hl7v2_store_id)
166228

167-
assert len(hl7v2_messages_list) > 0
168-
hl7v2_message_name = hl7v2_messages_list[0].get('name')
169-
nonlocal hl7v2_message_id
170-
hl7v2_message_id = hl7v2_message_name.split('/', 9)[9]
229+
assert len(hl7v2_messages_list) > 0
230+
hl7v2_message_name = hl7v2_messages_list[0].get('name')
231+
hl7v2_message_id = hl7v2_message_name.split('/', 9)[9]
171232

172233
hl7v2_messages.patch_hl7v2_message(
173234
project_id,

0 commit comments

Comments
 (0)