Skip to main content

Function Developer Kit for Python

Project description

FN development kit for Python
=============================

Purpose of this library to provide simple interface to parse HTTP 1.1 requests represented as string

Following examples are showing how to use API of this library to work with streaming HTTP requests from Fn service.

Handling Hot JSON Functions
---------------------------

A main loop is supplied that can repeatedly call a user function with a series of HTTP requests.
In order to utilise this, you can write your `app.py` as follows:

```python
import fdk


def handler(context, data=None, loop=None):
return data


if __name__ == "__main__":
fdk.handle(handler)

```

Applications powered by Fn: Concept
-----------------------------------

FDK is not only about developing functions, but providing necessary API to build serverless applications
that look like nothing but classes with methods powered by Fn.

```python
import requests

from fdk.application import decorators


@decorators.fn_app
class Application(object):

def __init__(self, *args, **kwargs):
pass

@decorators.with_fn(fn_image="denismakogon/fdk-python-echo:0.0.1")
def env(self, fn_data=None, **kwargs):
return fn_data

@decorators.fn(fn_type="sync")
def square(self, x, y, *args, **kwargs):
return x * y

@decorators.fn(fn_type="sync", dependencies={
"requests_get": requests.get
})
def request(self, *args, **kwargs):
requests_get = kwargs["dependencies"].get("requests_get")
r = requests_get('https://api.github.com/events')
r.raise_for_status()
return r.text


if __name__ == "__main__":
app = Application(config={})

res, err = app.env()
if err:
raise err
print(res)

res, err = app.traceback()
if err:
raise err
print(res)

res, err = app.square(10, 20)
if err:
raise err
print(res)

res, err = app.request()
if err:
raise err
print(res)

```
In order to identify to which Fn instance code needs to talk set following env var:

```bash
export FN_API_URL = http://localhost:8080
```
with respect to IP address or domain name where Fn lives.


Applications powered by Fn: supply data to a function
-----------------------------------------------------

At this moment those helper - decorators let developers interact with Fn - powered functions as with regular class methods.
In order to pass necessary data into a function developer just needs to do following
```python

if __name__ == "__main__":
app = Application(config={})

app.env(keyone="blah", keytwo="blah", somethingelse=3)

```
Key - value args will be turned into JSON instance and will be sent to a function as payload body.


Applications powered by Fn: working with function's result
----------------------------------------------------------

In order to work with result from function you just need to read key - value argument `fn_data`:
```python
@decorators.with_fn(fn_image="denismakogon/py-traceback-test:0.0.1",
fn_format="http")
def traceback(self, fn_data=None):
return fn_data
```

Applications powered by Fn: advanced serverless functions
---------------------------------------------------------

Since release v0.0.3 developer can consume new API to build truly serverless functions
without taking care of Docker images, application, etc.

```python
@decorators.fn(fn_type="sync")
def square(self, x, y, *args, **kwargs):
return x * y

@decorators.fn(fn_type="sync", dependencies={
"requests_get": requests.get
})
def request(self, *args, **kwargs):
requests_get = kwargs["dependencies"].get("requests_get")
r = requests_get('https://api.github.com/events')
r.raise_for_status()
return r.text
```

Each function decorated with `@decorator.fn` will become truly serverless and distributed.
So, how it works?

* A developer writes function
* FDK(Fn - powered app) creates a recursive Pickle v4.0 with 3rd - party dependencies
* FDK(Fn - powered app) transfers pickled object to a function based on Python3 GPI(general purpose image)
* FDK unpickles function and its 3rd - party dependencies and runs it
* Function sends response back to Fn - powered application function caller

So, each CPU - intensive functions can be sent to Fn with the only load on networking(given example creates 7kB of traffic between app's host and Fn).


Applications powered by Fn: exceptions
--------------------------------------

Applications powered by Fn are following Go - like errors concept. It gives you full control on errors whether raise them or not.
```python
res, err = app.env()
if err:
raise err
print(res)

```
Each error is an instance fn `FnError` that encapsulates certain logic that makes hides HTTP errors and turns them into regular Python - like exceptions.



Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fdk-0.0.24.tar.gz (18.2 kB view details)

Uploaded Source

Built Distribution

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

fdk-0.0.24-py2.py3-none-any.whl (18.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file fdk-0.0.24.tar.gz.

File metadata

  • Download URL: fdk-0.0.24.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for fdk-0.0.24.tar.gz
Algorithm Hash digest
SHA256 97f4baadf6a2e29e93e3dd880c5ca797b7effa903042a07575fc9fb435e801bd
MD5 b12dcbf8709e2bd6e9e14e3a53f35070
BLAKE2b-256 fed37d102a779dbbee53891a39fb1d38ff6c92e7dfd500a763dc1476d76bc267

See more details on using hashes here.

File details

Details for the file fdk-0.0.24-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for fdk-0.0.24-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d11bacf401d148fb40cb0e8522f8900a88e4760f20f60381df345dadbf3b170e
MD5 71f9427989bb88be0bb84a1f1de3ce65
BLAKE2b-256 45be877bd11f03a2a1c7e65c781ed2c70a49e773ba9cbd8205237fc72f525a8e

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