Skip to content

Commit 96308b2

Browse files
committed
refactured the pypesa module
1 parent e348b60 commit 96308b2

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ To begin using the package is pretty straight forward
8383
### Example of Usage (Customer to Bussiness Transaction)
8484

8585
```python
86-
>>>from pypesa import Mpesa
87-
>>>mpesa = Mpesa()
86+
>>>import pypesa
87+
>>>mpesa = pypesa()
8888
>>>transaction_query = {"input_Amount": "10",
8989
"input_Country": "TZN",
9090
"input_Currency": "TZS",

Diff for: pypesa/__init__.py

+32-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
"""
2+
Python Package to Easy the Integration with Vodacom Public API
3+
"""
4+
15
import os
26
import json
7+
import sys
38
import base64
49
import socket
510
import requests
@@ -136,18 +141,30 @@ def environment(self, enviro: str) -> Union[sandbox, production]:
136141

137142
@property
138143
def api_key(self) -> str:
139-
return self.auth_keys["api_key"]
144+
'''
145+
Return current api key
146+
'''
147+
return self.auth_keys.get("api_key")
140148

141149
@api_key.setter
142150
def api_key(self, Api_key: str) -> str:
151+
'''
152+
Use this propery to explicit set a api_key
153+
154+
>> import pypesa
155+
>> wallet = pypesa()
156+
>> wallet.api_key = " Your api key" #here
157+
158+
'''
143159
if isinstance(Api_key, str):
144160
self.auth_keys["api_key"] = Api_key
145161
return self.auth_keys["api_key"]
146-
raise TypeError(f"API key must be a of type String not {type(Api_key)}")
162+
raise TypeError(
163+
f"API key must be a of type String not {type(Api_key)}")
147164

148165
@property
149166
def public_key(self) -> str:
150-
return self.auth_keys["public_key"]
167+
return self.auth_keys.get("public_key")
151168

152169
@public_key.setter
153170
def public_key(self, pb_key: str) -> str:
@@ -165,7 +182,8 @@ def origin_address(self, ip_address: str) -> str:
165182
if isinstance(ip_address, str):
166183
self._origin_ip = ip_address
167184
return self._origin_ip
168-
raise TypeError(f"Address must be of type string not {type(ip_address)}")
185+
raise TypeError(
186+
f"Address must be of type string not {type(ip_address)}")
169187

170188
@authenticated
171189
def default_headers(self, auth_key: Optional[str] = "") -> dict:
@@ -216,7 +234,8 @@ def verify_query(transaction_query: dict, required_fields: set) -> bool:
216234
def customer_to_bussiness(self, transaction_query: dict) -> dict:
217235
""""""
218236

219-
self.verify_query(transaction_query, self.urls.re_customer_to_bussiness)
237+
self.verify_query(transaction_query,
238+
self.urls.re_customer_to_bussiness)
220239

221240
try:
222241
return requests.post(
@@ -233,7 +252,8 @@ def customer_to_bussiness(self, transaction_query: dict) -> dict:
233252
def bussiness_to_customer(self, transaction_query: dict) -> dict:
234253
""""""
235254

236-
self.verify_query(transaction_query, self.urls.re_bussiness_to_customer)
255+
self.verify_query(transaction_query,
256+
self.urls.re_bussiness_to_customer)
237257

238258
try:
239259

@@ -251,7 +271,8 @@ def bussiness_to_customer(self, transaction_query: dict) -> dict:
251271
def bussiness_to_bussiness(self, transaction_query: dict) -> dict:
252272
""""""
253273

254-
self.verify_query(transaction_query, self.urls.re_bussiness_to_bussiness)
274+
self.verify_query(transaction_query,
275+
self.urls.re_bussiness_to_bussiness)
255276

256277
try:
257278
return requests.post(
@@ -328,4 +349,7 @@ def direct_debit_payment(self, transaction_query: dict) -> dict:
328349
verify=True,
329350
)
330351
except (requests.ConnectTimeout, requests.ConnectionError):
331-
raise MpesaConnectionError
352+
raise MpesaConnectionError
353+
354+
355+
sys.modules[__name__] = Mpesa

Diff for: pypesa/service_urls.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ class Required(object):
7474

7575
class sandbox(Required):
7676
def __init__(self):
77-
7877
"""
7978
Service URL to be used during sandbox Development
8079
@@ -98,6 +97,12 @@ def __init__(self):
9897

9998
self.direct_debit_payment = "https://openapi.m-pesa.com:443/sandbox/ipg/v2/vodacomTZN/directDebitPayment/"
10099

100+
def __str__(self) -> str:
101+
return '<Using Sandbox Urls>'
102+
103+
def __str__(self) -> str:
104+
return '<Using Sandbox Urls>'
105+
101106

102107
class production(Required):
103108
def __init__(self):
@@ -120,7 +125,11 @@ def __init__(self):
120125
"https://openapi.m-pesa.com:443/openapi/ipg/v2/vodacomTZN/reversal/"
121126
)
122127
self.transaction_status = "https://openapi.m-pesa.com:443/openapi/ipg/v2/vodacomTZN/queryTransactionStatus/"
123-
124128
self.direct_debit = "https://openapi.m-pesa.com:443/openapi/ipg/v2/vodacomTZN/directDebitCreation/"
125-
126129
self.direct_debit_payment = "https://openapi.m-pesa.com:443/openapi/ipg/v2/vodacomTZN/directDebitPayment/"
130+
131+
def __str__(self) -> str:
132+
return '<Using Production Urls>'
133+
134+
def __repr__(self) -> str:
135+
return '<Using Production Urls>'

0 commit comments

Comments
 (0)