Skip to main content

Python client for Core API.

Project description

python3-core-api-client

Python client for Core API.

This client was built for and tested on the 1.241 version of the API.

Support

This client is officially supported by Cyberfusion.

Have questions? Ask your support questions on the platform. No access to the platform? Send an email to support@cyberfusion.io. GitHub issues are not actively monitored.

Install

This client can be used in any Python project and with any framework.

This client requires Python 3.11 or higher.

PyPI

Run the following command to install the package from PyPI:

pip3 install python3-core-api-client

Debian

Run the following commands to build a Debian package:

mk-build-deps -i -t 'apt -o Debug::pkgProblemResolver=yes --no-install-recommends -y'
dpkg-buildpackage -us -uc

Usage

API documentation

Refer to the API documentation for information about API requests.

Enums and Models are auto-generated based on the OpenAPI spec - so the client is completely in line with the Core API. Requests and Resources are not auto-generated.

Getting started

Initialise the CoreApiConnector with your username and password or API key.

The connector takes care of authentication, and offers several resources (such as virtual_hosts) and endpoints (i.e. list_virtual_hosts).

from cyberfusion.CoreApiClient.connector import CoreApiConnector

# Using username and password

connector = CoreApiConnector(
    username='username', password='password'
)

# Or using API key

connector = CoreApiConnector(
    api_key='api_key'
)

virtual_hosts = connector.virtual_hosts.list_virtual_hosts()

Authentication

This client takes care of authentication.

If authentication using username and password fails, cyberfusion.CoreApiClient.exceptions.AuthenticationException is thrown.

If authentication using API key fails, the regular CallException exception fis raised.

Don't have an API user? Contact Cyberfusion.

Requests

The client uses a fluent interface to build requests.

To view all possible requests:

  • Start with the connector
  • Go to the desired resource
  • Call the desired endpoint

Code example:

from cyberfusion.CoreApiClient.connector import CoreApiConnector

from cyberfusion.CoreApiClient.models import MailDomainCreateRequest

connector = CoreApiConnector(
    username='username', password='password'
)

connector.mail_domains.create_mail_domain(
    MailDomainCreateRequest(
        domain='cyberfusion.io',
        unix_user_id=1,
        is_local=True,
        catch_all_forward_email_addresses=[],
    )
)

Models are validated before sending the request (using Pydantic). If invalid data is provided, pydantic.ValidationError is thrown.

For example:

from cyberfusion.CoreApiClient.connector import CoreApiConnector

from cyberfusion.CoreApiClient.models import MailAliasCreateRequest

connector = CoreApiConnector(
    username='username', password='password'
)

connector.mail_aliases.create_mail_alias(
    MailAliasCreateRequest(
        local_part='&^@$#^&@$#^&',
        mail_domain_id=1,
    )
)
# throw pydantic.ValidationError

The exception will provide more details about the validation errors.

Responses

Get model from response

Calling a n endpoint returns the resource model.

For example:

from cyberfusion.CoreApiClient.connector import CoreApiConnector

from cyberfusion.CoreApiClient.models import MailDomainCreateRequest

connector = CoreApiConnector(
    username='username', password='password'
)

mail_domain_resource = connector.mail_domains.create_mail_domain(
    MailDomainCreateRequest(
        domain='cyberfusion.io',
        unix_user_id=1,
        is_local=True,
        catch_all_forward_email_addresses=[],
    )
)
# mail_domain_resource is a model representing the API resource

Throw exception on failure

If a request returns an unexpected HTTP status code, cyberfusion.CoreApiClient.exceptions.CallException is thrown.

Enums

Some properties only accept certain values (enums).

Find these values in cyberfusion.CoreApiClient.models.

Deep dive

Custom requests session

Want to provide your own requests session? Pass it to the connector:

import requests

from cyberfusion.CoreApiClient.connector import CoreApiConnector

connector = CoreApiConnector(
    ..., requests_session=requests.Session()
)

Don't pass a custom session? A default one is created, including retries.

Manual requests

Don't want to use the full SDK, but easily send requests and retrieve responses from the Core API?

Initialise the connector as usual, and call send:

import requests

from cyberfusion.CoreApiClient.connector import CoreApiConnector

connector = CoreApiConnector(...)

response = connector.send(method='GET', path='/foobar', data={}, query_parameters={})

response.status_code
response.json
response.body
response.headers
response.failed

To raise cyberfusion.CoreApiClient.exceptions.CallException in case of an unexpected HTTP status code, use send_or_fail.

Generating models

Auto-generate models as follows:

datamodel-codegen --input-file-type openapi --input $file --output src/cyberfusion/CoreApiClient/models.py --target-python-version 3.11

Replace $file by the path to the OpenAPI spec (JSON).

Note: don't replace models.py in full - it contains customisations.

Test strategy

Tests use a mock server, Stoplight Prism.

Prism checks requests' syntactic validity - based on the OpenAPI spec.

Therefore, the resources' test suites solely call methods without asserting specifics: nearly all possible issues - invalid requests, mismatch between resource models and endpoint, etc. - are already caught by having a validating mock server.

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

python3_core_api_client-0.1.tar.gz (50.8 kB view details)

Uploaded Source

Built Distribution

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

python3_core_api_client-0.1-py3-none-any.whl (77.8 kB view details)

Uploaded Python 3

File details

Details for the file python3_core_api_client-0.1.tar.gz.

File metadata

  • Download URL: python3_core_api_client-0.1.tar.gz
  • Upload date:
  • Size: 50.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for python3_core_api_client-0.1.tar.gz
Algorithm Hash digest
SHA256 e969a6e69c627a16448485e6855fe7cd92597c658440ebf4b4f8c9b7b06cdf6a
MD5 b26077a44f86cb01269fa69d284b4687
BLAKE2b-256 975d5fccf6a495070d6c51cc7118872052f03b167255b74c32ad3618c10b6c1f

See more details on using hashes here.

File details

Details for the file python3_core_api_client-0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for python3_core_api_client-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0f838546df623fa293b80f774861e7ca7d688e332562b70d0451d2a434f1f953
MD5 3cd6ffb58cba02e14ea24ce2a280a136
BLAKE2b-256 725da73d9eac43be8c588c45ef147eea1bd636db0090dfb8a37a634c9370ac87

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