Add a document download client

Allows uploading documents to the Document Download API.
The client is configured with an API host and auth token. There's
no need for a flag to disable the client in the test environments
at the moment since the upload is only triggered by a specific
payload which would only be sent with an explicit goal of using
document download.
This commit is contained in:
Alexey Bezhan
2018-04-04 17:31:02 +01:00
parent a0c6f6a22e
commit 204aaf172d
4 changed files with 70 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
import base64
import requests
class DocumentDownloadClient:
def init_app(self, app):
self.api_host = app.config['DOCUMENT_DOWNLOAD_API_HOST']
self.auth_token = app.config['DOCUMENT_DOWNLOAD_API_KEY']
def get_upload_url(self, service_id):
return "{}/services/{}/documents".format(self.api_host, service_id)
def upload_document(self, service_id, file_contents):
response = requests.post(
self.get_upload_url(service_id),
headers={
'Authorization': "Bearer {}".format(self.auth_token),
},
files={
'document': base64.b64decode(file_contents)
}
)
response.raise_for_status()
return response.json()['document']['url']