diff --git a/README.md b/README.md index 07b9446..003b39a 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: 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 39118f1..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): - return names_add(self) + 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 7c40d97..3919199 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: str, family: 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: str, family: 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: First name for the person + family: 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 + post_body['Names'][0]['Family'] = family + 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..96a28c8 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: str, family: 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="TestGiven", + family="TestFamily" + )) print(json.dumps(new_name, indent=4)) except HTTPError as err: print('[ERROR] Exception caught')