Skip to content

Commit

Permalink
Use HTTP GET request for acquiring family information. Fix #33.
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl authored and gsong committed Oct 28, 2019
1 parent fb288ae commit cc233ac
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
29 changes: 24 additions & 5 deletions epo_ops/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,20 @@ def _check_for_exceeded_quota(self, response):
raise
return response # pragma: no cover

def _post(self, url, data, extra_headers=None, params=None):
def _req(self, url, data, extra_headers=None, params=None, use_get=False):
headers = {"Accept": self.accept_type, "Content-Type": "text/plain"}
headers.update(extra_headers or {})
return self.request.post(url, data=data, headers=headers, params=params)
request_method = self.request.post
if use_get:
request_method = self.request.get
return request_method(url, data=data, headers=headers, params=params)

def _make_request(self, url, data, extra_headers=None, params=None):
def _make_request(self, url, data, extra_headers=None, params=None, use_get=False):
extra_headers = extra_headers or {}
token = "Bearer {0}".format(self.access_token.token)
extra_headers["Authorization"] = token

response = self._post(url, data, extra_headers, params)
response = self._req(url, data, extra_headers, params, use_get=use_get)
response = self._check_for_expired_token(response)
response = self._check_for_exceeded_quota(response)
response.raise_for_status()
Expand All @@ -83,6 +86,21 @@ def _make_request_url(self, service, reference_type, input, endpoint, constituen
]
return u"/".join(filter(None, parts))

def _make_request_url_get(
self, service, reference_type, input, endpoint, constituents
):
constituents = constituents or []
parts = [
self.__service_url_prefix__,
service,
reference_type,
input and input.__class__.__name__.lower(),
input.as_api_input(),
endpoint,
",".join(constituents),
]
return u"/".join(filter(None, parts))

# Service requests
def _service_request(self, path, reference_type, input, endpoint, constituents):
if type(input) == list:
Expand Down Expand Up @@ -114,9 +132,10 @@ def _image_request(self, path, range, document_format):
)

def family(self, reference_type, input, endpoint=None, constituents=None):
return self._service_request(
url = self._make_request_url_get(
self.__family_path__, reference_type, input, endpoint, constituents
)
return self._make_request(url, None, params=input.as_api_input(), use_get=True)

def image(self, path, range=1, document_format="application/tiff"):
return self._image_request(path, range, document_format)
Expand Down
14 changes: 14 additions & 0 deletions epo_ops/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,17 @@ def post(self, url, data=None, **kwargs):

self.reset_env()
return response

def get(self, url, data=None, **kwargs):
self.reset_env()

for mw in self.middlewares:
url, data, kwargs = mw.process_request(self.env, url, data, **kwargs)

response = self.env["response"] or requests.get(url, **kwargs)

for mw in reversed(self.middlewares):
response = mw.process_response(self.env, response)

self.reset_env()
return response
17 changes: 17 additions & 0 deletions tests/helpers/api_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ def assert_family_success(client):
return response


def assert_family_biblio_success(client):
response = client.family(*data, constituents=["biblio"])
assert_request_success(response)
assert "patent-family" in response.text
assert "exchange-document" in response.text
assert "bibliographic-data" in response.text
return response


def assert_family_legal_success(client):
response = client.family(*data, constituents=["legal"])
assert_request_success(response)
assert 'patent-family legal="true"' in response.text
assert "ops:legal" in response.text
return response


def assert_image_success(client):
response = client.image(*idata)
assert_request_success(response)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

from .helpers.api_helpers import (
assert_bulk_service_retrival_success,
assert_family_biblio_success,
assert_family_legal_success,
assert_family_success,
assert_image_success,
assert_number_service_success,
Expand All @@ -32,6 +34,14 @@ def test_family(all_clients):
assert_family_success(all_clients)


def test_family_biblio(all_clients):
assert_family_biblio_success(all_clients)


def test_family_legal(all_clients):
assert_family_legal_success(all_clients)


def test_image(all_clients):
assert_image_success(all_clients)

Expand Down

0 comments on commit cc233ac

Please sign in to comment.