A Python 3 client for Spark API: http://sparkplatform.com/docs/overview/api
TODO
import sparkler
from sparkler.client import SparkClient
# Defaults are noted as the string values, unless those values are in all caps --
# those values should be replaced with the values assigned to your API key.
client = SparkClient({
"key" :"YOUR_CLIENT_KEY",
"secret" :"YOUR_CLIENT_SECRET",
"auth_callback_uri":"YOUR_CALLBACK_URI",
"api_user_agent" :"YOUR_CUSTOM_API_CLIENT_NAME",
"auth_mode" :"hybrid"
})
# Do the following only if you already have an access and refresh token
client.register_session("YOUR_ACCESS_TOKEN", "YOUR_REFRESH_TOKEN")
try:
listings = client.get("listings")
# Or...
# contact = client.post("contacts", {"DisplayName": "Joshua Murray", "PrimaryEmail": "[email protected]"})
# contact = client.put("/contacts/20100000000000000000000000", {"DisplayName": "Joshua Murphy"})
# response = client.delete("/contacts/20100000000000000000000000")
except sparkler.exceptions.ApplicationUnauthorizedException:
print("Go here and get your code: %s" % client.auth.authorization_uri())
print("Send that code to client.auth.grant('CODE')")
except sparkler.exceptions.AuthExpiredException:
client.auth.refresh() # If the refresh is successful, attempt your request again.
import sparkler
from sparkler.client import SparkClient
# Defaults are noted as the string values, unless those values are in all caps --
# those values should be replaced with the values assigned to your API key.
client = SparkClient({
"key" :"YOUR_CLIENT_KEY",
"secret" :"YOUR_CLIENT_SECRET",
"auth_callback_uri":"YOUR_CALLBACK_URI",
"api_user_agent" :"YOUR_CUSTOM_API_CLIENT_NAME",
"auth_mode" :"oauth2" # Default is "hybrid"
})
# Do the following only if you already have an access and refresh token
client.register_session("YOUR_ACCESS_TOKEN", "YOUR_REFRESH_TOKEN")
try:
listings = client.get("listings")
except sparkler.exceptions.ApplicationUnauthorizedException:
print("Go here and get your code: %s" % client.auth.authorization_uri())
print("Send that code to client.auth.grant('CODE')")
except sparkler.exceptions.AuthExpiredException:
client.auth.refresh() # If the refresh is successful, attempt your request again.
Not supported. We encourage you to ask for an OAuth 2 key as a replacement for a SparkApi Auth key, if that's what you were provided.
While these are applied with defaults, you can further configure the client with the parameter examples below:
client = SparkClient({
# ... required parameters omitted ...
"auth_endpoint_uri":"https://sparkplatform.com/openid",
"api_endpoint_uri" :"https://sparkapi.com",
"data_access_version": "v1"
})
The SparkApi client creates a global logger with the name 'spark_client'. This can be accessed directly by the SparkClient.logger attribute. For example, to change the client's logging to DEBUG:
import logging
SparkClient.logger.setLevel(logging.DEBUG)
The logger level is set to INFO by default.
Custom sparkler exceptions are available in exceptions.py. They are fairly well documented in that file, but to reiterate:
Raised when a required client configuration setting is not supplied.
Raised when a request to the API resulted in a non-2xx code, and did not fit the use case for another exception above. This exception instance will have the public attribute "response", which is an instance of sparkler.response.Response() and contains the API error code and error message.
All other exceptions extend HttpStatusNotSuccessfulException
Raised if the application has not yet been authorized by the end user to access to the API.
Raised when the authorization token has expired, thus requiring a refresh or a new token.
Raised when a step in the authorization process has failed.
- Auto-refresh oauth2
- Clean up imports
- Top-down reconfiguration on the fly