Skip to main content

Conveniently build an SDK for web apis. Handle file uploads, downloads and streaming. Interact with job services. Natively work with FastTaskAPI.

Project description

fastSDK

Built your SDK for any hosted service

Ever wanted to use web APIs as if they are any other python function? It was never easier to build an SDK for your hosted services, than it is with socaity-client.

Why?

Let's say you have created a webservice for example with fastAPI or flask and now you want to write a python client for it. In a first approach you would just use the requests library and send the requests to the server. However, while you do so, your cpu is idle waiting for the request to finish. Over time your requirements get bigger, suddenly you do not only have one endpoint but multiples. You will do different requests in parallel, you need to transfer files, and you struggle to do so in a structured, performant way. Suddenly you end up in a threading, asyncio and complexity hell with many inconsistencies. You realize that you cannot transfer your 1GB video via an simple web request to your beautiful API. All these problems are solved with the fastSDK. Simple API calls, file uploads, job handling, and cloud storage providers are just a few features of the fastSDK.

FastSDK is designed to work beautifully with long-running services like machine learning and data processing endpoints. It works hand-in-hand with FastTaskAPI that let's you build and deploy those services and endpoints easily.

Service compatibility

Out of the box works with following services:

Features:

  • Easy file upload, download thanks to media-toolkit.
  • Async and Threaded job support. Execute multiple requests and heavy preprocessing tasks in parallel and with high speed.
    • Massively parallel job and request execution.
  • Working with services that create "Jobs". The SDK will wait for the job to finish and return the result.
  • Streaming of files
  • Automatic serialization of data types

Installation

To install from PyPI: This version includes all features needed to conveniently wrap your API into an SDK.

pip install fastsdk

Including cloud storage providers: This allows you to upload bigger filed to common cloud storage providers.

pip install fastsdk[full] #  full feature support
pip install fastsdk[azure]  # only azure blob storage support
pip install fastsdk[s3] #only  s3 upload

Get started

To build your SDK for a web API follow these steps:

  1. Create a ServiceClient with the base url of the service and the endpoint routes.
  2. Use the @fastSDK and @fastJob decorators around the service client to create your SDK.

Create the service client - the object that sends the requests to the server.

from fastsdk import ServiceClient, ImageFile

# 1. create a service client
srvc_face2face = ServiceClient(service_url="localhost:8020/api")
# on a request the module will try to cast the parameters to the specified types
srvc_face2face.add_endpoint(
    endpoint_route="add_face",
    post_params={"face_name": str},
    file_params={"img": ImageFile} 
)

Based on the service we create the SDK by using the smart decorators @fastSDK and @fastJob. The @fastSDK decorator will create a class with the service client as a class attribute.

The @fastJob decorator allows your functions to:

  • be called asynchronously and in a separate thread.
  • be called with a job object which can be used to send requests to the service endpoint.

Let's create the SDK for the service client.

from fastsdk import fastSDK, fastJob
@fastSDK(service_client=srvc_face2face)
class face2face:
  @fastJob
  def _add_face(self, job: InternalJob, face_name: str, source_img: bytes, save: bool = True):
        # send the request to the service endpoint using the provided job object
        endpoint_request = job.request("add_reference_face", face_name, source_img, save)
        # wait until server finished the job
        result = endpoint_request.get_result() 
        return result

Add this point your SDK is ready and now you can work with it as follows

f2f = face2face()
# start the request in a seperate thread
# Note that the job object is not passed to the kwargs. The wrapper will handle this for us.
ref_face_v_job = f2f.add_face(face_name="potter", img="path/to/image/of/harry")
# do something else... this works becauce ref_face_v_job will start a thread
ref_face_vector = ref_face_v_job.get_result() # wait for the server and thread to finish the job

Cloud storage providers and file uploads

The SDK comes with a built-in support for cloud storage providers like Amazon S3 and Azure Blob Storage. To directly up and download files to the cloud storage provider, you can use the following code snippets.

from fastsdk import AzureBlobStorage
# Create container and upload file
container = AzureBlobStorage(sas_access_token)
file_url = container.upload(file="path/to/file")
# Use media-toolkit to download file
file = MediaFile.from_url(container.download(file_id))

File size limited file uploads

Let's say you built an SDK for a service which works with files > 10mb. In a usual workflow the client will upload the file to a storage provider and then send the file id to the service. Instead of implementing this from hand, you can use the SDK to handle the file uploads for you.

from fastsdk import create_cloud_storage, ServiceClient

cs = create_cloud_storage(azure_sas_access_token=AZURE_SAS_ACCESS_TOKEN,
                          azure_connection_string=AZURE_SAS_CONNECTION_STRING)
srvc_face2face = ServiceClient(cloud_storage=cs, upload_to_cloud_storage_threshold_mb=10)

In this case every file > 10mb will be uploaded to the cloud storage provider. Then instead of the file, the file_url will be send to the service. If the file size is smaller than 10mb, the file will be send directly to the service as bytes. The MediaToolkit knows how to handle the file_url and will download the file for you in the service.

Recommendation: Use environment variables to store the cloud storage access tokens.

ServiceClient in detail

The purpose of a service client is to send request and retrieve responses from the server.

from socaity_client import ServiceClient, FastSDK

srvc_face2face = ServiceClient(
  service_url="localhost:8020/api",
  model_name="face2face",
  model_domain_tags=[ModelDomainTag.IMAGE, ModelDomainTag.AUDIO],
  model_tags=[ModelTag.FACE2FACE, ModelTag.IMAGE2IMAGE]
)

Authorization

Let's say you have created your SDK with @fastSDK and named it face2face Then you can init it with arguments. In this moment you can pass the api key.

f2f = face2face(service_name="runpod", api_key="my_api_key")

Alternatively you can set the api_keys in the settings and give them names like "runpod". Add this at the beginning of your script.

import os
import settings
settings.api_keys["runpod"] = os.getenv("my_api_key")
# Then you can init the service client without the api_key argument at any place.
f2f = face2face(service_name="runpod")

API keys can be set in environment variables, when creating the Service Client or when initializing your SDK. In settings.py the default environment variables are set.

Contribute

We at socaity want to provide the best tools to bring generative AI to the cloud. Please report bugs, your ideas and feature requests in the issues section. fastSDK is licensed under the MIT license and free-to-use.

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

fastsdk-0.0.3.tar.gz (42.3 kB view details)

Uploaded Source

Built Distribution

fastsdk-0.0.3-py3-none-any.whl (48.1 kB view details)

Uploaded Python 3

File details

Details for the file fastsdk-0.0.3.tar.gz.

File metadata

  • Download URL: fastsdk-0.0.3.tar.gz
  • Upload date:
  • Size: 42.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.11

File hashes

Hashes for fastsdk-0.0.3.tar.gz
Algorithm Hash digest
SHA256 c40eacfe89e9332c638dcdb2e6fc330bd5d6be0047e89bd72a80fe30d137e877
MD5 58ddf18d597faede342a5e07c97f2064
BLAKE2b-256 9cb2acb066619576f0c8ba0bd996211682093f95eb546edb59db744aacd13562

See more details on using hashes here.

File details

Details for the file fastsdk-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: fastsdk-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 48.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.11

File hashes

Hashes for fastsdk-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 4026960cce6f1b7ab3210b5739e8117376cb4ef21ff6a17b5ead9774d3910598
MD5 30c35504c1766bc157a659903c37dd6d
BLAKE2b-256 beffeb0d57d831ef0271c6605786e2ad7a292b7f772c520ef99819b5ff185007

See more details on using hashes here.

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