Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Be sure to edit the values for SERVER, CO_ID, USERNAME, PASSWORD, and IDENTIFIER. See the comments in the code.

Code Block
languagepy
firstline1
titleapi_demo.py
linenumberstrue
collapsetrue
import comanage_registry_restapiv1_client
from comanage_registry_restapiv1_client.rest import ApiException
from comanage_registry_restapiv1_client.models import CoGroupRequest
from comanage_registry_restapiv1_client.models import CoGroupsRequestType
from comanage_registry_restapiv1_client.models import CoGroupMemberRequest
from comanage_registry_restapiv1_client.models import CoGroupMemberRequestPerson
from comanage_registry_restapiv1_client.models import CoGroupMembersRequestType

from pprint import pprint

# The Registry server URL. It should include the /registry path.
SERVER = "https://registry.example.org/registry"

# The CO in which to work.
CO_ID = 2

# The REST API username.
USERNAME = ""

# The REST API password.
PASSWORD = ""

# The value of an Identifier attached to an existing CO Person.
IDENTIFIER = "albert.einstein"

# Defining the host is optional and defaults to https://localhost/registry
# See configuration.py for a list of all supported configuration parameters.
configuration = comanage_registry_restapiv1_client.Configuration(
    host="https://registry-test.cadre5safes.org.au/registry"SERVER,
    username=USERNAME,
    password=PASSWORD,
)


# Enter a context with an instance of the API client.
with comanage_registry_restapiv1_client.ApiClient(configuration) as api_client:
    # Create an instance of the API class.
    api_instance = comanage_registry_restapiv1_client.V1Api(api_client)

    # Create a new group and record its ID.
    newGroup = CoGroupRequest(
        version="1.0",
        co_id=str(CO_ID),
        name="API Test Group",
        description="An API Test",
        open=False,
        status="Active",
    )

    newGroupRequest = CoGroupsRequestType(
        request_type="CoGroups", version="1.0", co_groups=[newGroup]
    )

    try:
        api_response = api_instance.add_co_group(newGroupRequest)
        newGroupId = int(api_response.id)

        print("The response of add_co_group:\n")
        pprint(api_response)
    except ApiException as e:
        print("Exception when calling add_co_group: %s\n" % e)

    # Find the CO Person ID for identifier
    try:
        api_response = api_instance.get_co_people(coid=2, search_identifier=IDENTIFIER)
        coPersonId = int(api_response.co_people[0].id)

        print("The response of get_co_people:\n")
        pprint(api_response)
    except ApiException as e:
        print("Exception when calling get_co_people: %s\n" % e)

    # Determine if the CO Person is a member of the new group.
    isMember = False
    try:
        api_response = api_instance.get_co_group_members(copersonid=coPersonId)
        memberships = api_response.co_group_members

        for m in memberships:
            groupId = int(m.co_group_id)
            if groupId == newGroupId:
                isMember = True
                break

        print("The response of get_co_group_members\n")
        pprint(api_response)
    except ApiException as e:
        print("Exception when calling get_co_group_members: %s\n" % e)

    # If not already a member then add the CO Person to the Co Group.
    if not isMember:
        memberRequestPerson = CoGroupMemberRequestPerson(type="CO", id=str(coPersonId))
        newMembership = CoGroupMemberRequest(
            version="1.0",
            co_group_id=str(newGroupId),
            person=memberRequestPerson,
            member=True,
            owner=False,
        )

        newMembershipRequest = CoGroupMembersRequestType(
            request_type="CoGroupMembers",
            version="1.0",
            co_group_members=[newMembership],
        )

        try:
            api_response = api_instance.add_co_group_member(newMembershipRequest)

            print("The response of add_co_group_member:\n")
            pprint(api_response)
        except ApiException as e:
            print("Exception when calling add_co_group_member: %s\n" % e)

...