8 VerificationKey, Signature, export_digest, export_verification_key, \
10 from .server_state
import ServerState
11 from .contributor_list
import ContributorList
12 from typing
import Optional, Union
13 from requests
import post, get, Response
14 from os.path
import join, exists
18 SERVER_BUSY_503_CLIENT_MSG =
"Server is busy. Retrying ..."
26 cert_path: Optional[str] =
None,
27 insecure: bool =
False):
28 assert not cert_path
or exists(cert_path)
30 self.verify: Union[bool, str,
None] =
False if insecure
else cert_path
35 Get the status of the server.
38 resp = get(join(self.
base_url,
"contributors"), verify=self.verify)
39 if resp.status_code == 503:
40 print(SERVER_BUSY_503_CLIENT_MSG)
44 resp.raise_for_status()
45 return ContributorList.from_json(resp.content.decode())
50 Get the status of the server.
53 resp = get(join(self.
base_url,
"state"), verify=self.verify)
54 if resp.status_code == 503:
55 print(SERVER_BUSY_503_CLIENT_MSG)
59 resp.raise_for_status()
60 return ServerState.from_json(resp.content.decode())
64 GET /challenge request, downloading to file
71 def _get_challenge() -> Response:
78 with _get_challenge()
as resp:
79 if resp.status_code == 503:
80 print(SERVER_BUSY_503_CLIENT_MSG)
84 resp.raise_for_status()
85 with open(challenge_file,
"wb")
as out_f:
86 for chunk
in resp.iter_content(chunk_size=CHUNK_SIZE):
93 response_digest: bytes,
94 verification_key: VerificationKey,
95 signature: Signature) ->
None:
97 POST /contribute, uploading from file with all authentication headers
104 with open(response_file,
"rb")
as upload_f:
107 files={
'response': upload_f},
110 resp.raise_for_status()