Skip to main content

This package is used to interact with the microsoft graph API

Project description

Microsoft Python client

Python tests pre-commit CodeQL codecov

Microsoft graph API Python client with support for Server to Server Oauth tokens with App Only access.

Install

This package is available on Pypi

pip install ms-python-client

Requirements

  • Python >= 3.10

Usage

Defining your env variables

Define the following variables in your env or your .env file:

  • MS_ACCOUNT_ID
  • MS_CLIENT_ID
  • MS_CLIENT_SECRET

For testing purposes

For testing purposes, you can use the following values:

  • MS_ACCESS_TOKEN

This token could be obtained from the Microsoft Graph Explorer by clicking on the Sign in with Microsoft button and then clicking on the Access Token tab.

Initialize the MSApiClient from environment variables

from ms_python_client.ms_api_client import MSApiClient

ms_client = MSApiClient.init_from_env()

Initialize the MSApiClient from .env

from ms_python_client.ms_api_client import MSApiClient

ms_client = MSApiClient.init_from_dotenv()

Initialize the MSApiClient manually

from ms_python_client.ms_api_client import MSApiClient

ms_client = MSApiClient(
        account_id="<YOUR ACCOUNT ID>",
        client_id="<YOUR CLIENT ID>",
        client_secret="<YOUR CLIENT SECRET>")

Use the file system to store the access token instead of environment

There are some cases where you might want to store the access token in the file system in order to share its value with other elements of the application (Ex. different pods on a Kubernetes/Openshift application).

You can define the path where the token will be stored, passing the use_path variable to the constructor:

from ms_python_client.ms_api_client import MSApiClient

ms_client = MSApiClient(
        account_id="<YOUR ACCOUNT ID>",
        client_id="<YOUR CLIENT ID>",
        client_secret="<YOUR CLIENT SECRET>",
        use_path="/path/to/token/folder")

How to make API calls

USER_ID = "12345"
SUBJECT = "Test meeting"

query = {"$count": "true", "$filter": f"contains(subject,'{SUBJECT}')"}
result = cern_ms_client.events.list_events(USER_ID, query)

Optional: How to configure the logging

from ms_python_client.utils.logger import setup_logs

setup_logs(log_level=logging.DEBUG)

Available endpoints

events:

  1. get all events
  2. get a single event
  3. create an event
  4. update an event
  5. delete an event

CERN specific endpoints

Instead of using the MSApiClient class, you can use the CERNMSApiClient class, which is a subclass of the MSApiClient class. This class will provide you some more utilities but it will only work for CERN users (obviously).

This will be used in the context of synchronizing the events between the CERN Indico system and the calendars of the Zoom Rooms.

How to initialize the CERNMSApiClient

from ms_python_client.cern_ms_api_client import CERNMSApiClient

cern_ms_client = CERNMSApiClient.init_from_env()

Available endpoints

events:

  1. get all events
  2. get a single event using indico id
  3. create an event
  4. update an event using indico id
  5. delete an event using indico id

You will find useful the EventParameters and PartialEventParameters classes, which will help you to create the events.

indico_event_id is the id of the event in the indico system which is mandatory to create an event.

USER_ID is the email of the Zoom Room.

from ms_python_client.cern_ms_api_client import (
        CERNMSApiClient,
        EventParameters,
        PartialEventParameters,
        )

cern_ms_client = CERNMSApiClient.init_from_env()

USER_ID = os.getenv("USER_ID") # Which is the email of the Zoom Room
INDICO_EVENT_ID = os.getenv("INDICO_EVENT_ID")

event_parameters = EventParameters(
        subject="Test meeting",
        start_time="2021-10-01T12:00:00",
        end_time="2021-10-01T13:00:00",
        timezone="Europe/Zurich",
        indico_event_id=INDICO_EVENT_ID,
        zoom_url="https://cern.zoom.us/******",
)

partial_event_parameters = PartialEventParameters(
        indico_event_id=INDICO_EVENT_ID,
        end_time="2021-10-01T14:00:00",
) # You can update only the end_time of the event for example

cern_ms_client.events.create_event(USER_ID, event_parameters)
cern_ms_client.events.update_event_by_indico_id(USER_ID, partial_event_parameters)
cern_ms_client.events.delete_event_by_indico_id(USER_ID, INDICO_EVENT_ID)

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

ms_python_client-0.1.1.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

ms_python_client-0.1.1-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file ms_python_client-0.1.1.tar.gz.

File metadata

  • Download URL: ms_python_client-0.1.1.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.0 Linux/5.15.0-1041-azure

File hashes

Hashes for ms_python_client-0.1.1.tar.gz
Algorithm Hash digest
SHA256 230cc155d5a35f6d236d09d37399736ba343d5967ef9409b0491194914c5ca0b
MD5 186c263cd2b3a9a1ee546f34fa3ccfcf
BLAKE2b-256 89fdb965bac06ce17c7afb9e2b2e57295676351e00db2fd4a099e8a072f8d392

See more details on using hashes here.

Provenance

File details

Details for the file ms_python_client-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: ms_python_client-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.0 Linux/5.15.0-1041-azure

File hashes

Hashes for ms_python_client-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a9ccabc558e7eb49c8433d24e0d7527dd99a2f1be4f8ef14ac1bbeea9d8734c7
MD5 db3e9f2968a225ae65f22a35e7faf226
BLAKE2b-256 e691baf4d466317cb35c991539042aec814b7be49793c1f52f773f479a410410

See more details on using hashes here.

Provenance

Supported by

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