diff --git a/README.md b/README.md index 36dcaa5..1efd76e 100644 --- a/README.md +++ b/README.md @@ -1,41 +1,46 @@ -# Mono-Python -A simple python wrapper for MonoAPI (https://mono.co) +# PyMono +pymono is a python wrapper for Mono +- Account +- Transactions +- Statements +- Credits +- Debits +- Bvn Lookup -## Guide -Register on Mono website and get your Authorization key. +## Getting Started -Still in Development Phase - -# Built with -- Python 3.x - +- Register on Mono website and get your Authorization key. +- Setup your mono connect with your mono public key + +## Installing ```python pip install ?? ``` + +# Usage ```python - usage from pymono import Mono mono=Mono('MONO-CONNECT-ID') -# Authenticate Mono api key and use mono conncet id - data=mono.Auth() - mono.SetUserId(data.get('id')) - -# return the account of a user id gotten from mono - mono.GetAccount() - - ........ + # Authenticate Mono api key and use mono conncet id + data,status=mono.Auth() + #set user id + mono.SetUserId(data.get('id')) + # return status and json data + mono.getAccount() + ........ ``` +## Sanbox to test mono-connect + +- ## Todo -- complete unit test -- Travis integration - publish to pypi -- Test with API access from Mono - Support Webhook +- Documentation \ No newline at end of file diff --git a/pymono/Base.py b/pymono/Base.py index 292dee9..6c38736 100644 --- a/pymono/Base.py +++ b/pymono/Base.py @@ -1,14 +1,15 @@ +import json import os import requests -import json +from requests.exceptions import ConnectTimeout, ConnectionError -from pymono.Errors import MissingAuthKeyError, InvalidMethodError,Error +from pymono.Errors import MissingAuthKeyError class BaseAPI(object): - _BASE_URL = "https://api.withmono.com/ - + _BASE_URL = "https://api.withmono.com/" + _CONTENT_TYPE = 'application/json' def __init__(self): @@ -16,7 +17,7 @@ def __init__(self): if not self._MONO_SEC_KEY: raise MissingAuthKeyError("Missing Authorization key argument or env variable") - def _headers(self): + def header(self): """ header function needed """ @@ -25,61 +26,37 @@ def _headers(self): 'mono-sec-key': self._MONO_SEC_KEY } - def _parse_json(self, response): + def parse_json(self, response): """ This function takes in every json response sent back by the server and trys to get out the important return variables Returns a python tuple of status code, status(bool), message, data """ data = response.json() - return response.status_code, data + return data, response.status_code def _url(self, path): return self._BASE_URL + path - def _handle_request(self, method, url, data=None): + def _handle_request(self, method_type, url, data=None, params=None): """ Generic function to handle all API url calls Returns a python tuple of status code,data """ - method_type = { - 'GET': requests.get, - 'POST': requests.post, - } - - payload = json.dumps(data) - request = method_type.get(method) - - if not request: - raise InvalidMethodError("Request method not recognised or implemented") - - response = request(self._url(url), headers=self._headers(), data=payload, verify=True) - if response.status_code == 404: - raise InvalidMethodError("Not Found") - - if response.status_code in [200, 201]: - return self._parse_json(response) - else: - raise InvalidMethodError("Not connected") - - -class MonoUser: - def __init__(self): - """ - Setter & Getter Class for User Id response - """ - self.user_id = "" - - def SetUserId(self, user_id): - """ - This function set a return user id - """ - self.user_id = user_id - - def GetUserId(self): - """ - This function get a user id - """ - return self.user_id + payload = json.dumps({"code": data }) + param = {'period': params} + try: + response = requests.request(url=self._url(url), method=method_type, headers=self.header(), data=payload,params=param) + if response.status_code == 400: + return self.parse_json(response) + + if response.status_code in [200, 201]: + return self.parse_json(response) + else: + return self.parse_json(response) + except ConnectTimeout: + return 'The request timed out' + except ConnectionError: + return "connection not available" diff --git a/pymono/Errors.py b/pymono/Errors.py index d09deab..6301242 100644 --- a/pymono/Errors.py +++ b/pymono/Errors.py @@ -18,9 +18,9 @@ class InvalidMethodError(PyMonoError): """ pass + class Error(PyMonoError): - """ + """ Random exception taker """ - pass - + pass diff --git a/pymono/Mono.py b/pymono/Mono.py index 84ad62a..7fb4506 100644 --- a/pymono/Mono.py +++ b/pymono/Mono.py @@ -1,12 +1,13 @@ -from pymono import BaseAPI, MonoUser -import json +from pymono import BaseAPI + +from pymono import MonoUser class Mono(BaseAPI, MonoUser): - def __init__(self, code: str): + def __init__(self,code): + self.code=code super().__init__() - self.code = code def Auth(self): """ @@ -14,29 +15,33 @@ def Auth(self): :return: user_id """ - return self._handle_request("POST", 'account/"auth', data=self.code) + return self._handle_request("POST", 'account/auth', data=self.code) - def GetAccount(self) -> dict: + def getAccount(self) -> dict: """ This function get the User Account after authentication :return: json data of a user account """ - return self._handle_request("GET", "accounts/" +self.GetUserId()) + return self._handle_request("GET", "accounts/" + self.GetUserId()) - def GetTransactions(self): + def getTransactions(self): """ This function get the User transaction :return: json data of a user transactions """ return self._handle_request("GET", f'accounts/{self.GetUserId()}/transactions') - def getStatement(self,month): + def getStatement(self, month, type="json"): """ This function get a User Statement of account :return: json data of user statement of account :return: """ - return self._handle_request('GET',f" accounts/{self.GetUserId()}/statement?period={month}") + # if type == "json": + return self._handle_request('GET', f"accounts/{self.GetUserId()}/statement", params=month) + # if type=="Pdf": + # data=self._handle_request('GET', f" accounts/{self.GetUserId()}/statement", param=month) + # data[0]['data'] def getUserCredits(self): """ @@ -53,14 +58,14 @@ def getUserDebits(self): """ return self._handle_request("GET", f"accounts/{self.GetUserId()}/debits") - def GetUserIdentity(self): + def getUserIdentity(self): """ This function get a User Identity :return: json data of user identity """ return self._handle_request("GET", f'accounts/{self.GetUserId()}/identity') - def Bvn_lookup(self,bvn)->dict: + def bvn_lookup(self, bvn) -> dict: """ This function lookup a user bvn :return: json data of a user details diff --git a/pymono/User.py b/pymono/User.py new file mode 100644 index 0000000..d6b5e52 --- /dev/null +++ b/pymono/User.py @@ -0,0 +1,20 @@ + +class MonoUser: + def __init__(self, user_id=None): + """ + Setter & Getter Class for User Id response + """ + self.user_id = user_id + + def SetUserId(self, id): + """ + This function set a return user id + """ + self.user_id = id + + def GetUserId(self): + """ + This function get a user id + + """ + return self.user_id diff --git a/pymono/__init__.py b/pymono/__init__.py index 3fc55e6..dfa0aec 100644 --- a/pymono/__init__.py +++ b/pymono/__init__.py @@ -1 +1,3 @@ -from .Base import BaseAPI, MonoUser +from .Base import BaseAPI +from .User import MonoUser + diff --git a/tests/test_mono.py b/tests/test_mono.py index 4f47d74..7b43772 100644 --- a/tests/test_mono.py +++ b/tests/test_mono.py @@ -1,28 +1,56 @@ from . import Mono, BaseAPI, TestCase, main, test_mono_key +def assertUUid(uuid): + if uuid.isalnum(): + return True + else: + False + + class TestMono(TestCase): def setUp(self) -> None: super(TestMono, self).setUp() self.mono = Mono.Mono(code=" ") - # self.user_id = self.mono.GetUserId() + (data,status)= self.mono.Auth() + self.mono.SetUserId(data.get('id')) def test_mono_key(self): self.assertNotEqual(test_mono_key, "Missing Authorization key argument or env variable") def test_mono_auth(self): - status, data = self.mono.Auth() - print(data) - self.mono.SetUserId(data['id']) - - # def test_mono_Get_Account(self): - # status, data = self.mono.GetAccount() - # self.assertEqual(status, 200) - # - # def test_mono_Transaction(self): - # status, data = self.mono.GetAccount() - # self.assertEqual(status, 200) + assertUUid(self.mono.GetUserId()) + + def test_mono_Get_Account(self): + (data,status) = self.mono.getAccount() + self.assertEqual(status, 200) + + def test_mono_Transaction(self): + (data,status) = self.mono.getAccount() + self.assertEqual(status, 200) + + def test_mono_UserCredits(self): + (data,status) = self.mono.getUserCredits() + self.assertEqual(status, 200) + + def test_mono_UserDebits(self): + (data,status) = self.mono.getUserDebits() + self.assertEqual(status, 200) + + def test_mono_UserIdentity(self): + (data,status) = self.mono.getUserIdentity() + self.assertEqual(status, 200) + + def test_mono_getStatement(self): + + (data,status)= self.mono.getStatement("last6month") + self.assertEqual(status, 200) + + + # def test_mono_bvn_lookup(self): + # data = self.mono.bvn_lookup() + # self.assertEqual(data[1], 200) if __name__ == '__main__':