DocumentEngine   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 35
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get_document() 0 9 1
A __init__() 0 3 1
A create_documents() 0 19 4
1
# -*- coding: utf-8 -*-
2
from zope.interface import Interface, implementer
3
import requests
4
import json
5
6
7
class IDocumentEngine(Interface):
8
    def create_documents(self, data, system_token, callback_url=None):
9
        '''create the documents with incoming data with optional callback_url'''
10
11
    def get_document(self, generation_id, system_token, accept='application/pdf'):
12
        '''get the document. Accept is either application/pdf (default) or either text/html'''
13
14
15
@implementer(IDocumentEngine)
16
class DocumentEngine(object):
17
    def __init__(self, baseurl, template_id):
18
        self.baseurl = baseurl
19
        self.template_id = template_id
20
21
    def create_documents(self, data, system_token, callback_url=None, callback_method=None, template_version=None):
22
        '''create the documents with incoming data with optional callback_url'''
23
        generation_data = {
24
            'template_id': self.template_id,
25
            'data': data
26
        }
27
        if template_version is not None:
28
            generation_data['template_version'] = template_version
29
        if callback_url is not None:
30
            generation_data['callback_method'] = callback_method if callback_method is not None else 'POST'
31
            generation_data['callback_url'] = callback_url
32
        headers = {
33
            'OpenAmSSOID': system_token,
34
            'Accept': 'application/json',
35
            'Content-Type': 'application/json'
36
        }
37
        res = requests.post('{0}/generations'.format(self.baseurl), json=generation_data, headers=headers)
38
        res.raise_for_status()
39
        return json.loads(res.text)['id']
40
41
    def get_document(self, generation_id, system_token, accept='application/pdf'):
42
        '''get the document. Accept is either application/pdf (default) or either text/html'''
43
        headers = {
44
            'OpenAmSSOID': system_token,
45
            'Accept': accept
46
        }
47
        res = requests.get('{0}/generations/{1}/document'.format(self.baseurl, generation_id), headers=headers)
48
        res.raise_for_status()
49
        return res.content
50
51
52