flask-snow

Adds ServiceNow support to Flask.

Installation

Use pip to install the extension:

$ pip install flask-snow

Usage

flask-snow is managed through a Snow instance:

from flask import Flask
from flask_snow import Snow

app = Flask(__name__)
snow = Snow(app)

you may also set up your Snow instance later using init_app method:

snow = Snow()

app = Flask(__name__)
snow.init_app(app)

Once created, resources may be created:

incident = snow.resource(api_path='/table/incident')
record = incident.get(query={}, limit=1).first()

Check out the pysnow API documentation for more info.

Configuration

The following configuration values exist for flask-snow.

Name Default value Description
SNOW_INSTANCE None Instance name
SNOW_HOST None FQDN (instead of instance)
SNOW_USE_SSL True Whether or not to use SSL
SNOW_RAISE_ON_EMPTY False Raise an exception on empty result sets

Authentication

The following authentication methods are supported:

  • Static - Set a static username and password to use for all requests
  • Oauth - set client id and secret, and authenticate users in the application context
  • session - pre-made requests-compatible session

Static

Name Default value Description
SNOW_USER None Username
SNOW_PASSWORD None Password

Oauth

Name Default value Description
SNOW_OAUTH_CLIENT_ID None Client ID (from ServiceNow)
SNOW_OAUTH_CLIENT_SECRET None Client secret (from ServiceNow)

Session

Passing a custom session to flask-snow doesn’t require any configuration. Simply:

import requests
from flask import Flask
from flask_snow import Snow

s = requests.Session()

# Lets disable SSL verification
s.verify = False
s.auth = requests.auth.HTTPBasicAuth('myusername', 'mypassword')

snow = Snow()

app = Flask(__name__)
snow.init_app(app, session=s)

Examples

Full examples can be found here

API

class flask_snow.Snow(app=None)

Central controller class. Provides token_updater setter, context handling and basic / oauth client logic

Parameters:app – App to pass directly to Snow (if not using factory)
connection

Snow connection instance, stores a pysnow.Client instance and pysnow.Resource instances

Creates a new pysnow.Client object if it doesn’t exist in the app slice of the context stack

Returns:pysnow.Client object
init_app(app, session=None)

Initializes snow extension

Set config default and find out which client type to use

Parameters:
  • app – App passed from constructor or directly to init_app (factory)
  • session – requests-compatible session to pass along to init_app
Raises:
  • ConfigError - if unable to determine client type
resource(*args, **kwargs)

Creates a new pysnow.Resource object if it doesn’t exist in its clients context

Parameters:
  • args – args to pass along to pysnow.Resource
  • kwargs – kwargs to pass along to pysnow.Resource
Returns:

pysnow.Resource object

set_token(token)

Calls token_updater callback with the new token after updating the pysnow.Client object in the app context

Parameters:token – token dict returned from previous interactions with flask-snow
token_updater

Callback function called when a token update occurs

Use to keep track of your user OAuth tokens:

token_updater(updated_token):
    session['token'] = updated_token

snow.init_app(app)
snow.token_updater = token_updater