Skip to content

Commit f5f872e

Browse files
authored
Merge pull request #8 from hadpro24/dev
remove python3.6 support
2 parents 0a730e4 + d6994ae commit f5f872e

File tree

6 files changed

+247
-95
lines changed

6 files changed

+247
-95
lines changed

.github/workflows/pylint.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Pylint
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
strategy:
9+
matrix:
10+
python-version: ["3.8", "3.9", "3.10"]
11+
steps:
12+
- uses: actions/checkout@v3
13+
- name: Set up Python ${{ matrix.python-version }}
14+
uses: actions/setup-python@v3
15+
with:
16+
python-version: ${{ matrix.python-version }}
17+
- name: Install dependencies
18+
run: |
19+
python -m pip install --upgrade pip
20+
pip install pylint flake8 pytest coverage
21+
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
22+
- name: Analysing the code with pylint and flake8
23+
run: |
24+
pylint $(git ls-files '*.py')
25+
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
26+
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics

examples/code_usage.py

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
"""
2+
Module example code usage:
3+
4+
Ce module fournit les fonctions nécessaires
5+
pour envoyer des SMS via l'api Nimba SMS avec le SDK python.
6+
"""
7+
18
import os
29
import logging
310

@@ -6,84 +13,96 @@
613
ACCOUNT_SID = os.environ.get('NIMBA_ACCOUNT_SID')
714
AUTH_TOKEN = os.environ.get('NIMBA_AUTH_TOKEN')
815

16+
client = Client(ACCOUNT_SID, AUTH_TOKEN)
17+
logging.basicConfig()
18+
logging.basicConfig(filename='./log.txt') # or loging in file
19+
client.http_client.logger.setLevel(logging.INFO)
920

10-
def example():
21+
def check_balance():
1122
"""
12-
Some example usage of NIMBA SMS API with python client
23+
Check balance sms
1324
"""
14-
client = Client(ACCOUNT_SID, AUTH_TOKEN)
15-
logging.basicConfig()
16-
logging.basicConfig(filename='./log.txt') # or loging in file
17-
client.http_client.logger.setLevel(logging.INFO)
18-
19-
# Get your account balance
2025
response = client.accounts.get()
2126
if response.ok:
2227
my_account = response.data
23-
print('My Account balance : {}'.format(my_account['balance']))
28+
print(f"My Account balance : {my_account['balance']}")
2429

2530

26-
# Get groups
31+
def get_groups():
32+
"""
33+
get groups list
34+
"""
2735
response = client.groups.list()
2836
if response.ok:
2937
all_groups = response.data
30-
print('There are {} groupes'.format(len(all_messages)))
38+
print(f'There are {len(all_groups)} groupes')
3139

3240

33-
# Get Sendernames
41+
def get_sendernames():
42+
"""
43+
Get sendername validated
44+
"""
3445
response = client.sendernames.list()
3546
if response.ok:
3647
all_sendernames = response.data
3748
for item in all_sendernames:
3849
print(item)
3950

4051

41-
# Get Contact
52+
def add_contact():
53+
"""
54+
process contact added
55+
"""
4256
response = client.contacts.list()
4357
if response.ok:
4458
all_contacts = response.data
4559
for item in all_contacts:
4660
print(item)
4761

48-
49-
# Create Contact
5062
# This contact will be added to the default contact list
5163
response = client.contacts.create(numero='224XXXXXXXXX')
5264
if response.ok:
53-
contact = response.data
65+
print(response.data)
5466
# Create with groups and name - name and groups are optional.
55-
response = client.contacts.create(numero='224XXXXXXXXX', name='Foo', groups=['API', 'Facebook Client'])
67+
response = client.contacts.create(
68+
numero='224XXXXXXXXX', name='Foo', groups=['API', 'Facebook Client'])
5669
if response.ok:
57-
contact = response.data
70+
print(response.data)
5871

5972

60-
# Get All messages
73+
def send_message():
74+
"""
75+
Send message
76+
"""
6177
response = client.messages.list()
6278
if response.ok:
6379
all_messages = response.data
64-
print('There are {} messages in your account.'.format(len(all_messages)))
80+
print(f'There are {len(all_messages)} messages in your account.')
6581

6682
# Get only last 10 messages
6783
response = client.messages.list(limit=10)
6884
if response.ok:
6985
some_messages = some_messages.data
7086
print('Here are the last 10 messages in your account:')
71-
for m in some_messages:
72-
print(m)
87+
for message in some_messages:
88+
print(message)
7389

7490
# send message...
7591
print('Sending a message...')
7692
response = client.messages.create(to=['XXXX'],
7793
sender_name='YYYY', message='Hi Nimba!')
7894
if response.ok:
79-
print('message response : {}'.format(response.data))
80-
95+
print(f'message response : {response.data}')
8196

8297
# Retrieve message
8398
response = client.messages.retrieve(messageid='XXXXXXXXXXXXXXXXXXXXX')
8499
if response.ok:
85-
print("Message retrieve : {}".format(response.data))
100+
print(f"Message retrieve : {response.data}")
86101

87102

88-
def __name__ == '__main__':
89-
exemple()
103+
if __name__ == '__main__':
104+
check_balance()
105+
get_groups()
106+
get_sendernames()
107+
add_contact()
108+
send_message()

nimbasms/__init__.py

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
1+
"""
2+
A Nimba SMS Client API.
3+
4+
This module contains Class Client manager services available in API : developers.nimbasms.com
5+
6+
Dependencies
7+
-----------
8+
requests : A library for HTTP Request
9+
10+
class
11+
---------
12+
HTTPClient : Abstract class representing HTTP Client
13+
Response: Response representing data output.
14+
Client : Manager all services APIs.
15+
"""
16+
117
import logging
218
import platform
319
import json
420

21+
from urllib.parse import urlencode
522
from requests import Request, Session, hooks
623
from requests.adapters import HTTPAdapter
7-
from urllib.parse import urlencode
8-
from nimbasms.execptions import NimbaException
24+
from nimbasms.execptions import NimbaSMSException
25+
26+
from nimbasms.rest import Accounts
27+
from nimbasms.rest import Contacts
28+
from nimbasms.rest import Groups
29+
from nimbasms.rest import Messages
30+
from nimbasms.rest import SenderNames
931

1032
__version__ = '1.0.0'
1133

1234
_logger = logging.getLogger('nimbasms')
1335

14-
class HttpClient(object):
36+
class HttpClient:
1537
"""
1638
An Abstract class representing an HTTP client.
1739
"""
@@ -23,7 +45,10 @@ def request(self, method, url, params=None, data=None, headers=None,
2345
raise NimbaSMSException('HttpClient is a an abstract class')
2446

2547

26-
class Response(object):
48+
class Response:
49+
"""
50+
Representing data output API calls.
51+
"""
2752
def __init__(self, status_code, text, headers=None):
2853
self.content = text
2954
self.headers = headers
@@ -33,14 +58,20 @@ def __init__(self, status_code, text, headers=None):
3358

3459
@property
3560
def text(self):
61+
"""
62+
Content output byte
63+
"""
3664
return self.content
3765

3866
@property
3967
def data(self):
68+
"""
69+
Output data response APIs
70+
"""
4071
return json.loads(self.content)
4172

4273
def __repr__(self):
43-
return 'HTTP {} {}'.format(self.status_code, self.content)
74+
return f'HTTP {self.status_code} {self.content}'
4475

4576

4677
class NimbaHttpClient(HttpClient):
@@ -120,31 +151,33 @@ def request(self, method, url, params=None, data=None, headers=None,
120151
return self.last_response
121152

122153
def _log_request(self, kwargs):
154+
"""
155+
Logger request APIs
156+
"""
123157
self.logger.info('-- BEGIN Nimba SMS API Request --')
124158

125159
if kwargs['params']:
126-
self.logger.info('{} Request: {}?{}'.format(kwargs['method'],
127-
kwargs['url'], urlencode(kwargs['params'])))
128-
self.logger.info('Query Params: {}'.format(kwargs['params']))
160+
self.logger.info(
161+
f"{kwargs['method']} Request: {kwargs['url']}?{urlencode(kwargs['params'])}")
162+
self.logger.info(f"Query Params: {kwargs['params']}")
129163
else:
130-
self.logger.info('{} Request: {}'.format(kwargs['method'],
131-
kwargs['url']))
164+
self.logger.info(f"{kwargs['method']} Request: {kwargs['url']}")
132165

133166
if kwargs['headers']:
134167
self.logger.info('Headers:')
135168
for key, value in kwargs['headers'].items():
136169
#Do not log authorization headers
137170
if 'authorization' not in key.lower():
138-
self.logger.info('{} : {}'.format(key, value))
171+
self.logger.info(f'{key} : {value}')
139172

140173
self.logger.info('-- END Nimba SMS API Request --')
141174

142175
def _log_response(self, response):
143-
self.logger.info('Response Status Code: {}'.format(response.status_code))
144-
self.logger.info('Response Headers: {}'.format(response.headers))
176+
self.logger.info(f'Response Status Code: {response.status_code}')
177+
self.logger.info(f'Response Headers: {response.headers}')
145178

146179

147-
class Client(object):
180+
class Client:
148181
"""A client for accessing the Nimba SMS API."""
149182

150183
def __init__(self, account_sid=None, access_token=None):
@@ -154,13 +187,10 @@ def __init__(self, account_sid=None, access_token=None):
154187
:param str account_sid: Account SID
155188
:param str access_token: Token authenticate
156189
"""
157-
self.account_sid = account_sid
158-
self.access_token = access_token
159-
160-
if not self.account_sid or not self.access_token:
161-
raise NimbaException("Credentials are required"
190+
if not account_sid or not access_token:
191+
raise NimbaSMSException("Credentials are required"
162192
" to create a NimbaClient")
163-
self.auth = (self.account_sid, self.access_token)
193+
self.auth = (account_sid, access_token)
164194

165195
self.http_client = NimbaHttpClient()
166196

@@ -193,12 +223,8 @@ def request(self, method, uri, params=None, data=None,
193223
os_name = platform.system()
194224
os_arch = platform.machine()
195225
python_version = platform.python_version()
196-
headers['User-Agent'] = 'nimba-python/{} ({} {}) Python/{}'.format(
197-
pkg_version,
198-
os_name,
199-
os_arch,
200-
python_version
201-
)
226+
headers['User-Agent'] = (
227+
f'nimba-python/{pkg_version} ({os_name} {os_arch}) Python/{python_version}')
202228
headers['X-Nimba-Client'] = 'utf-8'
203229

204230
if method == 'POST' and 'Content-Type' not in headers:
@@ -225,7 +251,6 @@ def accounts(self):
225251
:returns Accounts
226252
"""
227253
if self._accounts is None:
228-
from nimbasms.rest import Accounts
229254
self._accounts = Accounts(self)
230255
return self._accounts
231256

@@ -237,7 +262,6 @@ def messages(self):
237262
:returns Messages NimbaAPI
238263
"""
239264
if self._messages is None:
240-
from nimbasms.rest import Messages
241265
self._messages = Messages(self)
242266
return self._messages
243267

@@ -249,30 +273,27 @@ def contacts(self):
249273
:returns Contacts NimbaAPI
250274
"""
251275
if self._contacts is None:
252-
from nimbasms.rest import Contacts
253276
self._contacts = Contacts(self)
254277
return self._contacts
255278

256279
@property
257280
def groups(self):
258281
"""
259-
Group Accounts
260-
282+
Group Accounts.
283+
261284
:returns Groups NimbaAPI
262285
"""
263286
if self._groups is None:
264-
from nimbasms.rest import Groups
265287
self._groups = Groups(self)
266288
return self._groups
267289

268290
@property
269291
def sendernames(self):
270292
"""
271-
Sendername Accounts
272-
293+
Sendername Accounts.
294+
273295
:returns Sendername NimbaAPI
274296
"""
275297
if self._sendernames is None:
276-
from nimbasms.rest import SenderNames
277298
self._sendernames = SenderNames(self)
278299
return self._sendernames

nimbasms/execptions.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1-
class NimbaException(Exception):
2-
pass
1+
"""
2+
Exception module for this project.
3+
"""
4+
5+
class NimbaSMSException(Exception):
6+
"""
7+
Module exception for project
8+
"""

0 commit comments

Comments
 (0)