A Python client library for the SS12000 API.
This project has been archived.
The maintainers of this project have marked this project as archived. No new releases are expected.
Project description
SS12000 Python Client Library
This is a Python client library designed to simplify interaction with the SS12000 API, a standard for information exchange between school administration processes based on OpenAPI 3. The library handles HTTP requests and Bearer Token authentication, providing a structured approach to interact with all the API's defined endpoints.
You can download your own personal copy of the SS12000 standard for free from here: sis.se.
Important
The SS12000 does not require the server to support all of the endpoints. You need to actually look at the server documentation to see which endpoints that are actually available with each service. Adding some sort of discovery service is beyond the scope of this small library in my humble opinion.
All dates are in the RFC 3339 format, we're not cavemen here.
Table of Contents
Installation
- Save the Client: Save the code from ss12000_client.py in your project directory.
- Install Dependencies: This library uses requests for making HTTP calls. If you plan to use the webhook receiver example, you will also need fastapi and uvicorn.
pip install requests fastapi uvicorn
Usage
Initializing the Client
To start using the client, import it and create an instance with your API base URL and your JWT Bearer Token.
from ss12000client import SS12000Client
base_url = "https://some.server.se/v2.0" # Replace with your test server URL
auth_token = "YOUR_JWT_TOKEN_HERE" # Replace with your actual JWT token
client = SS12000Client(base_url, auth_token)
Fetching Organizations
You can retrieve a list of organizations or a specific organization by its ID.
async def get_organization_data():
try:
print("Fetching organizations...")
organizations = client.get_organisations(limit=2)
print("Fetched organizations:", json.dumps(organizations, indent=2))
if organizations and organizations.get('data'):
first_org_id = organizations['data'][0]['id']
print(f"\nFetching organization with ID: {first_org_id}...")
org_by_id = client.get_organisation_by_id(first_org_id, expand_reference_names=True)
print("Fetched organization by ID:", json.dumps(org_by_id, indent=2))
except Exception as e:
print(f"Error fetching organization data: {e}")
Fetching Persons
Similarly, you can fetch persons and expand related data such as duties.
async def get_person_data():
try:
print("\nFetching persons...")
persons = client.get_persons(limit=2, expand=['duties'])
print("Fetched persons:", json.dumps(persons, indent=2))
if persons and persons.get('data'):
first_person_id = persons['data'][0]['id']
print(f"\nFetching person with ID: {first_person_id}...")
person_by_id = client.get_person_by_id(first_person_id, expand=['duties', 'responsibleFor'], expand_reference_names=True)
print("Fetched person by ID:", json.dumps(person_by_id, indent=2))
except Exception as e:
print(f"Error fetching person data: {e}")
Fetch ...
Check the API reference below to see all available nodes.
Webhooks (Subscriptions)
The client provides methods to manage subscriptions (webhooks).
async def manage_subscriptions():
try:
print("\nFetching subscriptions...")
subscriptions = client.get_subscriptions()
print("Fetched subscriptions:", json.dumps(subscriptions, indent=2))
# Example: Create a subscription (requires a publicly accessible webhook URL)
# print("\nCreating a subscription...")
# new_subscription = client.create_subscription(
# name="My Python Test Subscription",
# target="http://your-public-webhook-url.com/ss12000-webhook", # Replace with your public URL
# resource_types=["Person", "Activity"]
# )
# print("Created subscription:", json.dumps(new_subscription, indent=2))
# Example: Delete a subscription
# if subscriptions and subscriptions.get('data'):
# sub_to_delete_id = subscriptions['data'][0]['id']
# print(f"\nDeleting subscription with ID: {sub_to_delete_id}...")
# client.delete_subscription(sub_to_delete_id)
# print("Subscription deleted successfully.")
except Exception as e:
print(f"Error managing subscriptions: {e}")
API Reference
The SS12000Client class is designed to expose methods for all SS12000 API endpoints. Here is a list of the primary resource paths defined in the OpenAPI specification, along with their corresponding client methods:
- /organisations
- get_organisations(**params): Get a list of organizations.
- lookup_organisations(ids, school_unit_codes, organisation_codes, expand_reference_names): Get multiple organizations.
- get_organisation_by_id(org_id, expand_reference_names): Get a specific organization.
- /persons
- get_persons(**params): Get a list of persons.
- lookup_persons(ids, civic_nos, expand, expand_reference_names): Get multiple persons.
- get_person_by_id(person_id, expand, expand_reference_names): Get a specific person.
- /placements
- get_placements(**params): Get a list of placements.
- lookup_placements(ids, expand, expand_reference_names): Get multiple placements.
- get_placement_by_id(placement_id, expand, expand_reference_names): Get a specific placement.
- /duties
- get_duties(**params): Get a list of duties.
- lookup_duties(ids, expand, expand_reference_names): Get multiple duties.
- get_duty_by_id(duty_id, expand, expand_reference_names): Get a specific duty.
- /groups
- get_groups(**params): Get a list of groups.
- lookup_groups(ids, expand, expand_reference_names): Get multiple groups.
- get_group_by_id(group_id, expand, expand_reference_names): Get a specific group.
- /programmes
- get_programmes(**params): Get a list of programmes.
- lookup_programmes(ids, expand, expand_reference_names): Get multiple programmes.
- get_programme_by_id(programme_id, expand, expand_reference_names): Get a specific programme.
- /studyplans
- get_study_plans(**params): Get a list of study plans.
- lookup_study_plans(ids, expand, expand_reference_names): Get multiple study plans.
- get_study_plan_by_id(study_plan_id, expand, expand_reference_names): Get a specific study plan.
- /syllabuses
- get_syllabuses(**params): Get a list of syllabuses.
- lookup_syllabuses(ids, expand_reference_names): Get multiple syllabuses.
- get_syllabus_by_id(syllabus_id, expand_reference_names): Get a specific syllabus.
- /schoolUnitOfferings
- get_school_unit_offerings(**params): Get a list of school unit offerings.
- lookup_school_unit_offerings(ids, expand, expand_reference_names): Get multiple school unit offerings.
- get_school_unit_offering_by_id(offering_id, expand, expand_reference_names): Get a specific school unit offering.
- /activities
- get_activities(**params): Get a list of activities.
- lookup_activities(ids, expand, expand_reference_names): Get multiple activities.
- get_activity_by_id(activity_id, expand, expand_reference_names): Get a specific activity.
- /calendarEvents
- get_calendar_events(**params): Get a list of calendar events.
- lookup_calendar_events(ids, expand, expand_reference_names): Get multiple calendar events.
- get_calendar_event_by_id(event_id, expand, expand_reference_names): Get a specific calendar event.
- /attendances
- get_attendances(**params): Get a list of attendances.
- lookup_attendances(ids, expand, expand_reference_names): Get multiple attendances.
- get_attendance_by_id(attendance_id, expand, expand_reference_names): Get a specific attendance.
- delete_attendance(attendance_id): Delete an attendance.
- /attendanceEvents
- get_attendance_events(**params): Get a list of attendance events.
- lookup_attendance_events(ids, expand, expand_reference_names): Get multiple attendance events.
- get_attendance_event_by_id(event_id, expand, expand_reference_names): Get a specific attendance event.
- /attendanceSchedules
- get_attendance_schedules(**params): Get a list of attendance schedules.
- lookup_attendance_schedules(ids, expand, expand_reference_names): Get multiple attendance schedules.
- get_attendance_schedule_by_id(schedule_id, expand, expand_reference_names): Get a specific attendance schedule.
- /grades
- get_grades(**params): Get a list of grades.
- lookup_grades(ids, expand, expand_reference_names): Get multiple grades.
- get_grade_by_id(grade_id, expand, expand_reference_names): Get a specific grade.
- /aggregatedAttendance
- get_aggregated_attendances(**params): Get a list of aggregated attendances.
- lookup_aggregated_attendances(ids, expand, expand_reference_names): Get multiple aggregated attendances.
- get_aggregated_attendance_by_id(attendance_id, expand, expand_reference_names): Get a specific aggregated attendance.
- /resources
- get_resources(**params): Get a list of resources.
- lookup_resources(ids, expand_reference_names): Get multiple resources.
- get_resource_by_id(resource_id, expand_reference_names): Get a specific resource.
- /rooms
- get_rooms(**params): Get a list of rooms.
- lookup_rooms(ids, expand_reference_names): Get multiple rooms.
- get_room_by_id(room_id, expand_reference_names): Get a specific room.
- /subscriptions
- get_subscriptions(**params): Get a list of webhooks subscriptions.
- create_subscription(name, target, resource_types): Create a new webhooks subscription.
- delete_subscription(subscription_id): Delete a webhooks subscription.
- get_subscription_by_id(subscription_id): Get a specific webhooks subscription.
- update_subscription(subscription_id, expires): Update a webhooks subscription (e.g., expiry date).
- /deletedEntities
- get_deleted_entities(entities, meta_modified_after): Get a list of deleted entities.
- /log
- get_log(**params): Get a list of log entries.
- /statistics
- get_statistics(**params): Get a list of statistics.
Each method accepts parameters corresponding to the API's query parameters and request bodies, as defined in the OpenAPI specification. Detailed information on available parameters can be found in the docstrings within ss12000_client.py.
The .yaml file can be downloaded from the SS12000 site over at sis.se.
Webhook Receiver (FastAPI Example)
A separate FastAPI server can be used to receive notifications from the SS12000 API.This is just an example and is not part of the client library. It just shows how you could implement a receiver server for the webhooks. The code below is not production ready code, it's just a thought experiment that will point you in a direction toward a simple solution.
Save this in a separate file, e.g., 'webhook_server.py'
from fastapi import FastAPI, Request, HTTPException
import uvicorn
import json
webhook_app = FastAPI()
@webhook_app.post("/ss12000-webhook")
async def ss12000_webhook(request: Request):
"""
Webhook endpoint for SS12000 notifications.
"""
print("Received a webhook from SS12000!")
print("Headers:", request.headers)
try:
body = await request.json()
print("Body:", json.dumps(body, indent=2))
# Implement your logic to handle the webhook message here.
# E.g., save the information to a database, trigger an update, etc.
if body and body.get('modifiedEntites'):
for resource_type in body['modifiedEntites']:
print(f"Changes for resource type: {resource_type}")
# You can call the SS12000Client here to fetch updated information
# depending on the resource type.
# Example: if resource_type == 'Person': client.get_persons(...)
if body and body.get('deletedEntities'):
print("There are deleted entities to fetch from /deletedEntities.")
# Call client.get_deleted_entities(...) to fetch the deleted IDs.
return {"message": "Webhook received successfully\!"}
except json.JSONDecodeError:
raise HTTPException(status_code=400, detail="Invalid JSON body")
except Exception as e:
print(f"Error processing webhook: {e}")
raise HTTPException(status_code=500, detail=f"Internal server error: {e}")
# To run the FastAPI webhook server:
# Save the above code as e.g., 'webhook_server.py'
# Then run from your terminal: 'uvicorn webhook_server:webhook_app --host 0.0.0.0 --port 3001'
To run the FastAPI webhook server, save the code above in a file (e.g., webhook_server.py) and execute it using:
uvicorn webhook_server:webhook_app --host 0.0.0.0 --port 3001
Remember that your webhook URL must be publicly accessible for the SS12000 API to send notifications to it.
Contributing
Contributions are welcome! If you want to add, improve, optimize or just change things just send in a pull request and I will have a look. Found a bug and don't know how to fix it? Create an issue!
License
This project is licensed under the MIT Licence.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ss12000_client-0.1.0.tar.gz.
File metadata
- Download URL: ss12000_client-0.1.0.tar.gz
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4701d28feaaf0a8c5dac1f80c8aafac30a0b12b09ca26c62c4f6c065bba770af
|
|
| MD5 |
b8eebc0933e94b5326c2f00d2c8a1464
|
|
| BLAKE2b-256 |
881989c6cea1a5dbe1027537b39538b9a52a7e7d92d1526576ffaacc1d055d7a
|
File details
Details for the file ss12000_client-0.1.0-py3-none-any.whl.
File metadata
- Download URL: ss12000_client-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
34069a1b5dcaf428ff0a6e8974039fcce5369df2a7e1fbb2ca24eb6093b9e0e5
|
|
| MD5 |
ea87471401d8c19d9630f9a000000856
|
|
| BLAKE2b-256 |
129b450332cf12b7573ac1f8a67ab5ad829266a89d2ada6ae5ad22ef537fd0cd
|