Passed
Pull Request — master (#1030)
by Konstantin
02:43
created

ocrd_network.client   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 29
dl 0
loc 38
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A Client.__init__() 0 9 1
A Client.send_processing_request() 0 9 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A verify_server_protocol() 0 8 4
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