Skip to main content

Python SDK for executing the SMARTdiagnostics API

Project description

Personal Access Tokens

Starting with release 3.13, SMARTdiagnostics provides the capability to create Personal Access Tokens (PAT) that can be used as a Bearer token to authorize API requests. These access tokens are connected to the user that created them, acting on behalf of that SD user. Currently, the tokens are long living with no expiration date,. In the future, however, we do plan institute lifetimes to these tokens.

To use the new Python SDK to access SMARTdiagnostics APIs, you must first create a Personal Access Token (PAT). There is currently NO interface withing the SMARTdiagnostics UI to create PATs. Therefore, you will need to create one through the API after you have logged into SD in a browser. The easiest way to create tokens at this time is through our Swagger UI interface.

Finally, you will only be given the actual token one time, as part of the response from the CREATE request. There is no other way to retrieve the token through other APIs. Although it is long-living, you only have one time to copy and use the token. If you lose the token, another one will have to be created.

To create a PAT:

  1. Log into SD
  2. Access the Swagger UI controls and look for the Users APIs near the bottom, specifically the POST to Users/CurrentUser/tokens
  3. Expand this API and click on the "Try it Out" button
  4. The POST request only requires one property, the "description" which will be shown to you in SMARTdiagnostics in the future and (currently) when you request a list of PATs through the API. Use the description to note what application this PAT is used for or why you created it.
  5. Click the blue "Execute" button and then look for the response. The token property is your new PAT and it must be copied and saved for use later. For the purposes of this example, we will save this token to a file that will be read by the Python script.

Overview of the SMARTdiagnostics Python SDK

The Python SDK provides access to all APIs exposed through the Internal APIs as well as their REQUEST and RESPONSE models. We've chosen to provide an SDK specifically for this set of Internal APIs so that we can be intentional about exposing helpful APIs that we can commit to supporting long-term. If an API is not exposed through the Internal list, then it should be considered Private, not supported outside of the KCF Development team and subject to change.

There are two main advantages to using the SDK:

  1. All web request logic is handled internally. There is no need to create additional methods for handling this request/response logic.
  2. All models necessary to both make requests (POST/PUT) and to consume responses (GET) are provided. You do not have to create a set of objects/classes to hold this data.

Dependencies

We currently use an application called AutoREST to create the SDK for Python. One of dependencies of the SDK that allows it to manage the requests is msrest. This must be installed before using the SDK.

pip install msrest

Parameter Usage

If you examine the API documentation through Swagger UI, most GET requests that allow paging take at least four standard request parameters:

  • Filter: This is a string that specifies the properties to refine the response
  • Page: when a response has more than one page of items, request additional pages of data by incrementing this value
  • PageLimit: The number of items to include on page. Default is 10. Max is 2,000 items per page. In most cases, it will be more efficient to request the maximum so that fewer requests are made. YMMV.
  • OrderBy: Which property to sort the response by.

In general Python fashion, these request parameters are translated to their lowercase, underline separated variant which is shown in the example script (eg. "page_limit", "order_by")

Settings.ini file

The script variables, including the PAT are stored in an external file called settings.ini. The format of this file matches that of the configparser module.

[SDAPI]
base_url = https://sd.kcftech.com
token = 0123456789+abcdefghijk/kcf
corporation_id = 00000000-0000-0000-0000-000000000001

Example script

For this simple example, we will list all of the Locations for a given Corporation. For this example, we assume that the response provides more than one page of results to show one possible way to page the results.

from smartdiagnostics_sdk import SmartDiagnosticsApi, models
import configparser

# Get configuration
configuration = configparser.ConfigParser()
configuration.read("settings.ini")

# Prod base_url example: https://sd.kcftech.com
base_url = configuration["SDAPI"]["base_url"]
bearer_token = configuration["SDAPI"]["token"]
corporation_id = configuration["SDAPI"]["corporation_id"]

# instantiate client and add token for requests
sd_client = SmartDiagnosticsApi(base_url=base_url)
sd_client.config.headers["Authorization"] = "bearer " + bearer_token

# call the locations GET API
corporation_filter = "CorporationId eq '{}'".format(corporation_id)
location_response = sd_client.get_locations(filter=corporation_filter) 

total_pages = location_response.total_pages

# Print some details for each location
print("LOCATIONS:")
for i in range(1, total_pages + 1):
    location_response = sd_client.get_locations(filter=corporation_filter,page=i, page_limit=10)
    for location in location_response.result:
        print(location.name)
        print("Id: {}".format(location.id))
        print("Abbreviation: {}".format(location.abbreviation))
        print("CorporationId: {}".format(location.corporation.corporation_id))
        print("Settings: {}".format(location.settings))

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

smartdiagnostics-sdk-3.16.3.tar.gz (82.3 kB view details)

Uploaded Source

Built Distribution

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

smartdiagnostics_sdk-3.16.3-py3-none-any.whl (319.6 kB view details)

Uploaded Python 3

File details

Details for the file smartdiagnostics-sdk-3.16.3.tar.gz.

File metadata

  • Download URL: smartdiagnostics-sdk-3.16.3.tar.gz
  • Upload date:
  • Size: 82.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5

File hashes

Hashes for smartdiagnostics-sdk-3.16.3.tar.gz
Algorithm Hash digest
SHA256 76dc1c072566aabe851d9f7130200949294143f45a1be7f14f7c462a73879e30
MD5 c79e8b40ff8e100c1a3aa6d5ade56cf1
BLAKE2b-256 d434a8e7f758ef20f73c2308f0dde0372f6d25743a3ef5db0bbb674d6e2eb8c8

See more details on using hashes here.

File details

Details for the file smartdiagnostics_sdk-3.16.3-py3-none-any.whl.

File metadata

  • Download URL: smartdiagnostics_sdk-3.16.3-py3-none-any.whl
  • Upload date:
  • Size: 319.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5

File hashes

Hashes for smartdiagnostics_sdk-3.16.3-py3-none-any.whl
Algorithm Hash digest
SHA256 655ba873708baa12a021282ae2680581504cf9e63f12348fbbe1ff82151d800e
MD5 335356364573c4293a01c0a02887f3cd
BLAKE2b-256 38b1f351ac6ca59c3b724630a044e1d5bdfe54fea31b55e12db798fb1af9015c

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