The Blockspring Python package comes with a powerful API to easily run and define your functions.
To install the package, run
pip install blockspring from the command line.
Here are the package methods:
Run(function_id, params, [api_key]): Execute code remotely given a function_id, a dictionary of input parameters, and an optional api_key. You will receive a raw Blockspring response. If your function outputs valid JSON, that JSON will be parsed and returned. If your function returned files, they will be output as base64 strings.
RunParsed(function_id, params, [api_key]): This does the same as
Runexcept it will return a
Requestobject (see below) instead of a raw Blockspring response. Keep in mind, files returned by your function will no longer be base64 strings, but saved to a temp directory with their location returned to you.
Define(your_function): Tell Blockspring what to execute on runtime. Pass in the name of your function.
Request: When you define a function, you have access to a request object. Request parses inputs going into your function and comes with two things:
Params: This is a object of all of the input parameters passed into your function. Any file sent as an input to your file will be saved to a temp directory and its location passed into Params.
getErrors(): This is a method that lists off any input errors. It's useful when chaining functions.
getHeaders(): This is a method that lists off response headers when you execute
runParsed. It's useful for checking rate limits, etc.
Response: When you define a function, you have access to helpers for building your JSON response:
addOutput(key, value): Pass in any key and value to be added to your output. These will show up in a green box on your function's homepage.
addErrorOutput(title, [message]): This is for catching errors in your function. Pass in an error title and optional error message. These will show up in a red box on your function's homepage.
addFileOutput(key, file_name): This is for adding files to your output. Blockspring renders images and csv files when running functions on your homepage. Otherwise, it passes files along between functions be encoding/decoding them in base64.
end(): This prints your output as JSON.
As an example, let's build a comprehensive function that calls other Blockspring functions, outputs an image, and has error handling. First, play around with the final product live on Blockspring.
Below is the code. Click to preload the blockspring editor with our code, test out the function for yourself, and feel free to publish to your account.