Skip to main content

module for working with the armorapi

Project description

Armor Python API Auth wrapper

What is it

This package provides a simple python interface for interacting with the Armor.com API. It provides a simple mechanism for authentication and making API requests. Specific API requests are down to the developer, this package simply provides authentication mechanisms and the means to make requests.

Installation

From PyPi $ pip install armorapi

Use

Authentication

The package provides both v1 and v2 authentication. v1 authentication is the default method. v2 authentication requires MFA, v1 does NOT.

v1 authentication

from armorapi import *
aa = ArmorApi(username,password)

v2 authentication

from armorapi import *
aa = ArmorApi(username,password,auth=2)

v1 authorisation tokens are valid for 15 minutes, the api object provides a simple means to reissue a token, this updates the authorisation token value to be sent with the next request:

aa.v1_reissue_authorisation_token()

Reissuing of tokens is performed in a thread safe manner, therefore v1 token reissue can be set in a separate threading.timer thread for a seamless update process (in the case of this example every 10 minutes):

import threading
from armorapi import *
aa = ArmorApi(username,password,auth=2)

timer = threading.timer(600, aa.v1_reissue_authorisation_token)
timer.start()

V2 authentication doesn't have a token reissue mechanism.

The api object handles by default 4 authentication failures every 10 minutes, i.e. if a 401 http response code is returned it will attempt to reauthenticate, but will only do this in 4 times in a 10 minute period before causing an exception. The number of attempts before exception in a 10 minute period can be set as desired:

from armorapi import *
aa = ArmorApi(username,password, retries401=8)

Account IDs

By default the api object will use the first Armor account ID assigned to the user authenticating without the user needing to set and account ID. In many cases this will be fine as generally users are only assigned to one account, but in cases where a user has multiple accounts a specific account can be selected at instantiation:

from armorapi import *
aa = ArmorApi(username,password, accountid=<account_id>)

api requests

The api object has the public method 'make_request' available, intended for making api requests

from armorapi import *
aa = ArmorApi(username,password)
response = aa.make_request('https://api.armor.com/me')

make_request performs a GET request by default, POST and PUT are also available methods, post and put requests accept a data input of json like data made up of dictionaries and/or lists.

from armorapi import *
aa = ArmorApi(username,password)
response = aa.make_request('https://api.armor.com/me', method='POST', data={'key': 'value', 'key2': 'value2'})

HTTP Headers and more

Although the api object sets account and authorisation headers, many API requests rely on custom http headers in both the request and response. the api object levarages the python requests module, specifically a requests session. All requests session methods and members are available for use, see the requests doucmentation for further advance use. https headers can be added at the session level so the header persists across requests:

from armorapi import *
aa = ArmorApi(username,password)
aa.session.headers.update({'Range': 'entities=0-10; max=10'})

If a header value is only needed for a single request dict type values can be passed with make_request method, the values will be merged with the session-level values that are set. The method-level parameters override session parameters. Method-level parameters will not be persisted across requests:

from armorapi import *
aa = ArmorApi(username,password)
response = aa.make_request('https://api.armor.com/me', headers={'Range': 'entities=0-10; max=10'})

`

api responses

Returned responses are requests.Response objects, please see the linked requests documentation for full details, however key features are:

from pprint import pprint
from armorapi import *
aa = ArmorApi(username,password)
response = aa.make_request('https://api.armor.com/me')
response.headers
    {'Content-Length': '1221', 'Content-Type': 'application/json; charset=utf-8', 'Server': 'Microsoft-HTTPAPI/2.0'}
pprint(response.json())
    {'accounts': [{'accountType': 'Direct',
               'currency': 'USD       ',
               'id': 0007,
               'isSynced': True,
               'name': 'Documentation Example Account',
               'parent': 1,
                ....

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

armorapi-0.0.3.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

armorapi-0.0.3-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: armorapi-0.0.3.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.2 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.8

File hashes

Hashes for armorapi-0.0.3.tar.gz
Algorithm Hash digest
SHA256 6861c549cbf892a2e69ce492517a971106d5dd839f7497eaaa18affd3d3eb067
MD5 014ddc41a41ea055a6097a1a7d5f8c2a
BLAKE2b-256 642020bb98f123ec80fe2551174597b537123d251529399f2cbe8b93ff210f68

See more details on using hashes here.

File details

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

File metadata

  • Download URL: armorapi-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.2 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.8

File hashes

Hashes for armorapi-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 088aa23f08f3f715575c50db1e4a8ccf01437a6a7da12fbe3ddbafdda4bf5b71
MD5 63362f92a5c2984e491af3a75a94eccf
BLAKE2b-256 baeec782cabc313953c13d9db80a9a0fd167398852cbe19efdac933a1227734e

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