| Total Complexity | 6 |
| Total Lines | 38 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import json |
||
| 2 | from os import environ |
||
| 3 | import requests |
||
| 4 | |||
| 5 | |||
| 6 | # TODO: This is just a conceptual implementation and first try to |
||
| 7 | # trigger further discussions on how this should look like. |
||
| 8 | class Client: |
||
| 9 | def __init__( |
||
| 10 | self, |
||
| 11 | server_addr_processing: str = environ.get('OCRD_NETWORK_SERVER_ADDR_PROCESSING', ''), |
||
| 12 | server_addr_workflow: str = environ.get('OCRD_NETWORK_SERVER_ADDR_WORKFLOW', ''), |
||
| 13 | server_addr_workspace: str = environ.get('OCRD_NETWORK_SERVER_ADDR_WORKSPACE', ''), |
||
| 14 | ): |
||
| 15 | self.server_addr_processing = server_addr_processing |
||
| 16 | self.server_addr_workflow = server_addr_workflow |
||
| 17 | self.server_addr_workspace = server_addr_workspace |
||
| 18 | |||
| 19 | def send_processing_request(self, processor_name: str, req_params: dict): |
||
| 20 | verify_server_protocol(self.server_addr_processing) |
||
| 21 | req_url = f'{self.server_addr_processing}/processor/{processor_name}' |
||
| 22 | req_headers = {"Content-Type": "application/json; charset=utf-8"} |
||
| 23 | req_json = json.loads(json.dumps(req_params)) |
||
| 24 | |||
| 25 | print(f'Sending processing request to: {req_url}') |
||
| 26 | response = requests.post(url=req_url, headers=req_headers, json=req_json) |
||
| 27 | return response.json() |
||
| 28 | |||
| 29 | |||
| 30 | def verify_server_protocol(address: str): |
||
| 31 | protocol_matched = False |
||
| 32 | for protocol in ['http://', 'https://']: |
||
| 33 | if address.startswith(protocol): |
||
| 34 | protocol_matched = True |
||
| 35 | break |
||
| 36 | if not protocol_matched: |
||
| 37 | raise ValueError(f'Wrong/Missing protocol in the server address: {address}') |
||
| 38 |