Skip to content

Commit a98b446

Browse files
committed
Add automations methods
1 parent a841f93 commit a98b446

File tree

2 files changed

+74
-6
lines changed

2 files changed

+74
-6
lines changed

afip/afip.py

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@
1212
os.environ["SSL_CERT_FILE"] = os.path.join(os.path.dirname(__file__), "data", "cacert.pem")
1313

1414
class Afip:
15-
sdk_version_number = '1.1.2'
15+
sdk_version_number = '1.2.0'
1616

1717
def __init__(self, options: dict):
18-
if not(options.get("CUIT")):
19-
raise Exception("CUIT field is required in options")
20-
21-
self.CUIT: int = options.get("CUIT")
18+
self.CUIT: int = options.get("CUIT") if options.get("CUIT") else None
2219
self.production: bool = options.get("production") if options.get("production") == True else False
2320
self.environment: str = "prod" if self.production == True else "dev"
2421
self.cert: str = options.get("cert")
@@ -194,4 +191,75 @@ def createWSAuth(self, username: str, password: str, alias: str, wsid: str) -> d
194191

195192

196193
raise Exception("Error: Waiting for too long")
194+
195+
# Create automation
196+
def createAutomation(self, automation: str, params: dict, wait: bool = True) -> dict:
197+
conn = http.client.HTTPSConnection("app.afipsdk.com")
198+
199+
payload = {
200+
"automation": automation,
201+
"params": params
202+
}
203+
204+
headers = {
205+
"Content-Type": "application/json",
206+
"sdk-version-number": self.sdk_version_number,
207+
"sdk-library": "python",
208+
"sdk-environment": self.environment
209+
}
210+
211+
if self.access_token: headers["Authorization"] = "Bearer %s" % self.access_token
212+
213+
conn.request("POST", "/api/v1/automations", json.dumps(payload), headers)
214+
215+
res = conn.getresponse()
216+
217+
data = res.read()
218+
219+
if res.getcode() >= 400:
220+
raise Exception(data.decode("utf-8"))
221+
222+
decoded_res = json.loads(data.decode("utf-8"))
223+
224+
if not(wait) or decoded_res["status"] == "complete":
225+
return decoded_res
226+
227+
return self.getAutomationDetails(decoded_res["id"], wait)
228+
229+
# Create automation
230+
def getAutomationDetails(self, id: str, wait: bool = True) -> dict:
231+
conn = http.client.HTTPSConnection("app.afipsdk.com")
232+
233+
headers = {
234+
"Content-Type": "application/json",
235+
"sdk-version-number": self.sdk_version_number,
236+
"sdk-library": "python",
237+
"sdk-environment": self.environment
238+
}
239+
240+
if self.access_token: headers["Authorization"] = "Bearer %s" % self.access_token
241+
242+
retry_request = 24
243+
244+
while retry_request >= 0:
245+
retry_request -= 1
246+
247+
conn.request("GET", "/api/v1/automations/%s" % id, None, headers)
248+
249+
res = conn.getresponse()
250+
251+
data = res.read()
252+
253+
if res.getcode() >= 400:
254+
raise Exception(data.decode("utf-8"))
255+
256+
decoded_res = json.loads(data.decode("utf-8"))
257+
258+
if not(wait) or decoded_res["status"] == "complete":
259+
return decoded_res
260+
261+
time.sleep(5)
262+
263+
264+
raise Exception("Error: Waiting for too long")
197265

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "afip.py"
3-
version = "1.1.2"
3+
version = "1.2.0"
44
description = "Conéctate a ARCA hoy mismo"
55
authors = ["AfipSDK <ayuda@afipsdk.com>"]
66
readme = "README.md"

0 commit comments

Comments
 (0)