Skip to content

Commit 5022323

Browse files
committed
Add cert creation and ws authorization
1 parent a9e1842 commit 5022323

File tree

2 files changed

+101
-3
lines changed

2 files changed

+101
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
<!-- ABOUT THE PROJECT -->
3333
## Acerca del proyecto
34-
Con más de 60.000 descargas en sus versiones de Python, PHP, Node y Ruby desde el 2017 Afip SDK es la librería elegida por los desarrolladores para integrar sus plataformas con AFIP.
34+
Con más de 60k descargas, desde el 2017 Afip SDK es la librería elegida por los desarrolladores para integrar sus plataformas con AFIP.
3535

3636
Esta librería fue creada con la intención de ayudar a los programadores a usar los Web Services de AFIP sin romperse la cabeza ni perder tiempo tratando de entender la complicada documentación que AFIP provee.
3737

afip/afip.py

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import http.client
22
import json
3+
import time
34

45
from .web_service import WebService
56
from .electronic_billing import ElectronicBilling
@@ -8,7 +9,7 @@
89
from .register_scope_thirteen import RegisterScopeThirteen
910

1011
class Afip:
11-
sdk_version_number = '1.0.0'
12+
sdk_version_number = '1.0.1'
1213

1314
def __init__(self, options: dict):
1415
if not(options.get("CUIT")):
@@ -93,4 +94,101 @@ def webService(self, service, options: dict = {}) -> WebService:
9394
options['service'] = service
9495
options['generic'] = True
9596

96-
return WebService(self, options)
97+
return WebService(self, options)
98+
99+
# Create AFIP cert
100+
def createCert(self, username: str, password: str, alias: str) -> dict:
101+
conn = http.client.HTTPSConnection("app.afipsdk.com")
102+
103+
payload = {
104+
"environment": self.environment,
105+
"tax_id": self.CUIT,
106+
"username": username,
107+
"password": password,
108+
"alias": alias
109+
}
110+
111+
headers = {
112+
"Content-Type": "application/json",
113+
"sdk-version-number": self.sdk_version_number,
114+
"sdk-library": "python",
115+
"sdk-environment": self.environment
116+
}
117+
118+
if self.access_token: headers["Authorization"] = "Bearer %s" % self.access_token
119+
120+
retry_request = 24
121+
122+
while retry_request >= 0:
123+
retry_request -= 1
124+
125+
conn.request("POST", "/api/v1/afip/certs", json.dumps(payload), headers)
126+
127+
res = conn.getresponse()
128+
129+
data = res.read()
130+
131+
if res.getcode() >= 400:
132+
raise Exception(data.decode("utf-8"))
133+
134+
decoded_res = json.loads(data.decode("utf-8"))
135+
136+
if decoded_res["status"] == "complete":
137+
return decoded_res["data"]
138+
139+
if decoded_res.get("long_job_id"):
140+
payload["long_job_id"] = decoded_res["long_job_id"]
141+
142+
time.sleep(5)
143+
144+
raise Exception("Error: Waiting for too long")
145+
146+
# Create AFIP cert
147+
def createWSAuth(self, username: str, password: str, alias: str, wsid: str) -> dict:
148+
conn = http.client.HTTPSConnection("app.afipsdk.com")
149+
150+
payload = {
151+
"environment": self.environment,
152+
"tax_id": self.CUIT,
153+
"username": username,
154+
"password": password,
155+
"alias": alias,
156+
"wsid": wsid
157+
}
158+
159+
headers = {
160+
"Content-Type": "application/json",
161+
"sdk-version-number": self.sdk_version_number,
162+
"sdk-library": "python",
163+
"sdk-environment": self.environment
164+
}
165+
166+
if self.access_token: headers["Authorization"] = "Bearer %s" % self.access_token
167+
168+
retry_request = 24
169+
170+
while retry_request >= 0:
171+
retry_request -= 1
172+
173+
conn.request("POST", "/api/v1/afip/ws-auths", json.dumps(payload), headers)
174+
175+
res = conn.getresponse()
176+
177+
data = res.read()
178+
179+
if res.getcode() >= 400:
180+
raise Exception(data.decode("utf-8"))
181+
182+
decoded_res = json.loads(data.decode("utf-8"))
183+
184+
if decoded_res["status"] == "complete":
185+
return decoded_res["data"]
186+
187+
if decoded_res.get("long_job_id"):
188+
payload["long_job_id"] = decoded_res["long_job_id"]
189+
190+
time.sleep(5)
191+
192+
193+
raise Exception("Error: Waiting for too long")
194+

0 commit comments

Comments
 (0)