Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 26 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,46 @@
# Mono-Python
A simple python wrapper for MonoAPI (https://mono.co)
# PyMono
pymono is a python wrapper for <a href="https://mono.co"> Mono </a>

- 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 <a href="https://mono.co"> Mono </a> 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
<!-- - React js <a href="https://codesandbox.io/s/laughing-wildflower-0te1o?file=/src/App.js">sandbox</a> from Mono -->
-
## Todo
- complete unit test
- Travis integration
- publish to pypi
- Test with API access from Mono
- Support Webhook
- Documentation
71 changes: 24 additions & 47 deletions pymono/Base.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
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):
self._MONO_SEC_KEY = os.getenv('MONO-SEC-KEY', None)
if not self._MONO_SEC_KEY:
raise MissingAuthKeyError("Missing Authorization key argument or env variable")

def _headers(self):
def header(self):
"""
header function needed
"""
Expand All @@ -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"
6 changes: 3 additions & 3 deletions pymono/Errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class InvalidMethodError(PyMonoError):
"""
pass


class Error(PyMonoError):
"""
"""
Random exception taker
"""
pass

pass
29 changes: 17 additions & 12 deletions pymono/Mono.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,47 @@
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):
"""
This function Authenticate the API with key from mono
: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):
"""
Expand All @@ -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
Expand Down
20 changes: 20 additions & 0 deletions pymono/User.py
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion pymono/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from .Base import BaseAPI, MonoUser
from .Base import BaseAPI
from .User import MonoUser

52 changes: 40 additions & 12 deletions tests/test_mono.py
Original file line number Diff line number Diff line change
@@ -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__':
Expand Down