Skip to content

Commit

Permalink
Merge pull request #420 from skalish/operation-from-id
Browse files Browse the repository at this point in the history
Allow creation of an Operation from job resource ID
  • Loading branch information
pcattori committed Jul 8, 2020
2 parents 0a50a94 + 6e050da commit cce4cd4
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 2 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## 0.13.0-dev

**BETA**
Important: Do not use BETA features for production workflows.
- Added function to get operation from resource ID

**NEW FEATURES**
- [#383](https://github.com/Datatamer/tamr-client/issues/383) Now able to create an Operation from Job resource id

## 0.12.0
**BETA**
Important: Do not use BETA features for production workflows.
Expand Down
3 changes: 2 additions & 1 deletion docs/beta/operation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ Operation

.. autofunction:: tamr_client.operation.poll
.. autofunction:: tamr_client.operation.wait
.. autofunction:: tamr_client.operation.succeeded
.. autofunction:: tamr_client.operation.succeeded
.. autofunction:: tamr_client.operation.from_resource_id
13 changes: 13 additions & 0 deletions tamr_client/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ def succeeded(operation: Operation) -> bool:
return operation.status is not None and operation.status["state"] == "SUCCEEDED"


def from_resource_id(
session: Session, instance: Instance, resource_id: str
) -> Operation:
"""Get operation by ID
Args:
resource_id: The ID of the operation
"""
url = URL(instance=instance, path=f"operations/{resource_id}")
r = session.get(str(url))
return _from_response(instance, r)


def _from_response(instance: Instance, response: requests.Response) -> Operation:
"""
Handle idiosyncrasies in constructing Operations from Tamr responses.
Expand Down
15 changes: 15 additions & 0 deletions tamr_unify_client/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ class Operation(BaseResource):
def from_json(cls, client, resource_json, api_path=None):
return super().from_data(client, resource_json, api_path)

@classmethod
def from_resource_id(cls, client, resource_id):
"""Get an operation by resource ID.
:param client: Delegate underlying API calls to this client.
:type client: :class:`~tamr_unify_client.Client`
:param resource_id: The ID of the operation
:type resource_id: str
:returns: The specified operation
:rtype: :class:`~tamr_unify_client.operation.Operation`
"""
url = f"operations/{resource_id}"
response = client.get(url).successful()
return Operation.from_response(client, response)

@classmethod
def from_response(cls, client, response):
"""
Expand Down
18 changes: 18 additions & 0 deletions tests/tamr_client/test_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@ def test_operation_from_response_noop():
tc.operation.poll(s, op2w)


@responses.activate
def test_from_resource_id():
s = utils.session()
instance = utils.instance()
url = tc.URL(path="operations/1")

operation_json = utils.load_json("operation_succeeded.json")
responses.add(responses.GET, str(url), json=operation_json)

resource_id = "1"
op = tc.operation.from_resource_id(s, instance, resource_id)
assert op.url == url
assert op.type == operation_json["type"]
assert op.description == operation_json["description"]
assert op.status == operation_json["status"]
assert tc.operation.succeeded(op)


@responses.activate
def test_operation_poll():
s = utils.session()
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/test_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ def test_operation_from_json(client):
assert op1.succeeded


@responses.activate
def test_operation_from_resource_id(client):
responses.add(responses.GET, full_url(client, "operations/1"), json=op_1_json)

op1 = Operation.from_resource_id(client, "1")

assert op1.resource_id == "1"
assert op1.succeeded


@responses.activate
def test_operation_from_response(client):
responses.add(responses.GET, full_url(client, "operations/1"), json=op_1_json)
Expand Down

0 comments on commit cce4cd4

Please sign in to comment.