Skip to main content

Helper library for easier creation of Google services.

Project description

Python Library for helping to create Python services from Google APIs

Something I have to do a lot is create connections to Google Service endpoints. This generally requires a fair amount of boiler-plate code that io just copied and pasted from other locations. Also any time a new service is needed, I need to find that specific implementation of the service builder and add the new endpoint.

Obviously this can lead to potential errors and if a bug is found, I have to remember where I created these services and fix it in many projects.

This has led me to create a small helper library to make the process easier. Included in the library is a complete and updated (at time of publishing) list of all the Google APIs along with their versions and endpoints as Enums ready for use.

Usage

service_builder.build_service() needs two mandatory pieces of information:

Parameter Type Description
key str The credentials to use to authenticate the user to the service it has to create.
service Service The Google API endpoint it is trying to connect to.
api_key See below Optional An API key for those services which require an API key in addition to credentials.
extra_scopes List[str] Optional Extra OAuth scopes needed.

key

These can be supplied in one of 4 ways.

  1. A valid google.oauth2.Credentials object, created and passed to the service builder.
  2. A str which is the name of a local file containing the json representation of an OAuth credentials object.
  3. A Mapping object which is a json keyfile dict
  4. An OAuthKey object from this package, which is a dataclass object comprising of the minimum fields necessary to perform the OAuth connection.

service

This is an Enum in the service_framework.service module, which defines the Google service you're planning to connect to. All the standard Google services are included (as of the date listed in the Services.py file).

If a new service has been added but this library has yet to be updated, you can request it by using either Service('<UNLISTED SERVICE>') or Service.from_value('<UNLISTED SERVICE>'). This will cause the library to attempt to fetch the service's definition from Google and will create an entry in the enum for this new service.

Examples

1. Using Credentials

from service_framework.services import Service
from service_framework import service_builder
from google.oauth2 import credentials as oauth

credentials = oauth.Credentials(
   access_token="<YOUR ACCESS TOKEN>",
   refresh_token="<YOUR REFRESH TOKEN>",
   token_uri="https://oauth2.googleapis.com/token",
   client_id="<YOUR CLIENT ID>",
   client_secret="<YOUR CLIENT SECRET>"
)

service = service_builder.build_service(service=Service.CHAT, key=credentials)

2. Using an OAuthKey

from service_framework.services import Service
from service_framework import service_builder

key = service_builder.OAuthKey(
   access_token="<YOUR ACCESS TOKEN>",
   refresh_token="<YOUR REFRESH TOKEN>",
   token_uri="https://oauth2.googleapis.com/token",
   client_id="<YOUR CLIENT ID>",
   client_secret="<YOUR CLIENT SECRET>"
)

service = service_builder.build_service(service=Service.GMAIL, key=key)

Here, we are creating the key as an OAuthKey object as we have clearly got the tokens and client_[id|secret] from elsewhere. Creating the service is then as simple as invoking the service_builder.build_service method which will return you a service you can 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

python_service_builder-2.4.1.tar.gz (18.8 kB view details)

Uploaded Source

Built Distribution

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

python_service_builder-2.4.1-py3-none-any.whl (22.0 kB view details)

Uploaded Python 3

File details

Details for the file python_service_builder-2.4.1.tar.gz.

File metadata

  • Download URL: python_service_builder-2.4.1.tar.gz
  • Upload date:
  • Size: 18.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for python_service_builder-2.4.1.tar.gz
Algorithm Hash digest
SHA256 b6ba0a6335ce435cd2061b592451c6c1b98900aa9e69c19f0c435a3f66fa0ed1
MD5 5a42220589717d3b78830375ee24a92b
BLAKE2b-256 f8ee90f6b66c4563bb0ce22ca87d95f7821c98308114f2699a93910dd9794e41

See more details on using hashes here.

File details

Details for the file python_service_builder-2.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for python_service_builder-2.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4a58c09d458d5d9325b0072367cabe7524c0aa2ae62e32f9f85e851fbba119f9
MD5 038579e32d825e77b847d2d4a8095603
BLAKE2b-256 34689078f144e956072fc67ff7f69b4c2647bb95f7d8238a9159dbc2b9f19ae0

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