pingen2sdk 2.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pingen2sdk might be problematic. Click here for more details.

pingen2sdk/__init__.py ADDED
@@ -0,0 +1,28 @@
1
+ # flake8: noqa
2
+ from typing import Optional
3
+
4
+ import os
5
+
6
+ api_key: Optional[str] = None
7
+ client_id: Optional[str] = None
8
+ client_secret: Optional[str] = None
9
+ api_production: str = "https://api.pingen.com"
10
+ auth_production: str = "https://identity.pingen.com"
11
+ api_staging: str = "https://api-staging.pingen.com"
12
+ auth_staging: str = "https://identity-staging.pingen.com"
13
+ request_timeout: int = 20
14
+
15
+ # OAuth
16
+ from pingen2sdk.oauth import OAuth
17
+
18
+ # PingenResponse
19
+ from pingen2sdk.response import PingenResponse
20
+
21
+ # PingenError
22
+ from pingen2sdk.error import PingenError
23
+
24
+ # APIRequestor
25
+ from pingen2sdk.api import APIRequestor
26
+
27
+ # API Resources
28
+ from pingen2sdk.api_resources import *
pingen2sdk/api.py ADDED
@@ -0,0 +1,146 @@
1
+ import requests
2
+ import pingen2sdk
3
+
4
+ from io import IOBase, BytesIO
5
+ from typing import Any, Dict, Mapping, Optional, cast
6
+ from requests.models import PreparedRequest
7
+
8
+
9
+ def _response_interpreter(
10
+ body: Dict,
11
+ status_code: int,
12
+ headers: Mapping[str, str],
13
+ ) -> pingen2sdk.PingenResponse:
14
+ if 200 <= status_code < 310:
15
+ return pingen2sdk.PingenResponse(body, status_code, headers)
16
+ else:
17
+ raise pingen2sdk.PingenError(None, body, status_code, headers)
18
+
19
+
20
+ class APIRequestor(object):
21
+ access_token: str
22
+ api_base: str
23
+ user_agent: str = "PINGEN.SDK.PYTHON"
24
+
25
+ def __init__(self, access_token: str, use_staging: Optional[bool] = False):
26
+ if use_staging is False:
27
+ self.api_base = pingen2sdk.api_production
28
+ else:
29
+ self.api_base = pingen2sdk.api_staging
30
+
31
+ self.access_token = access_token
32
+
33
+ def perform_get_request(
34
+ self,
35
+ url: str,
36
+ params: Optional[Mapping[str, Any]] = None,
37
+ supplied_headers: Optional[Mapping[str, str]] = None,
38
+ ) -> pingen2sdk.PingenResponse:
39
+ r = requests.get(
40
+ self.prepare_path(url, params),
41
+ headers=self.request_headers(supplied_headers),
42
+ timeout=pingen2sdk.request_timeout,
43
+ )
44
+
45
+ return _response_interpreter(r.text, r.status_code, r.headers)
46
+
47
+ @staticmethod
48
+ def perform_put_request(
49
+ url: str,
50
+ file: BytesIO,
51
+ ):
52
+ requests.put(url, data=file)
53
+
54
+ def perform_post_request(
55
+ self,
56
+ url: str,
57
+ payload: str,
58
+ supplied_headers: Optional[Mapping[str, str]] = None,
59
+ ) -> pingen2sdk.PingenResponse:
60
+ r = requests.post(
61
+ self.prepare_path(url),
62
+ payload,
63
+ headers=self.request_headers(supplied_headers),
64
+ timeout=pingen2sdk.request_timeout,
65
+ )
66
+
67
+ return _response_interpreter(r.text, r.status_code, r.headers)
68
+
69
+ def perform_patch_request(
70
+ self,
71
+ url: str,
72
+ payload: str,
73
+ supplied_headers: Optional[Mapping[str, str]] = None,
74
+ ) -> pingen2sdk.PingenResponse:
75
+ r = requests.patch(
76
+ self.prepare_path(url),
77
+ payload,
78
+ headers=self.request_headers(supplied_headers),
79
+ timeout=pingen2sdk.request_timeout,
80
+ )
81
+
82
+ return _response_interpreter(r.text, r.status_code, r.headers)
83
+
84
+ def perform_cancel_request(self, url: str) -> pingen2sdk.PingenResponse:
85
+ r = requests.patch(
86
+ self.prepare_path(url),
87
+ headers=self.request_headers(),
88
+ timeout=pingen2sdk.request_timeout,
89
+ )
90
+
91
+ return _response_interpreter(r.text, r.status_code, r.headers)
92
+
93
+ def perform_delete_request(self, url: str) -> pingen2sdk.PingenResponse:
94
+ r = requests.delete(
95
+ self.prepare_path(url),
96
+ headers=self.request_headers(),
97
+ timeout=pingen2sdk.request_timeout,
98
+ )
99
+
100
+ return _response_interpreter(r.text, r.status_code, r.headers)
101
+
102
+ def perform_stream_request(
103
+ self,
104
+ url: str,
105
+ ) -> IOBase:
106
+ response = requests.get(
107
+ self.prepare_path(url),
108
+ headers=self.request_headers(),
109
+ timeout=pingen2sdk.request_timeout,
110
+ stream=True,
111
+ )
112
+
113
+ return cast(IOBase, response.text)
114
+
115
+ def prepare_path(
116
+ self,
117
+ url: str,
118
+ params: Optional[Mapping[str, Any]] = None,
119
+ ) -> str:
120
+ path = "%s%s" % (self.api_base, url)
121
+
122
+ request = PreparedRequest()
123
+ request.prepare_url(path, params)
124
+
125
+ return request.url
126
+
127
+ def request_headers(
128
+ self,
129
+ supplied_headers: Optional[Mapping[str, str]] = None,
130
+ ) -> Mapping[str, str]:
131
+ supplied_headers_dict: Optional[Dict[str, str]] = (
132
+ dict(supplied_headers) if supplied_headers is not None else None
133
+ )
134
+
135
+ headers = {
136
+ "User-Agent": self.user_agent,
137
+ "Authorization": "Bearer {}".format(self.access_token),
138
+ "Content-Type": "application/vnd.api+json",
139
+ "Accept": "application/vnd.api+json",
140
+ }
141
+
142
+ if supplied_headers_dict is not None:
143
+ for key, value in supplied_headers_dict.items():
144
+ headers[key] = value
145
+
146
+ return headers
@@ -0,0 +1,11 @@
1
+ # flake8: noqa
2
+ from pingen2sdk.api_resources.letters import Letters as Letters
3
+ from pingen2sdk.api_resources.file_upload import FileUpload as FileUpload
4
+ from pingen2sdk.api_resources.letter_events import LetterEvents as LetterEvents
5
+ from pingen2sdk.api_resources.users import Users as Users
6
+ from pingen2sdk.api_resources.user_associations import (
7
+ UserAssociations as UserAssociations,
8
+ )
9
+ from pingen2sdk.api_resources.organisations import Organisations as Organisations
10
+ from pingen2sdk.api_resources.batches import Batches as Batches
11
+ from pingen2sdk.api_resources.batch_events import BatchEvents as BatchEvents
@@ -0,0 +1,29 @@
1
+ import pingen2sdk
2
+
3
+ from typing import Any, Mapping, Optional
4
+
5
+
6
+ class BatchEvents(object):
7
+ organisation_id: str
8
+ api_requestor: pingen2sdk.APIRequestor
9
+
10
+ def __init__(
11
+ self,
12
+ organisation_id: str,
13
+ access_token: str,
14
+ use_staging: bool = False,
15
+ ):
16
+ self.organisation_id = organisation_id
17
+ self.api_requestor = pingen2sdk.APIRequestor(access_token, use_staging)
18
+
19
+ def get_collection(
20
+ self,
21
+ batch_id: str,
22
+ params: Optional[Mapping[str, Any]] = None,
23
+ supplied_headers: Optional[Mapping[str, str]] = None,
24
+ ) -> pingen2sdk.PingenResponse:
25
+ return self.api_requestor.perform_get_request(
26
+ "/organisations/%s/batches/%s/events" % (self.organisation_id, batch_id),
27
+ params,
28
+ supplied_headers,
29
+ )
@@ -0,0 +1,219 @@
1
+ import pingen2sdk
2
+ import json
3
+
4
+ from typing import Any, Mapping, Optional, List
5
+ from typing_extensions import Literal
6
+
7
+
8
+ class Batches(object):
9
+ def __init__(
10
+ self,
11
+ organisation_id: str,
12
+ access_token: str,
13
+ use_staging: bool = False,
14
+ ):
15
+ self.organisation_id = organisation_id
16
+ self.api_requestor = pingen2sdk.APIRequestor(access_token, use_staging)
17
+
18
+ def get_details(
19
+ self,
20
+ batch_id: str,
21
+ params: Optional[Mapping[str, Any]] = None,
22
+ supplied_headers: Optional[Mapping[str, str]] = None,
23
+ ) -> pingen2sdk.PingenResponse:
24
+ return self.api_requestor.perform_get_request(
25
+ "/organisations/%s/batches/%s" % (self.organisation_id, batch_id),
26
+ params,
27
+ supplied_headers,
28
+ )
29
+
30
+ def get_collection(
31
+ self,
32
+ params: Optional[Mapping[str, Any]] = None,
33
+ supplied_headers: Optional[Mapping[str, str]] = None,
34
+ ) -> pingen2sdk.PingenResponse:
35
+ return self.api_requestor.perform_get_request(
36
+ "/organisations/%s/batches" % self.organisation_id,
37
+ params,
38
+ supplied_headers,
39
+ )
40
+
41
+ def upload_and_create_batch(
42
+ self,
43
+ path_to_file: str,
44
+ name: str,
45
+ icon: Literal[
46
+ "campaign",
47
+ "megaphone",
48
+ "wave-hand",
49
+ "flash",
50
+ "rocket",
51
+ "bell",
52
+ "percent-tag",
53
+ "percent-badge",
54
+ "present",
55
+ "receipt",
56
+ "document",
57
+ "information",
58
+ "calendar",
59
+ "newspaper",
60
+ "crown",
61
+ "virus",
62
+ ],
63
+ file_original_name: str,
64
+ address_position: Literal["left", "right"],
65
+ grouping_type: Literal["zip", "merge"],
66
+ grouping_options_split_type: Literal["file", "page", "custom", "qr_invoice"],
67
+ grouping_options_split_size: Optional[int] = None,
68
+ grouping_options_split_separator: Optional[str] = None,
69
+ grouping_options_split_position: Optional[
70
+ Literal["first_page", "last_page"]
71
+ ] = None,
72
+ ) -> pingen2sdk.PingenResponse:
73
+ file_upload = pingen2sdk.FileUpload(self.api_requestor)
74
+ file_url, file_url_signature = file_upload.request_file_upload()
75
+ file_upload.put_file(path_to_file, file_url)
76
+
77
+ return self.create_batch(
78
+ file_url,
79
+ file_url_signature,
80
+ name,
81
+ icon,
82
+ file_original_name,
83
+ address_position,
84
+ grouping_type,
85
+ grouping_options_split_type,
86
+ grouping_options_split_size,
87
+ grouping_options_split_separator,
88
+ grouping_options_split_position,
89
+ )
90
+
91
+ def create_batch(
92
+ self,
93
+ file_url: str,
94
+ file_url_signature: str,
95
+ name: str,
96
+ icon: Literal[
97
+ "campaign",
98
+ "megaphone",
99
+ "wave-hand",
100
+ "flash",
101
+ "rocket",
102
+ "bell",
103
+ "percent-tag",
104
+ "percent-badge",
105
+ "present",
106
+ "receipt",
107
+ "document",
108
+ "information",
109
+ "calendar",
110
+ "newspaper",
111
+ "crown",
112
+ "virus",
113
+ ],
114
+ file_original_name: str,
115
+ address_position: Literal["left", "right"],
116
+ grouping_type: Literal["zip", "merge"],
117
+ grouping_options_split_type: Literal["file", "page", "custom", "qr_invoice"],
118
+ grouping_options_split_size: Optional[int] = None,
119
+ grouping_options_split_separator: Optional[str] = None,
120
+ grouping_options_split_position: Optional[
121
+ Literal["first_page", "last_page"]
122
+ ] = None,
123
+ ) -> pingen2sdk.PingenResponse:
124
+ attributes = {
125
+ "file_url": file_url,
126
+ "file_url_signature": file_url_signature,
127
+ "name": name,
128
+ "icon": icon,
129
+ "file_original_name": file_original_name,
130
+ "address_position": address_position,
131
+ "grouping_type": grouping_type,
132
+ "grouping_options_split_type": grouping_options_split_type,
133
+ }
134
+
135
+ if grouping_options_split_size is not None:
136
+ attributes["grouping_options_split_size"] = grouping_options_split_size
137
+
138
+ if grouping_options_split_separator is not None:
139
+ attributes[
140
+ "grouping_options_split_separator"
141
+ ] = grouping_options_split_separator
142
+
143
+ if grouping_options_split_position is not None:
144
+ attributes[
145
+ "grouping_options_split_position"
146
+ ] = grouping_options_split_position
147
+
148
+ return self.api_requestor.perform_post_request(
149
+ "/organisations/%s/batches" % self.organisation_id,
150
+ json.dumps({"data": {"type": "batches", "attributes": attributes}}),
151
+ )
152
+
153
+ def send_batch(
154
+ self,
155
+ batch_id: str,
156
+ delivery_product: Mapping[str, str],
157
+ print_mode: str,
158
+ print_spectrum: str,
159
+ ) -> pingen2sdk.PingenResponse:
160
+ return self.api_requestor.perform_patch_request(
161
+ "/organisations/%s/batches/%s/send" % (self.organisation_id, batch_id),
162
+ json.dumps(
163
+ {
164
+ "data": {
165
+ "id": batch_id,
166
+ "type": "batches",
167
+ "attributes": {
168
+ "delivery_products": delivery_product,
169
+ "print_mode": print_mode,
170
+ "print_spectrum": print_spectrum,
171
+ },
172
+ }
173
+ }
174
+ ),
175
+ )
176
+
177
+ def cancel_batch(
178
+ self,
179
+ batch_id: str,
180
+ ) -> pingen2sdk.PingenResponse:
181
+ return self.api_requestor.perform_cancel_request(
182
+ "/organisations/%s/batches/%s/cancel" % (self.organisation_id, batch_id),
183
+ )
184
+
185
+ def delete_batch(
186
+ self,
187
+ batch_id: str,
188
+ ) -> pingen2sdk.PingenResponse:
189
+ return self.api_requestor.perform_delete_request(
190
+ "/organisations/%s/batches/%s" % (self.organisation_id, batch_id),
191
+ )
192
+
193
+ def edit_batch(
194
+ self,
195
+ batch_id: str,
196
+ paper_types: List[str],
197
+ ) -> pingen2sdk.PingenResponse:
198
+ return self.api_requestor.perform_patch_request(
199
+ "/organisations/%s/batches/%s" % (self.organisation_id, batch_id),
200
+ json.dumps(
201
+ {
202
+ "data": {
203
+ "id": batch_id,
204
+ "type": "batches",
205
+ "attributes": {
206
+ "paper_types": paper_types,
207
+ },
208
+ }
209
+ }
210
+ ),
211
+ )
212
+
213
+ def get_statistics(
214
+ self,
215
+ batch_id: str,
216
+ ) -> pingen2sdk.PingenResponse:
217
+ return self.api_requestor.perform_get_request(
218
+ "/organisations/%s/batches/%s/statistics" % (self.organisation_id, batch_id)
219
+ )
@@ -0,0 +1,31 @@
1
+ import pingen2sdk
2
+ from typing import Tuple
3
+
4
+
5
+ class FileUpload(object):
6
+ def __init__(
7
+ self,
8
+ api_requestor: pingen2sdk.APIRequestor,
9
+ ):
10
+ self.api_requestor = api_requestor
11
+
12
+ def request_file_upload(self) -> Tuple[str, str]:
13
+ response = self.api_requestor.perform_get_request(
14
+ "/file-upload",
15
+ )
16
+
17
+ return (
18
+ response.data["data"]["attributes"]["url"],
19
+ response.data["data"]["attributes"]["url_signature"],
20
+ )
21
+
22
+ def put_file(
23
+ self,
24
+ path_to_file: str,
25
+ file_url: str,
26
+ ):
27
+ file = open(path_to_file, "rb")
28
+
29
+ self.api_requestor.perform_put_request(file_url, file)
30
+
31
+ file.close()
@@ -0,0 +1,40 @@
1
+ import pingen2sdk
2
+
3
+ from typing import Any, Mapping, Optional
4
+
5
+
6
+ class LetterEvents(object):
7
+ organisation_id: str
8
+ api_requestor: pingen2sdk.APIRequestor
9
+
10
+ def __init__(
11
+ self,
12
+ organisation_id: str,
13
+ access_token: str,
14
+ use_staging: bool = False,
15
+ ):
16
+ self.organisation_id = organisation_id
17
+ self.api_requestor = pingen2sdk.APIRequestor(access_token, use_staging)
18
+
19
+ def get_collection(
20
+ self,
21
+ letter_id: str,
22
+ params: Optional[Mapping[str, Any]] = None,
23
+ supplied_headers: Optional[Mapping[str, str]] = None,
24
+ ) -> pingen2sdk.PingenResponse:
25
+ return self.api_requestor.perform_get_request(
26
+ "/organisations/%s/letters/%s/events" % (self.organisation_id, letter_id),
27
+ params,
28
+ supplied_headers,
29
+ )
30
+
31
+ def get_issue_collection(
32
+ self,
33
+ params: Optional[Mapping[str, Any]] = None,
34
+ supplied_headers: Optional[Mapping[str, str]] = None,
35
+ ) -> pingen2sdk.PingenResponse:
36
+ return self.api_requestor.perform_get_request(
37
+ "/organisations/%s/letters/issues" % self.organisation_id,
38
+ params,
39
+ supplied_headers,
40
+ )
@@ -0,0 +1,204 @@
1
+ import pingen2sdk
2
+ import json
3
+
4
+ from io import IOBase
5
+ from typing import Any, Mapping, Optional, Dict, List
6
+ from typing_extensions import Literal
7
+
8
+
9
+ class Letters(object):
10
+ def __init__(
11
+ self,
12
+ organisation_id: str,
13
+ access_token: str,
14
+ use_staging: bool = False,
15
+ ):
16
+ self.organisation_id = organisation_id
17
+ self.api_requestor = pingen2sdk.APIRequestor(access_token, use_staging)
18
+
19
+ def get_details(
20
+ self,
21
+ letter_id: str,
22
+ params: Optional[Mapping[str, Any]] = None,
23
+ supplied_headers: Optional[Mapping[str, str]] = None,
24
+ ) -> pingen2sdk.PingenResponse:
25
+ return self.api_requestor.perform_get_request(
26
+ "/organisations/%s/letters/%s" % (self.organisation_id, letter_id),
27
+ params,
28
+ supplied_headers,
29
+ )
30
+
31
+ def get_collection(
32
+ self,
33
+ params: Optional[Mapping[str, Any]] = None,
34
+ supplied_headers: Optional[Mapping[str, str]] = None,
35
+ ) -> pingen2sdk.PingenResponse:
36
+ return self.api_requestor.perform_get_request(
37
+ "/organisations/%s/letters" % self.organisation_id,
38
+ params,
39
+ supplied_headers,
40
+ )
41
+
42
+ def upload_and_create_letter(
43
+ self,
44
+ path_to_file: str,
45
+ file_original_name: str,
46
+ address_position: Literal["left", "right"],
47
+ auto_send: False,
48
+ delivery_product: Optional[str] = None,
49
+ print_mode: Optional[str] = None,
50
+ print_spectrum: Optional[str] = None,
51
+ sender_address: Optional[str] = None,
52
+ meta_data: Optional[Dict] = None,
53
+ ) -> pingen2sdk.PingenResponse:
54
+ file_upload = pingen2sdk.FileUpload(self.api_requestor)
55
+ file_url, file_signature = file_upload.request_file_upload()
56
+ file_upload.put_file(path_to_file, file_url)
57
+
58
+ return self.create_letter(
59
+ file_url,
60
+ file_signature,
61
+ file_original_name,
62
+ address_position,
63
+ auto_send,
64
+ delivery_product,
65
+ print_mode,
66
+ print_spectrum,
67
+ sender_address,
68
+ meta_data,
69
+ )
70
+
71
+ def create_letter(
72
+ self,
73
+ file_url: str,
74
+ file_signature: str,
75
+ file_original_name: str,
76
+ address_position: Literal["left", "right"],
77
+ auto_send: False,
78
+ delivery_product: Optional[str] = None,
79
+ print_mode: Optional[str] = None,
80
+ print_spectrum: Optional[str] = None,
81
+ sender_address: Optional[str] = None,
82
+ meta_data: Optional[Dict] = None,
83
+ ) -> pingen2sdk.PingenResponse:
84
+ attributes = {
85
+ "file_original_name": file_original_name,
86
+ "file_url": file_url,
87
+ "file_url_signature": file_signature,
88
+ "address_position": address_position,
89
+ "auto_send": auto_send,
90
+ }
91
+
92
+ if delivery_product is not None:
93
+ attributes["delivery_product"] = delivery_product
94
+
95
+ if print_mode is not None:
96
+ attributes["print_mode"] = print_mode
97
+
98
+ if print_spectrum is not None:
99
+ attributes["print_spectrum"] = print_spectrum
100
+
101
+ if sender_address is not None:
102
+ attributes["sender_address"] = sender_address
103
+
104
+ if meta_data is not None:
105
+ attributes["meta_data"] = meta_data
106
+
107
+ return self.api_requestor.perform_post_request(
108
+ "/organisations/%s/letters" % self.organisation_id,
109
+ json.dumps({"data": {"type": "letters", "attributes": attributes}}),
110
+ )
111
+
112
+ def send_letter(
113
+ self,
114
+ letter_id: str,
115
+ delivery_product: str,
116
+ print_mode: str,
117
+ print_spectrum: str,
118
+ ) -> pingen2sdk.PingenResponse:
119
+ return self.api_requestor.perform_patch_request(
120
+ "/organisations/%s/letters/%s/send" % (self.organisation_id, letter_id),
121
+ json.dumps(
122
+ {
123
+ "data": {
124
+ "id": letter_id,
125
+ "type": "letters",
126
+ "attributes": {
127
+ "delivery_product": delivery_product,
128
+ "print_mode": print_mode,
129
+ "print_spectrum": print_spectrum,
130
+ },
131
+ }
132
+ }
133
+ ),
134
+ )
135
+
136
+ def cancel_letter(
137
+ self,
138
+ letter_id: str,
139
+ ) -> pingen2sdk.PingenResponse:
140
+ return self.api_requestor.perform_cancel_request(
141
+ "/organisations/%s/letters/%s/cancel" % (self.organisation_id, letter_id),
142
+ )
143
+
144
+ def delete_letter(
145
+ self,
146
+ letter_id: str,
147
+ ) -> pingen2sdk.PingenResponse:
148
+ return self.api_requestor.perform_delete_request(
149
+ "/organisations/%s/letters/%s" % (self.organisation_id, letter_id),
150
+ )
151
+
152
+ def edit_letter(
153
+ self,
154
+ letter_id: str,
155
+ paper_types: List[str],
156
+ ) -> pingen2sdk.PingenResponse:
157
+ return self.api_requestor.perform_patch_request(
158
+ "/organisations/%s/letters/%s" % (self.organisation_id, letter_id),
159
+ json.dumps(
160
+ {
161
+ "data": {
162
+ "id": letter_id,
163
+ "type": "letters",
164
+ "attributes": {
165
+ "paper_types": paper_types,
166
+ },
167
+ }
168
+ }
169
+ ),
170
+ )
171
+
172
+ def get_letter_file(
173
+ self,
174
+ letter_id: str,
175
+ ) -> IOBase:
176
+ return self.api_requestor.perform_stream_request(
177
+ "/organisations/%s/letters/%s/file" % (self.organisation_id, letter_id),
178
+ )
179
+
180
+ def calculate_price(
181
+ self,
182
+ country: str,
183
+ paper_types: List[str],
184
+ print_mode: str,
185
+ print_spectrum: str,
186
+ delivery_product: str,
187
+ ) -> pingen2sdk.PingenResponse:
188
+ return self.api_requestor.perform_post_request(
189
+ "/organisations/%s/letters/price-calculator" % self.organisation_id,
190
+ json.dumps(
191
+ {
192
+ "data": {
193
+ "type": "letter_price_calculator",
194
+ "attributes": {
195
+ "country": country,
196
+ "paper_types": paper_types,
197
+ "print_mode": print_mode,
198
+ "print_spectrum": print_spectrum,
199
+ "delivery_product": delivery_product,
200
+ },
201
+ }
202
+ }
203
+ ),
204
+ )
@@ -0,0 +1,37 @@
1
+ import pingen2sdk
2
+
3
+ from typing import Any, Mapping, Optional
4
+
5
+
6
+ class Organisations(object):
7
+ api_requestor: pingen2sdk.APIRequestor
8
+
9
+ def __init__(
10
+ self,
11
+ access_token: str,
12
+ use_staging: bool = False,
13
+ ):
14
+ self.api_requestor = pingen2sdk.APIRequestor(access_token, use_staging)
15
+
16
+ def get_details(
17
+ self,
18
+ organisation_id: str,
19
+ params: Optional[Mapping[str, Any]] = None,
20
+ supplied_headers: Optional[Mapping[str, str]] = None,
21
+ ) -> pingen2sdk.PingenResponse:
22
+ return self.api_requestor.perform_get_request(
23
+ "/organisations/%s" % organisation_id,
24
+ params,
25
+ supplied_headers,
26
+ )
27
+
28
+ def get_collection(
29
+ self,
30
+ params: Optional[Mapping[str, Any]] = None,
31
+ supplied_headers: Optional[Mapping[str, str]] = None,
32
+ ) -> pingen2sdk.PingenResponse:
33
+ return self.api_requestor.perform_get_request(
34
+ "/organisations",
35
+ params,
36
+ supplied_headers,
37
+ )
@@ -0,0 +1,25 @@
1
+ import pingen2sdk
2
+
3
+ from typing import Any, Mapping, Optional
4
+
5
+
6
+ class UserAssociations(object):
7
+ api_requestor: pingen2sdk.APIRequestor
8
+
9
+ def __init__(
10
+ self,
11
+ access_token: str,
12
+ use_staging: bool = False,
13
+ ):
14
+ self.api_requestor = pingen2sdk.APIRequestor(access_token, use_staging)
15
+
16
+ def get_collection(
17
+ self,
18
+ params: Optional[Mapping[str, Any]] = None,
19
+ supplied_headers: Optional[Mapping[str, str]] = None,
20
+ ) -> pingen2sdk.PingenResponse:
21
+ return self.api_requestor.perform_get_request(
22
+ "/user/associations",
23
+ params,
24
+ supplied_headers,
25
+ )
@@ -0,0 +1,25 @@
1
+ import pingen2sdk
2
+
3
+ from typing import Any, Mapping, Optional
4
+
5
+
6
+ class Users(object):
7
+ api_requestor: pingen2sdk.APIRequestor
8
+
9
+ def __init__(
10
+ self,
11
+ access_token: str,
12
+ use_staging: bool = False,
13
+ ):
14
+ self.api_requestor = pingen2sdk.APIRequestor(access_token, use_staging)
15
+
16
+ def get_details(
17
+ self,
18
+ params: Optional[Mapping[str, Any]] = None,
19
+ supplied_headers: Optional[Mapping[str, str]] = None,
20
+ ) -> pingen2sdk.PingenResponse:
21
+ return self.api_requestor.perform_get_request(
22
+ "/user",
23
+ params,
24
+ supplied_headers,
25
+ )
pingen2sdk/error.py ADDED
@@ -0,0 +1,33 @@
1
+ import json
2
+
3
+ from typing import Dict, Optional
4
+
5
+
6
+ class PingenError(Exception):
7
+ _message: Optional[str]
8
+ json_body: Optional[object]
9
+ status_code: Optional[int]
10
+ headers: Optional[Dict[str, str]]
11
+ request_id: Optional[str]
12
+
13
+ def __init__(
14
+ self,
15
+ message: Optional[str] = None,
16
+ body: Optional[str] = None,
17
+ status_code: Optional[int] = None,
18
+ headers: Optional[Dict[str, str]] = None,
19
+ ):
20
+ super(PingenError, self).__init__(message)
21
+
22
+ self._message = message
23
+
24
+ if bool(body):
25
+ self.json_body = json.loads(body)
26
+
27
+ self.headers = headers or {}
28
+ self.status_code = status_code
29
+ self.request_id = self.headers.get("x-request-id", None)
30
+
31
+
32
+ class AuthenticationError(PingenError):
33
+ pass
pingen2sdk/oauth.py ADDED
@@ -0,0 +1,82 @@
1
+ import json
2
+ import requests
3
+ import pingen2sdk
4
+
5
+ from pingen2sdk import error
6
+ from requests.models import PreparedRequest
7
+ from typing import Dict
8
+
9
+
10
+ class OAuth(object):
11
+ @staticmethod
12
+ def _set_client_id(params):
13
+ if "client_id" in params:
14
+ return
15
+
16
+ from pingen2sdk import client_id
17
+
18
+ if client_id:
19
+ params["client_id"] = client_id
20
+ return
21
+
22
+ raise error.AuthenticationError(
23
+ 'No client_id provided. (HINT: set your client_id using "pingen2sdk.client_id = <CLIENT-ID>").'
24
+ )
25
+
26
+ @staticmethod
27
+ def _set_client_secret(params):
28
+ if "client_secret" in params:
29
+ return
30
+
31
+ from pingen2sdk import client_secret
32
+
33
+ if client_secret:
34
+ params["client_secret"] = client_secret
35
+ return
36
+
37
+ raise error.AuthenticationError(
38
+ 'No client_secret provided. (HINT: set your client_secret using "pingen2sdk.client_secret = <CLIENT-SECRET>").'
39
+ )
40
+
41
+ @staticmethod
42
+ def authorize_url(use_staging=False, **params) -> str:
43
+ if use_staging is False:
44
+ path = pingen2sdk.auth_production
45
+ else:
46
+ path = pingen2sdk.auth_staging
47
+
48
+ OAuth._set_client_id(params)
49
+ if "response_type" not in params:
50
+ params["response_type"] = "code"
51
+
52
+ request = PreparedRequest()
53
+ request.prepare_url(path, params)
54
+
55
+ return request.url
56
+
57
+ @staticmethod
58
+ def get_token(use_staging=False, **params) -> Dict:
59
+ if use_staging is False:
60
+ url = pingen2sdk.api_production
61
+ else:
62
+ url = pingen2sdk.api_staging
63
+
64
+ OAuth._set_client_id(params)
65
+ OAuth._set_client_secret(params)
66
+
67
+ response = requests.post(
68
+ url + "/auth/access-tokens",
69
+ params,
70
+ {"Content-Type: application/x-www-form-urlencoded"},
71
+ )
72
+
73
+ return json.loads(response.text)
74
+
75
+ @staticmethod
76
+ def get_token_from_implicit(fragment: str) -> Dict:
77
+ params = dict(x.split("=") for x in fragment.split("&"))
78
+
79
+ return {
80
+ "access_token": params["access_token"],
81
+ "expires_in": params["expires_in"],
82
+ }
pingen2sdk/response.py ADDED
@@ -0,0 +1,23 @@
1
+ import json
2
+
3
+ from typing import Mapping, Optional
4
+
5
+
6
+ class PingenResponse(object):
7
+ body: str
8
+ status_code: int
9
+ headers: Mapping[str, str]
10
+ data: Optional[object] = None
11
+
12
+ def __init__(self, body: str, status_code: int, headers: Mapping[str, str]):
13
+ self.body = body
14
+
15
+ if bool(body):
16
+ self.data = json.loads(body)
17
+
18
+ self.status_code = status_code
19
+ self.headers = headers
20
+
21
+ @property
22
+ def request_id(self) -> Optional[str]:
23
+ return self.headers.get("x-request-id", None)
@@ -0,0 +1,24 @@
1
+ Copyright (c) 2023, Pingen GmbH
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+ * Redistributions of source code must retain the above copyright
7
+ notice, this list of conditions and the following disclaimer.
8
+ * Redistributions in binary form must reproduce the above copyright
9
+ notice, this list of conditions and the following disclaimer in the
10
+ documentation and/or other materials provided with the distribution.
11
+ * Neither the name of the <organization> nor the
12
+ names of its contributors may be used to endorse or promote products
13
+ derived from this software without specific prior written permission.
14
+
15
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
19
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,124 @@
1
+ Metadata-Version: 2.1
2
+ Name: pingen2sdk
3
+ Version: 2.0.1
4
+ Summary: Official Python Pingen SDK for API V2
5
+ Author-email: Pingen GmbH <info@pingen.com>
6
+ License: BSD-3-Clause
7
+ Project-URL: Homepage, https://github.com/pingencom/pingen2-sdk-python
8
+ Project-URL: Bug Tracker, https://github.com/pingencom/pingen2-sdk-python/issues
9
+ Classifier: Development Status :: 5 - Production/Stable
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: Natural Language :: English
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Programming Language :: Python
14
+ Classifier: Programming Language :: Python :: 3.8
15
+ Classifier: Programming Language :: Python :: 3.9
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
20
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
21
+ Requires-Python: >=3.8
22
+ Description-Content-Type: text/markdown
23
+ License-File: LICENSE
24
+ Requires-Dist: requests >=2.31.0
25
+
26
+ # Requirements
27
+
28
+ You need to have an account in pingen and obtain oauth credentials for your desired grant type (usually client_credentials).
29
+
30
+ How to obtain these are described here: https://api.pingen.com/documentation#section/Authentication/How-to-obtain-a-Client-ID
31
+
32
+ Python 3.8+
33
+
34
+ # Installation
35
+
36
+ You don't need this source code unless you want to modify the package. If you just want to use the package, just run:
37
+
38
+ ```sh
39
+ pip install --upgrade pingen
40
+ ```
41
+
42
+ # Environments
43
+
44
+ We have two Environments available: Production and Staging (see https://api.pingen.com/documentation#section/Basics/Environments)
45
+
46
+ This SDK supports staging as well. **When initiating the resource** (see Usage), the optional 'staging' attribute should be set, **as well as when creating an endpoint object**.
47
+
48
+ # Usage
49
+
50
+ The simplest way to integrate is using the client credentials grant (see https://api.pingen.com/documentation#section/Authentication/Which-grant-type-should-i-use)
51
+
52
+ ```python
53
+ import pingen2sdk
54
+
55
+ pingen2sdk.client_id = "YOUR_CLIENT_ID"
56
+ pingen2sdk.client_secret = "YOU_SECRET"
57
+
58
+ resp = pingen2sdk.OAuth.get_token(
59
+ use_staging=True,
60
+ grant_type="client_credentials",
61
+ scope={"letter", "batch", "webhook", "organisation_read"},
62
+ )
63
+
64
+ access_token = resp["access_token"]
65
+
66
+ organisationList = pingen2sdk.Organisations(access_token, True).get_collection()
67
+
68
+ organisation_id = organisationList.data["data"][0]["id"]
69
+
70
+ LettersEndpoint = pingen2sdk.Letters(organisation_id, access_token, True)
71
+
72
+ response = LettersEndpoint.upload_and_create_letter(
73
+ "./letter.pdf",
74
+ "sdk.pdf",
75
+ "left",
76
+ False,
77
+ "fast",
78
+ "simplex",
79
+ "color",
80
+ )
81
+
82
+ letter_id = response.data["data"]["id"]
83
+ ```
84
+
85
+ # Examples & Docs
86
+
87
+ Our API Docs are here: https://api.pingen.com/documentation
88
+
89
+ On the right-hand side of every endpoint you can see request samples for Python and other languages, which you can copy and paste into your application.
90
+
91
+ # Bugreport & Contribution
92
+
93
+ If you find a bug, please either create a ticket in github, or initiate a pull request.
94
+
95
+ # Versioning
96
+
97
+ We adhere to semantic (major.minor.patch) versioning (https://semver.org/). This means that:
98
+ * Patch (x.x.patch) versions fix bugs
99
+ * Minor (x.minor.x) versions introduce new, backwards compatible features or improve existing code.
100
+ * Major (major.x.x) versions introduce radical changes which are not backwards compatible.
101
+
102
+ In your automation or procedure you can always safely update patch & minor versions without the risk of your application failing.
103
+
104
+ # Testing
105
+
106
+ Run all tests for a specific Python version (modify `-e` according to your Python target):
107
+
108
+ ```sh
109
+ tox -e py312
110
+ ```
111
+
112
+ Run the linter with:
113
+
114
+ ```sh
115
+ tox -e lint
116
+ ```
117
+
118
+ The library uses Black for code formatting. Code must be formatted
119
+ with Black before PRs are submitted, otherwise CI will fail. Run the formatter
120
+ with:
121
+
122
+ ```sh
123
+ tox -e fmt
124
+ ```
@@ -0,0 +1,19 @@
1
+ pingen2sdk/__init__.py,sha256=b5TQD_YmYgVMW1WLeFvl-FKxuTJ9dTK9mLKd5OFHGEw,666
2
+ pingen2sdk/api.py,sha256=r_gOzWWJ-dE_vBLj_dtSAE2yU8MieR5Wz25KHrUN3JA,4308
3
+ pingen2sdk/error.py,sha256=xKSicKN4_Vfdtnw1BuoMZTQmRH0CDjxUTrasVaG9I2A,793
4
+ pingen2sdk/oauth.py,sha256=sA9gVlxa0PiaR-Y9wXhw5AneEeBRMa9rwJ1lnR8qUSQ,2203
5
+ pingen2sdk/response.py,sha256=9WTRH8L0tpuECpxyeyZZpNlT4bHDmegSIbTgr_tzjfI,537
6
+ pingen2sdk/api_resources/__init__.py,sha256=6I2TCueuIJl---BfkfrgETG-h7r6yZ5xba94t0jlCzQ,615
7
+ pingen2sdk/api_resources/batch_events.py,sha256=Qjg1twwq-Q-Km6V0CKAlOuhGzfk1mF_J4vyOlGejfng,830
8
+ pingen2sdk/api_resources/batches.py,sha256=sqc7gxSC_kryrKop-hV7BYmrATzskSN7sONbgZR4Szk,7039
9
+ pingen2sdk/api_resources/file_upload.py,sha256=nH5WuDYCY59KukTfOib_XsEHvDYiVkKsMMgYilSMDVs,719
10
+ pingen2sdk/api_resources/letter_events.py,sha256=U1sigTDKC9hSUTSL51o2pgMC_rDjn6ItnDvzL5Bf4CU,1214
11
+ pingen2sdk/api_resources/letters.py,sha256=YUK25f4RftEG5eyyfYPrXi9KcQwI72SH2J5PNIDeYgk,6583
12
+ pingen2sdk/api_resources/organisations.py,sha256=BpI7nl4cY3TKXVaLzzMMZoB--Lh3bslNb0htJqD4pYc,1033
13
+ pingen2sdk/api_resources/user_associations.py,sha256=-nspS9hCFuluwHRH0K-LPcYu_Goi4E6KwJ8fKDRNSPc,658
14
+ pingen2sdk/api_resources/users.py,sha256=J3rxvT06mP-ZhTLSn8eNWeU1h6DNt3njfBAfb9H87V8,631
15
+ pingen2sdk-2.0.1.dist-info/LICENSE,sha256=SVZhRVsgo9j1SXYIReQFqCNbo10S51gQJZXoFw5hyxs,1497
16
+ pingen2sdk-2.0.1.dist-info/METADATA,sha256=7M4O5EyQ474PxxPpWU2tWkgQr6FzDOaLv-k8Fx2pD84,3906
17
+ pingen2sdk-2.0.1.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
18
+ pingen2sdk-2.0.1.dist-info/top_level.txt,sha256=fEAkBAnYCegjii3gu5gss2oLLCdjSnKJfRlydbHS-SE,11
19
+ pingen2sdk-2.0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: bdist_wheel (0.43.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ pingen2sdk