From 59de30b0ea490bd0466cac3878707a24528d4abb Mon Sep 17 00:00:00 2001 From: Brennan Jones Date: Mon, 19 Sep 2022 14:12:04 -0400 Subject: [PATCH 1/2] implementing names_add method --- README.md | 3 +- comanage_api/__init__.py | 4 +- comanage_api/_names.py | 91 ++++++++++++++++++++++++++++++++++----- examples/names_example.py | 9 +++- 4 files changed, 90 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 07b9446..a9f9e84 100644 --- a/README.md +++ b/README.md @@ -274,8 +274,7 @@ ENTITY_OPTIONS = ['codeptid', 'cogroupid', 'copersonid', 'organizationid', 'orgi ### [Name API](https://spaces.at.internet2.edu/display/COmanage/Name+API) (COmanage v3.3.0+) -- `names_add() -> dict` - - `### NOT IMPLEMENTED ###` +- `names_add(person_type: str, person_id: int, given_name: str, family_name: str) -> dict` - Add a new Name. - `names_delete() -> bool` - `### NOT IMPLEMENTED ###` diff --git a/comanage_api/__init__.py b/comanage_api/__init__.py index 39118f1..cc2c3a1 100644 --- a/comanage_api/__init__.py +++ b/comanage_api/__init__.py @@ -224,8 +224,8 @@ def identifiers_view_one(self, identifier_id: int): return identifiers_view_one(self, identifier_id=identifier_id) # Name API - def names_add(self): - return names_add(self) + def names_add(self, person_type: str, person_id: int, given_name: str, family_name: str): + return names_add(self, person_type=person_type, person_id=person_id, given_name=given_name, family_name=family_name) def names_delete(self): return names_delete(self) diff --git a/comanage_api/_names.py b/comanage_api/_names.py index 7c40d97..257db1c 100644 --- a/comanage_api/_names.py +++ b/comanage_api/_names.py @@ -5,8 +5,7 @@ Methods ------- -names_add() -> dict - ### NOT IMPLEMENTED ### +names_add(person_type: str, person_id: int, given_name: str, family_name: str) -> dict Add a new Name. names_delete() -> bool ### NOT IMPLEMENTED ### @@ -25,18 +24,88 @@ import json -def names_add(self) -> dict: +def names_add(self, person_type: str, person_id: int, given_name: str, family_name: str) -> dict: """ - ### NOT IMPLEMENTED ### - Add a new Name. + Add a new Name. - :param self: - :return - 501 Server Error: Not Implemented for url: mock://not_implemented_501.local: + person_type: Type of person (i.e. "CO"|"Org") + person_id: COmanage ID for the person_type + given_name: First name for the person + family_name: Last name for the person + :request + { + "RequestType":"Names", + "Version":"1.0", + "Names": + [ + { + "Version":"1.0", + "Honorific":"", + "Given":"", + "Middle":"", + "Family":"", + "Suffix":"", + "Type":"", + "Language":"", + "PrimaryName":true|false, + "Person": + { + "Type":("CO"|"Org"), + "Id":"" + } + } + ] + }: + + Response Format + HTTP Status Response Body Description + 201 Added NewObjectResponse Name added + 400 Bad Request Name Request not + provided in POST body + 400 Invalid Fields ErrorResponse An error in one or more + provided fields + 401 Unauthorized Authentication required + 403 No Person Specified Either a CO Person or an Org Identity + must be specified to attach the + Name to + 403 Person Does Not Exist The specified CO Person or + Org Identity does not exist + 500 Other Error Unknown error """ - url = self._MOCK_501_URL - resp = self._mock_session.get( - url=url + post_body = { + "RequestType":"Names", + "Version":"1.0", + "Names": + [ + { + "Version":"1.0", + "Honorific":"", + "Given":"", + "Middle":"", + "Family":"", + "Suffix":"", + "Type":"official", + "Language":"", + "PrimaryName": True, + "Person": + { + "Type":"", + "Id":"" + } + } + ] + } + + post_body['Names'][0]['Given'] = given_name + post_body['Names'][0]['Family'] = family_name + post_body['Names'][0]['Person']['Type'] = person_type + post_body['Names'][0]['Person']['Id'] = person_id + + post_body = json.dumps(post_body) + url = self._CO_API_URL + '/names.json' + resp = self._s.post( + url=url, + data=post_body ) if resp.status_code == 201: return json.loads(resp.text) diff --git a/examples/names_example.py b/examples/names_example.py index d63cabf..bad4fc1 100644 --- a/examples/names_example.py +++ b/examples/names_example.py @@ -12,10 +12,15 @@ # must be set ahead of time and be valid within the CO CO_PERSON_ID = 163 -# names_add() -> dict +# names_add(person_type: str, person_id: int, given_name: str, family_name: str) -> dict print('### names_add') try: - new_name = api.names_add() + new_name = api.names_add( + person_type='copersonid', + person_id=CO_PERSON_ID, + given_name="TestGiven", + family_name="TestFamily" + )) print(json.dumps(new_name, indent=4)) except HTTPError as err: print('[ERROR] Exception caught') From faec32b4d5c1f2765946fe38e3e3118db2a58d12 Mon Sep 17 00:00:00 2001 From: Brennan Jones Date: Tue, 20 Sep 2022 14:29:09 -0400 Subject: [PATCH 2/2] implementing names_add method --- README.md | 2 +- comanage_api/__init__.py | 4 ++-- comanage_api/_names.py | 12 ++++++------ examples/names_example.py | 6 +++--- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index a9f9e84..003b39a 100644 --- a/README.md +++ b/README.md @@ -274,7 +274,7 @@ ENTITY_OPTIONS = ['codeptid', 'cogroupid', 'copersonid', 'organizationid', 'orgi ### [Name API](https://spaces.at.internet2.edu/display/COmanage/Name+API) (COmanage v3.3.0+) -- `names_add(person_type: str, person_id: int, given_name: str, family_name: str) -> dict` +- `names_add(person_type: str, person_id: int, given: str, family: str) -> dict` - Add a new Name. - `names_delete() -> bool` - `### NOT IMPLEMENTED ###` diff --git a/comanage_api/__init__.py b/comanage_api/__init__.py index cc2c3a1..b143fcb 100644 --- a/comanage_api/__init__.py +++ b/comanage_api/__init__.py @@ -224,8 +224,8 @@ def identifiers_view_one(self, identifier_id: int): return identifiers_view_one(self, identifier_id=identifier_id) # Name API - def names_add(self, person_type: str, person_id: int, given_name: str, family_name: str): - return names_add(self, person_type=person_type, person_id=person_id, given_name=given_name, family_name=family_name) + def names_add(self, person_type: str, person_id: int, given: str, family: str): + return names_add(self, person_type=person_type, person_id=person_id, given=given, family=family) def names_delete(self): return names_delete(self) diff --git a/comanage_api/_names.py b/comanage_api/_names.py index 257db1c..3919199 100644 --- a/comanage_api/_names.py +++ b/comanage_api/_names.py @@ -5,7 +5,7 @@ Methods ------- -names_add(person_type: str, person_id: int, given_name: str, family_name: str) -> dict +names_add(person_type: str, person_id: int, given: str, family: str) -> dict Add a new Name. names_delete() -> bool ### NOT IMPLEMENTED ### @@ -24,14 +24,14 @@ import json -def names_add(self, person_type: str, person_id: int, given_name: str, family_name: str) -> dict: +def names_add(self, person_type: str, person_id: int, given: str, family: str) -> dict: """ Add a new Name. person_type: Type of person (i.e. "CO"|"Org") person_id: COmanage ID for the person_type - given_name: First name for the person - family_name: Last name for the person + given: First name for the person + family: Last name for the person :request { "RequestType":"Names", @@ -96,8 +96,8 @@ def names_add(self, person_type: str, person_id: int, given_name: str, family_na ] } - post_body['Names'][0]['Given'] = given_name - post_body['Names'][0]['Family'] = family_name + post_body['Names'][0]['Given'] = given + post_body['Names'][0]['Family'] = family post_body['Names'][0]['Person']['Type'] = person_type post_body['Names'][0]['Person']['Id'] = person_id diff --git a/examples/names_example.py b/examples/names_example.py index bad4fc1..96a28c8 100644 --- a/examples/names_example.py +++ b/examples/names_example.py @@ -12,14 +12,14 @@ # must be set ahead of time and be valid within the CO CO_PERSON_ID = 163 -# names_add(person_type: str, person_id: int, given_name: str, family_name: str) -> dict +# names_add(person_type: str, person_id: int, given: str, family: str) -> dict print('### names_add') try: new_name = api.names_add( person_type='copersonid', person_id=CO_PERSON_ID, - given_name="TestGiven", - family_name="TestFamily" + given="TestGiven", + family="TestFamily" )) print(json.dumps(new_name, indent=4)) except HTTPError as err: