forked from open-lambda/open-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopen_lambda.py
62 lines (44 loc) · 1.84 KB
/
open_lambda.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
''' OpenLambda's Python API '''
import json as pyjson
from requests import Session
class OpenLambda:
''' Represents a client connection to OpenLambda '''
def __init__(self, address="localhost:5000"):
self._address = address
self._session = Session()
def _post(self, path, data=None):
''' Issues a _post request to the OL worker '''
return self._session.post(f'http://{self._address}/{path}', pyjson.dumps(data))
def run(self, fn_name, args, json=True):
''' Execute a serverless function '''
req = self._post(f"run/{fn_name}", args)
self._check_status_code(req, "run")
if json:
return req.json()
return req.text
def create(self, args):
''' Create a new sandbox '''
req = self._post("create", args)
self._check_status_code(req, "create")
return req.text.strip()
def destroy(self, sandbox_id):
''' Destroy a new sandbox '''
req = self._post(f"destroy/{sandbox_id}", {})
self._check_status_code(req, "destroy")
def pause(self, sandbox_id):
''' Pause a new sandbox '''
req = self._post(f"pause/{sandbox_id}", None)
self._check_status_code(req, "pause")
def get_statistics(self):
''' Returns stats of the OpenLambda server '''
req = self._session.get(f"http://{self._address}/stats")
self._check_status_code(req, "get_statistics")
return req.json()
@staticmethod
def _check_status_code(req, name):
if req.status_code != 200:
raise Exception(f'"{name}" failed with status code {req.status_code}: {req.text}')
def check_status(self):
''' Checks the status of the OpenLambda server '''
req = self._session.get(f"http://{self._address}/status")
self._check_status_code(req, "check_status")