|
| 1 | +# encoding: utf-8 |
| 2 | + |
| 3 | + |
| 4 | +import json |
| 5 | +import sys |
| 6 | +import requests |
| 7 | + |
| 8 | + |
| 9 | +# plugin response functions |
| 10 | + |
| 11 | +# the direct response from the plugin uses stdin and stderr |
| 12 | + |
| 13 | + |
| 14 | +def stdout(msg: str): |
| 15 | + sys.stdout.write(msg) |
| 16 | + sys.stdout.write('\n') |
| 17 | + |
| 18 | + |
| 19 | +def stderr(msg: str): |
| 20 | + sys.stderr.write(msg) |
| 21 | + sys.stderr.write('\n') |
| 22 | + |
| 23 | + |
| 24 | +def return_response(response: dict): |
| 25 | + stdout(json.dumps(response)) |
| 26 | + exit(0) |
| 27 | + |
| 28 | + |
| 29 | +def return_error_response(error: str): |
| 30 | + stderr(f'error in fylr-plugin-example: {error}') |
| 31 | + return_response( |
| 32 | + { |
| 33 | + 'error': { |
| 34 | + 'code': 'fylr-plugin-example.error', |
| 35 | + 'statuscode': 400, |
| 36 | + 'description': error, |
| 37 | + 'error': error, |
| 38 | + }, |
| 39 | + }, |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +# ----------------------------- |
| 44 | + |
| 45 | +# helper functions to parse callback data from fylr |
| 46 | + |
| 47 | +# the api url and access token are necessary if the plugin |
| 48 | +# needs to read/write more data over the fylr api |
| 49 | + |
| 50 | + |
| 51 | +def get_api_url(callback_data): |
| 52 | + url = get_json_value(callback_data, 'info.api_url') |
| 53 | + if not url: |
| 54 | + return_error_response('info.api_url missing!') |
| 55 | + return f'{url}/api/v1' |
| 56 | + |
| 57 | + |
| 58 | +def get_access_token(callback_data): |
| 59 | + access_token = get_json_value(callback_data, 'info.api_user_access_token') |
| 60 | + if not access_token: |
| 61 | + return_error_response('info.api_user_access_token missing!') |
| 62 | + return access_token |
| 63 | + |
| 64 | + |
| 65 | +# ----------------------------- |
| 66 | + |
| 67 | +# fylr api functions |
| 68 | + |
| 69 | +# the plugin can call the fylr api to read/write more data |
| 70 | + |
| 71 | + |
| 72 | +def fylr_api_headers(access_token): |
| 73 | + return {'authorization': f'Bearer {access_token}'} |
| 74 | + |
| 75 | + |
| 76 | +def get_from_api(api_url, path, access_token): |
| 77 | + resp = requests.get( |
| 78 | + url=f'{api_url}/{path}', |
| 79 | + headers=fylr_api_headers(access_token), |
| 80 | + ) |
| 81 | + |
| 82 | + return resp.text, resp.status_code |
| 83 | + |
| 84 | + |
| 85 | +def post_to_api(api_url, path, access_token, payload=None): |
| 86 | + resp = requests.post( |
| 87 | + url=f'{api_url}/{path}', |
| 88 | + headers=fylr_api_headers(access_token), |
| 89 | + data=payload, |
| 90 | + ) |
| 91 | + |
| 92 | + return resp.text, resp.status_code |
| 93 | + |
| 94 | + |
| 95 | +# ----------------------------- |
| 96 | + |
| 97 | + |
| 98 | +def get_json_value(js, path, expected=False, split_char='.'): |
| 99 | + |
| 100 | + current = js |
| 101 | + path_parts = [] |
| 102 | + current_part = '' |
| 103 | + |
| 104 | + for i in range(len(path)): |
| 105 | + if path[i] != split_char: |
| 106 | + current_part += path[i] |
| 107 | + if i == len(path) - 1: |
| 108 | + path_parts.append(current_part) |
| 109 | + continue |
| 110 | + |
| 111 | + if i > 0 and path[i - 1] == '\\': |
| 112 | + current_part += path[i] |
| 113 | + continue |
| 114 | + |
| 115 | + if len(current_part) > 0: |
| 116 | + path_parts.append(current_part) |
| 117 | + current_part = '' |
| 118 | + |
| 119 | + for path_part in path_parts: |
| 120 | + path_part = path_part.replace('\\' + split_char, split_char) |
| 121 | + |
| 122 | + if not isinstance(current, dict) or path_part not in current: |
| 123 | + if expected: |
| 124 | + raise Exception('expected: ' + path_part) |
| 125 | + else: |
| 126 | + return None |
| 127 | + |
| 128 | + current = current[path_part] |
| 129 | + |
| 130 | + return current |
0 commit comments