Skip to main content

Python client for Stanford MaIS APIs

Project description

Badge showing the latest version available on PyPI Badge showing supported Python versions Badge showing documentation build status

This is a Python client for interacting with Stanford MaIS APIs.

The Middleware and Integration Services group, part of University IT, provides many APIs for reading information from the Registry, the data store that contains information about people, accounts, groups, courses, and more. Although the data stores seem simple, they come from many different sources (for example, student records and HR records).

MaIS provides access to these APIs to authorized clients, through a set of XML and JSON APIs. This package provides a Python SDK for interfacing with some of those APIs.

This package was created & is maintained by Stanford Research Computing. It is made available to others in the hope that it will be useful!

Example

Here is an example of how you can use the Accounts API through this SDK:

from stanford.mais.client import MAISClient
from stanford.mais.account import AccountClient
from pathlib import Path

# Keep this secret!
client_cert = Path('client.pem')

# Set up clients
client = MAISClient.prod(client_cert)  # You can also use .uat() for UAT.
                                       # Or use .uat1() for UAT1.
accounts = AccountClient(client)

# Fetch an account
lelandjr = accounts.get('lelandjr')  # This is one way, or …
lelandjr = accounts['lelandjr']  # … you can do this!

# Look at account information
lelandjr.name  # "Stanford Jr., Leland"
lelandjr.services.library.is_active  # `True`

# Only interested in accounts of people?
sunetids = accounts.only_people()
sunetids.get('shared-email')  # Raises KeyError

# How about active people?
active_sunetids = accounts.only_people().only_active()

# Bulk validation of SUNetIDs
list_of_ids = 'leland jane lelandjr asld-gkm'
from stanford.mais.account.validate import validate
validated_results = validate(list_of_ids)
# validated_results.full={'jane'}
# validated_results.base={'leland'}
# validated_results.inactive={'lelandjr'}
# validated_results.unknown={'asld-gkm'}

Here is an example of how you can use the Workgroup API through this SDK:

from stanford.mais.client import MAISClient
from stanford.mais.workgroup import WorkgroupClient
from pathlib import Path

# Keep this secret!
client_cert = Path('client.pem')

# Set up clients
client = MAISClient.prod(client_cert)  # You can also use .uat() for UAT.
workgroups = WorkgroupClient(client)

# Fetch a workgroup
sysadmins = workgroups.get('research-computing:sysadmins')
sysadmins = workgroups['research-computing:sysadmins'] # This works too!

# Create a workgroup
if 'research-computing:everyone' not in workgroups:
    everyone = workgroups.create(
        name='research-computing:everyone',
        description='Everyone in Research Computing!',
        privgroup=True
    )
else:
    everyone = workgroups['research-computing:everyone']

# Workgroup members & administrators are sets, so you can use them like sets!
missing_people = sysadmins.members.people - everyone.members.people
if len(missing_people) > 0:
    print(
        'Some sysadmins are missing from everyone: ' +
        ','.join(missing_people)
    )
    everyone.members.people.update(sysadmins.members.people)

# Adding people also uses set operations
sysadmins.members.people.add(new_sysadmin)
everyone.members.people.add(new_sysadmin)

# Nesting workgroups also uses set operations
everyone.members.workgroups.add('research-computing:sysadmins')

# You can also access privgroups through this interface
sysadmins_privgroup = sysadmins.get_privgroup()
sysadmins_privgroup_members = sysadmins_privgroup.members
ldap_accounts = list((member.sunetid for member in sysadmins_privgroup_members))

APIs Supported

The following APIs are supported:

  • Account: Full support for Full Data records for individual accounts, for both people (SUNetIDs) and functional accounts. All of the information provided by the API is exposed, including service-specific settings. Support for fetching a list of accounts that changed status in the last X days. Support for ‘views’ that can act as if functional or inactive accounts don’t exist (so you don’t have to filter them out). Also provided is code for quickly validating a collection of SUNetIDs.

  • Workgroup: Full support for operations on individual workgroups. Creating workgroups. Searching for workgroup by name, or by member (member SUNetID, member certificate, or member (nested) workgroup). Modifying workgroup properties and membership. Deleting workgroups. Fetching privilege groups.

    Not currently planned: Anything related to workgroup integration linkages.

Support is not planned for the following APIs, as the author does not currently have a need for them:

If you are interested in working on a full (or mostly-full) implementation for an API, reach out to us!

Requirements

  • Python 3.11, or any later Python 3.x

    We will try to maintain support for whichever Python 3.x is present in the oldstable release of Debian. Python 2 will not be supported.

  • Requests 2.30.0, or any later 2.x.

    We do allow Requests 2.28.2, as packaged in Debian bookworm, but we do not currently test against it.

  • A client certificate, issued by MaIS, with permissions to the APIs you want to use, in the appropriate tier (such as PROD (production) or UAT). See the Getting Started guide for more information.

  • Testing requires PyTest and pytest-responses.

  • Linting requires MyPy and types-requests.

  • Building documentation requires Sphinx and the Sphinx RTD Theme.

At this time, there is no explicit support for threads, multiple processes, or async, though support may be added in the future. Until then, you should be safe to use these modules, so long as you don’t share instances across threads/processes.

Versions & Installation

Production releases are available through PyPi. The project name is stanford-mais. Semantic versioning is used for production releases.

Non-production releases are available on PyPi Test. Non-production releases use pre-release version numbers. To install the latest-available pre-release version, use a command like pip install -i https://test.pypi.org/simple/ stanford-mais --pre.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stanford_mais-0.55.0.tar.gz (109.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stanford_mais-0.55.0-py3-none-any.whl (80.2 kB view details)

Uploaded Python 3

File details

Details for the file stanford_mais-0.55.0.tar.gz.

File metadata

  • Download URL: stanford_mais-0.55.0.tar.gz
  • Upload date:
  • Size: 109.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for stanford_mais-0.55.0.tar.gz
Algorithm Hash digest
SHA256 3d0c565d7a79b15593616530ac4650380e0ce47cf0790f056320464c15fd154a
MD5 829986372bf5884192e81f0d2916649c
BLAKE2b-256 92e2685169e16e9c8a8fc1728f7b979710c228cb10d1c3cbf8207391a106bd8a

See more details on using hashes here.

Provenance

The following attestation bundles were made for stanford_mais-0.55.0.tar.gz:

Publisher: main.yml on stanford-rc/mais-apis-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file stanford_mais-0.55.0-py3-none-any.whl.

File metadata

  • Download URL: stanford_mais-0.55.0-py3-none-any.whl
  • Upload date:
  • Size: 80.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for stanford_mais-0.55.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d57d86e142dd25d97193cdd17a12e108701451fc94db5b920feab91832851a78
MD5 26af646fa70ab50c6c948e0bb3866b2e
BLAKE2b-256 b39c5b3d8823c56777b5f11bcb0a82c256efdc6200a8889238aab966b86dd196

See more details on using hashes here.

Provenance

The following attestation bundles were made for stanford_mais-0.55.0-py3-none-any.whl:

Publisher: main.yml on stanford-rc/mais-apis-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page