Skip to content

Commit e3aae80

Browse files
authored
Support version number in PDF fills (anvilco#40)
* Fix changelog typo * Support params in POST requests * Add `version_number` support in fill_pdf method * Add version-related constants, Update example * Update docs * Bump version * Update tests, fix from tests * Typo
1 parent 85552ad commit e3aae80

7 files changed

Lines changed: 76 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# 1.5.0 (2022-09-07)
1+
# 1.7.0 (2022-09-09)
2+
3+
- Added support for `version_number` in PDF Fill requests.
4+
5+
# 1.6.0 (2022-09-07)
26

37
- Added support for HTML/CSS and Markdown in `CreateEtchPacket`. [See examples here](https://www.useanvil.com/docs/api/e-signatures#generating-a-pdf-from-html-and-css).
48

docs/api_usage.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ Data to embed into the PDF. Supported `payload` types are:
3434
sure all required data is set.
3535
* `FillPDFPayload` - dataclass (see: [Data Types](#data-types))
3636

37+
**version_number: Optional[int]**
38+
39+
Version of the PDF template to use. By default, the request will use the latest published version.
40+
41+
You can also use the constants `Anvil.VERSION_LATEST_PUBLISHED` and `Anvil.VERSION_LATEST`
42+
instead of providing a specific version number.
43+
3744
Example:
3845

3946
```python
@@ -46,6 +53,14 @@ data = {
4653
"data": {"textField": "Some data"}
4754
}
4855
response = anvil.fill_pdf("some_template", data)
56+
57+
# A version number can also be passed in. This will retrieve a specific
58+
# version of the PDF to be filled if you don't want the current version
59+
# to be used.
60+
# You can also use the constant `Anvil.VERSION_LATEST` to fill a PDF that has not
61+
# been published yet. Use this if you'd like to fill out a draft version of
62+
# your template/PDF.
63+
response = anvil.fill_pdf("some_template", data, version_number=Anvil.VERSION_LATEST)
4964
```
5065

5166
### Anvil.generate_pdf

examples/fill_pdf.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ def main():
3434
# to a file.
3535
res = anvil.fill_pdf('abc123', data)
3636

37+
# A version number can also be passed in. This will retrieve a specific
38+
# version of the PDF to be filled if you don't want the current version
39+
# to be used.
40+
# You can also use the constant `Anvil.VERSION_LATEST` to fill a PDF that has not
41+
# been published yet. Use this if you'd like to fill out a draft version of
42+
# your template/PDF.
43+
#
44+
# res = anvil.fill_pdf('abc123', data, version_number=Anvil.VERSION_LATEST)
45+
3746
# Write the bytes to disk
3847
with open('./file.pdf', 'wb') as f:
3948
f.write(res)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[tool.poetry]
22

33
name = "python_anvil"
4-
version = "1.6.0"
4+
version = "1.7.0"
55
description = "Anvil API"
66

77
license = "MIT"

python_anvil/api.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ class Anvil:
3535
>> pdf_data = anvil.fill_pdf("the_template_id", payload)
3636
"""
3737

38+
# Version number to use for latest versions (usually drafts)
39+
VERSION_LATEST = -1
40+
# Version number to use for the latest published version.
41+
# This is the default when a version is not provided.
42+
VERSION_LATEST_PUBLISHED = -2
43+
3844
def __init__(self, api_key=None, environment='dev'):
3945
self.client = HTTPClient(api_key=api_key, environment=environment)
4046

@@ -63,10 +69,13 @@ def fill_pdf(
6369
Use the casts graphql query to get a list of available templates you
6470
can use for this request.
6571
66-
:param template_id: eid of an existing template/cast.
72+
:param template_id: eid of an existing template/cast
6773
:type template_id: str
6874
:param payload: payload in the form of a dict or JSON data
6975
:type payload: dict|str
76+
:param kwargs.version_number: specific template version number to use. If
77+
not provided, the latest _published_ version will be used.
78+
:type kwargs.version_number: int
7079
"""
7180
try:
7281
if isinstance(payload, dict):
@@ -86,6 +95,10 @@ def fill_pdf(
8695
"fields are set. "
8796
) from e
8897

98+
version_number = kwargs.pop("version_number", None)
99+
if version_number:
100+
kwargs["params"] = dict(versionNumber=version_number)
101+
89102
api = RestRequest(client=self.client)
90103
return api.post(
91104
f"fill/{template_id}.pdf",

python_anvil/api_resources/requests.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ def get(self, url, params=None, **kwargs):
6868

6969
def post(self, url, data=None, **kwargs):
7070
retry = kwargs.pop("retry", True)
71+
params = kwargs.pop("params", None)
7172
content, status_code, headers = self._request(
72-
"POST", url, json=data, retry=retry
73+
"POST", url, json=data, retry=retry, params=params
7374
)
7475
return self.process_response(content, status_code, headers, **kwargs)
7576

python_anvil/tests/test_api.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,36 @@ def test_with_kwargs(m_request_post, anvil):
7979
include_headers=True,
8080
)
8181

82+
@mock.patch('python_anvil.api.RestRequest.post')
83+
def test_with_version(m_request_post, anvil):
84+
payload = {"data": {"one": "One string"}}
85+
anvil.fill_pdf(
86+
"some_template",
87+
payload=payload,
88+
include_headers=True,
89+
version_number=Anvil.VERSION_LATEST,
90+
)
91+
m_request_post.assert_called_once_with(
92+
"fill/some_template.pdf",
93+
{"data": {"one": "One string"}},
94+
include_headers=True,
95+
params={"versionNumber": Anvil.VERSION_LATEST},
96+
)
97+
98+
@mock.patch('python_anvil.api.RestRequest.post')
99+
def test_with_params(m_request_post, anvil):
100+
payload = {"data": {"one": "One string"}}
101+
params = {"arbitrary": "Param"}
102+
anvil.fill_pdf(
103+
"some_template", payload=payload, include_headers=True, params=params
104+
)
105+
m_request_post.assert_called_once_with(
106+
"fill/some_template.pdf",
107+
{"data": {"one": "One string"}},
108+
include_headers=True,
109+
params=params,
110+
)
111+
82112
def describe_generate_pdf():
83113
@mock.patch('python_anvil.api.RestRequest.post')
84114
def test_dict_payload(m_request_post, anvil):

0 commit comments

Comments
 (0)