Skip to content

Commit

Permalink
allow to save document related files
Browse files Browse the repository at this point in the history
  • Loading branch information
genaromadrid committed Aug 9, 2016
1 parent c8026e4 commit 0e25c50
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Expand Up @@ -65,6 +65,21 @@ doc.id # -> '7500e528-ac6f-4ad3-9afd-74487c11576a'
doc.id # -> '7500e528-ac6f-4ad3-9afd-74487c11576a'
```

- Save Document related files

```python
from mifiel import Document, Client
client = Client(app_id='APP_ID', secret_key='APP_SECRET')

doc = Document.find(client, 'id')
# save the original file
doc.save_file('path/to/save/file.pdf')
# save the signed file (original file + signatures page)
doc.save_file_signed('path/to/save/file-signed.pdf')
# save the signed xml file
doc.save_xml('path/to/save/xml.xml')
```

## Development

### Install dependencies
Expand Down
19 changes: 19 additions & 0 deletions mifiel/document.py
@@ -1,6 +1,7 @@
from mifiel import Base
import mimetypes
from os.path import basename
import requests

class Document(Base):
def __init__(self, client):
Expand Down Expand Up @@ -41,3 +42,21 @@ def create(client, signatories, file=None, dhash=None, callback_url=None):
doc = Document(client)
doc.process_request('post', data=data, files=file)
return doc

def save_file(self, path):
url_ = self.url('{}/file').format(self.id)
response = requests.get(url_, auth=self.client.auth)
with open(path, 'w') as file_:
file_.write(response.text)

def save_file_signed(self, path):
url_ = self.url('{}/file_signed').format(self.id)
response = requests.get(url_, auth=self.client.auth)
with open(path, 'w') as file_:
file_.write(response.text)

def save_xml(self, path):
url_ = self.url('{}/xml').format(self.id)
response = requests.get(url_, auth=self.client.auth)
with open(path, 'w') as file_:
file_.write(response.text)
70 changes: 70 additions & 0 deletions test/mifiellib/test_document.py
Expand Up @@ -3,6 +3,7 @@

import json
import responses
import os.path

class TestDocument(BaseMifielCase):
def setUp(self):
Expand Down Expand Up @@ -109,3 +110,72 @@ def test_create_without_file_or_hash(self):
def test_create_with_file_and_hash(self):
with self.assertRaises(ValueError):
Document.create(self.client, [], dhash='dhash', file='file')

@responses.activate
def test_save_file(self):
doc_id = '8600153a-4845-4d11-aac6-1d3d6048e022'
url = self.client.url().format(path='documents')
url = '{}/{}/file'.format(url, doc_id)
responses.add(
method=responses.GET,
url=url,
body='some-pdf-contents',
status=200,
content_type='application/pdf',
)
doc = Document(self.client)
doc.id = doc_id
path = 'tmp/the-file.pdf'
doc.save_file(path)
req = self.get_last_request()
self.assertEqual(req.method, 'GET')
self.assertEqual(req.url, url)
self.assertEqual(doc.id, doc_id)
assert req.headers['Authorization'] is not None
self.assertTrue(os.path.isfile(path))

@responses.activate
def test_save_file_signed(self):
doc_id = '8600153a-4845-4d11-aac6-1d3d6048e022'
url = self.client.url().format(path='documents')
url = '{}/{}/file_signed'.format(url, doc_id)
responses.add(
method=responses.GET,
url=url,
body='some-pdf-contents',
status=200,
content_type='application/pdf',
)
doc = Document(self.client)
doc.id = doc_id
path = 'tmp/the-file-signed.pdf'
doc.save_file_signed(path)
req = self.get_last_request()
self.assertEqual(req.method, 'GET')
self.assertEqual(req.url, url)
self.assertEqual(doc.id, doc_id)
assert req.headers['Authorization'] is not None
self.assertTrue(os.path.isfile(path))

@responses.activate
def test_save_xml(self):
doc_id = '8600153a-4845-4d11-aac6-1d3d6048e022'
url = self.client.url().format(path='documents')
url = '{}/{}/xml'.format(url, doc_id)
responses.add(
method=responses.GET,
url=url,
body='<some><xml><contents /></xml></some>',
status=200,
content_type='application/xml',
)
doc = Document(self.client)
doc.id = doc_id
path = 'tmp/the-file.xml'
doc.save_xml(path)
req = self.get_last_request()
self.assertEqual(req.method, 'GET')
self.assertEqual(req.url, url)
self.assertEqual(doc.id, doc_id)
assert req.headers['Authorization'] is not None
self.assertTrue(os.path.isfile(path))

0 comments on commit 0e25c50

Please sign in to comment.