Skip to main content

Python client for Scute API

Project description

Scute Python Client

A Python client for the Scute API, with built-in support for authentication, users, devices, and tokens.
Includes ready-to-use integration with Django REST Framework.


Quickstart

🚀 Installation

pip install scute-client

Initialize the client:

scute = ScuteClient(
    app_id=SCUTE_APP_ID,
    app_secret=SCUTE_APP_SECRET,
)

📖 Usage Examples

Create a user

identifier = "user@example.com"  # or phone number
scute_user = scute.users.create_user(identifier)
print(scute_user)

Delete a user

scute.users.delete_user(user.sid, request.token)

Get a user by ID

user = scute.users.get_user_by_id("scute_user_id")
print(user)

List all users

for scute_user in scute.users.list_all_users():
    print(scute_user["id"], scute_user["email"])

🛠 Error Handling

All API errors raise APIRequestError:

from scute_client.exceptions import APIRequestError

try:
    user = scute.users.get_user_by_id("invalid_id")
except APIRequestError as e:
    print(f"Error: {e.status_code} - {e.message}")


🔑 Django REST Framework Integration

Add your Scute credentials to Django settings.py:

SCUTE_APP_ID = "your_app_id"
SCUTE_APP_SECRET = "your_app_secret"

Initialize the client:

from django.conf import settings
from scute_client import ScuteClient

scute = ScuteClient(
    app_id=settings.SCUTE_APP_ID,
    app_secret=settings.SCUTE_APP_SECRET,
)

First, ensure your User model has a sid (Scute ID) field. This field is required to link your local users with Scute users.

Example User model:

# users/models.py

from django.contrib.auth.models import AbstractUser
from django.db import models

class User(AbstractUser):
    # Store Scute ID returned from Scute API
    sid = models.CharField(max_length=255, unique=True, null=True, blank=True)

    def __str__(self):
        return self.username

Create a custom authentication class:

# authentication.py

from rest_framework.authentication import BaseAuthentication
from rest_framework.exceptions import AuthenticationFailed
from users.models import User


class ScuteAuthentication(BaseAuthentication):
    keyword = "Scute"

    def authenticate(self, request):
        auth_header = request.headers.get("Authorization")
        if not auth_header or not auth_header.startswith(self.keyword):
            return None

        token = auth_header.split(f"{self.keyword} ")[1]

        try:
            user_data = scute.auth.get_current_user(token)
        except Exception as e:
            raise AuthenticationFailed(f"Invalid token: {str(e)}")

        sid = user_data.get("id")
        if not sid:
            raise AuthenticationFailed("No user ID found in Scute response")

        user = User.objects.filter(sid=sid).first()
        if not user:
            raise AuthenticationFailed("User not found")

        if not user.is_active:
            raise AuthenticationFailed("Inactive user")

        return user, token

Enable it in settings.py:

REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES": [
        "path.to.authentication.ScuteAuthentication",
    ],
}

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

scute_client-0.1.5.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

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

scute_client-0.1.5-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file scute_client-0.1.5.tar.gz.

File metadata

  • Download URL: scute_client-0.1.5.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.11

File hashes

Hashes for scute_client-0.1.5.tar.gz
Algorithm Hash digest
SHA256 64a6fd41b749c5a0710aea0096e4dbbc4a163a284044f1a15ecba86bcfd858a6
MD5 5aeddb8f9313a031114a38e72478d68e
BLAKE2b-256 f77345e24868feb29f0ff0eafb92b901a145af034ecc2af95edec6627a653e34

See more details on using hashes here.

File details

Details for the file scute_client-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: scute_client-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.11

File hashes

Hashes for scute_client-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 9a724b1d2a2229aeb37b060fbec4030b614a6f3a6b939349fb71046adb38c5c9
MD5 67c03ca1ea9f10732541c29579f1638b
BLAKE2b-256 1333065a493d357ad1c2a13aefcc85e2ebd1cb1a368e8fa846c5e19b1e9dbbd7

See more details on using hashes here.

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