|
| 1 | +# All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +# not use this file except in compliance with the License. You may obtain |
| 5 | +# a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | + |
| 15 | +import functools |
| 16 | +import os |
| 17 | +import requests |
| 18 | + |
| 19 | + |
| 20 | +from fdk.application import errors |
| 21 | + |
| 22 | + |
| 23 | +def fn_app(fn_app_class): |
| 24 | + """ |
| 25 | + Sets up Fn app with config |
| 26 | +
|
| 27 | + @fn_app |
| 28 | + class MyApp(object): |
| 29 | +
|
| 30 | + def __init__(self, *args, **kwargs): |
| 31 | + pass |
| 32 | +
|
| 33 | + @fn_route( |
| 34 | + fn_image="denismakogon/hot-json-python:0.0.1", |
| 35 | + fn_type="sync", |
| 36 | + fn_memory=256, |
| 37 | + fn_format="json", |
| 38 | + fn_timeout=60, |
| 39 | + fn_idle_timeout=200, |
| 40 | + ) |
| 41 | + def do_stuff(self, *args, fn_data=None, **kwargs): |
| 42 | + pass |
| 43 | +
|
| 44 | + :param fn_app_class: any class |
| 45 | + :return: class decorator |
| 46 | + """ |
| 47 | + @functools.wraps(fn_app_class) |
| 48 | + def wrapper(*args, **kwargs): |
| 49 | + app_name = fn_app_class.__name__ |
| 50 | + fn_api_url = os.environ.get("API_URL") |
| 51 | + requests.get(fn_api_url).raise_for_status() |
| 52 | + fn_app_url = "{}/v1/apps/{}".format( |
| 53 | + fn_api_url, app_name.lower()) |
| 54 | + del_resp = requests.delete(fn_app_url) |
| 55 | + if del_resp.status_code != 404: |
| 56 | + del_resp.close() |
| 57 | + del_resp.raise_for_status() |
| 58 | + requests.post( |
| 59 | + "{}/v1/apps".format(fn_api_url), |
| 60 | + json={ |
| 61 | + "app": { |
| 62 | + "name": app_name.lower(), |
| 63 | + "config": kwargs.get("config"), |
| 64 | + }, |
| 65 | + } |
| 66 | + ).raise_for_status() |
| 67 | + return fn_app_class(*args) |
| 68 | + |
| 69 | + return wrapper |
| 70 | + |
| 71 | + |
| 72 | +def fn_route(fn_image=None, fn_type=None, |
| 73 | + fn_memory=256, fn_format=None, |
| 74 | + fn_timeout=60, fn_idle_timeout=200, |
| 75 | + fn_method="GET"): |
| 76 | + """ |
| 77 | + Sets up Fn app route based on parameters given above |
| 78 | + :param fn_image: Docker image |
| 79 | + :type fn_image: str |
| 80 | + :param fn_type: Fn route type (async/sync) |
| 81 | + :type fn_type: str |
| 82 | + :param fn_memory: Fn RAM to allocate |
| 83 | + :type fn_memory: int |
| 84 | + :param fn_format: Fn route format to accept |
| 85 | + :type fn_format: str |
| 86 | + :param fn_timeout: Fn route call timeout |
| 87 | + :type fn_timeout: int |
| 88 | + :param fn_idle_timeout: Fn route idle timeout (timeout between calls) |
| 89 | + :type fn_idle_timeout: int |
| 90 | + :param fn_method: HTTP method to use when calling Fn function |
| 91 | + :type fn_method: str |
| 92 | + :return: monkey-patched action (almost the same as decorated) |
| 93 | + """ |
| 94 | + |
| 95 | + def ext_wrapper(action): |
| 96 | + @functools.wraps(action) |
| 97 | + def inner_wrapper(*f_args, **f_kwargs): |
| 98 | + fn_api_url = os.environ.get("API_URL") |
| 99 | + requests.get(fn_api_url).raise_for_status() |
| 100 | + self = f_args[0] |
| 101 | + fn_path = action.__name__.lower() |
| 102 | + if not hasattr(action, "__path_created"): |
| 103 | + fn_routes_url = "{}/v1/apps/{}/routes".format( |
| 104 | + fn_api_url, self.__class__.__name__.lower()) |
| 105 | + resp = requests.post(fn_routes_url, json={ |
| 106 | + "route": { |
| 107 | + "path": "/{}".format(fn_path), |
| 108 | + "image": fn_image, |
| 109 | + "memory": fn_memory if fn_memory else 256, |
| 110 | + "type": fn_type if fn_type else "sync", |
| 111 | + "format": fn_format if fn_format else "default", |
| 112 | + "timeout": fn_timeout if fn_timeout else 60, |
| 113 | + "idle_timeout": (fn_idle_timeout if |
| 114 | + fn_idle_timeout else 120), |
| 115 | + }, |
| 116 | + }) |
| 117 | + |
| 118 | + try: |
| 119 | + resp.raise_for_status() |
| 120 | + except requests.HTTPError: |
| 121 | + resp.close() |
| 122 | + return None, Exception(resp.content) |
| 123 | + |
| 124 | + setattr(action, "__path_created", True) |
| 125 | + |
| 126 | + fn_exec_url = "{}/r/{}/{}".format( |
| 127 | + fn_api_url, self.__class__.__name__.lower(), fn_path) |
| 128 | + req = requests.Request(method=fn_method, |
| 129 | + url=fn_exec_url, |
| 130 | + json=f_kwargs) |
| 131 | + session = requests.Session() |
| 132 | + resp = session.send(req.prepare()) |
| 133 | + |
| 134 | + try: |
| 135 | + resp.raise_for_status() |
| 136 | + except requests.HTTPError: |
| 137 | + resp.close() |
| 138 | + return None, errors.FnError( |
| 139 | + "{}/{}".format(self.__class__.__name__.lower(), fn_path), |
| 140 | + resp.content) |
| 141 | + |
| 142 | + f_kwargs.update(fn_data=resp.text) |
| 143 | + |
| 144 | + resp.close() |
| 145 | + return action(*f_args, **f_kwargs), None |
| 146 | + |
| 147 | + return inner_wrapper |
| 148 | + |
| 149 | + return ext_wrapper |
0 commit comments