Skip to content

Commit

Permalink
Add timeout for requests internal call
Browse files Browse the repository at this point in the history
  • Loading branch information
moeenz committed Sep 22, 2019
1 parent b73074f commit 13a19eb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ Anyway there is good tutorial about <a href="http://gun.io/blog/how-to-github-fo

## Usage

Well, There is an example to Send SMS by Python below.
Well, There is an example to Send SMS by Python below. `timeout` parameter is optional in `KavenegarAPI` constructor, default value is set to 10 seconds.

### Send
```python
from kavenegar import *
try:
api = KavenegarAPI('Your APIKey')
api = KavenegarAPI('Your APIKey', timeout=20)
params = {
'sender': '',#optional
'receptor': '',#multiple mobile number, split by comma
Expand All @@ -46,7 +46,7 @@ except HTTPException as e:
#!/usr/bin/env python
from kavenegar import *
try:
api = KavenegarAPI('Your APIKey')
api = KavenegarAPI('Your APIKey', timeout=20)
params = {
'receptor': '',
'template': '',
Expand All @@ -65,7 +65,7 @@ except HTTPException as e:
#!/usr/bin/env python
from kavenegar import *
try:
api = KavenegarAPI('Your APIKey')
api = KavenegarAPI('Your APIKey', timeout=20)
params = {
'sender':'["",""]',#array of string as json
'receptor': '["",""]',#array of string as json
Expand Down
10 changes: 8 additions & 2 deletions kavenegar.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@
except ImportError:
import simplejson as json


# Default requests timeout in seconds.
DEFAULT_TIMEOUT = 10


class APIException(Exception):
pass

class HTTPException(Exception):
pass

class KavenegarAPI(object):
def __init__(self, apikey):
def __init__(self, apikey, timeout=None):
self.version = 'v1'
self.host = 'api.kavenegar.com'
self.apikey = apikey
self.timeout = timeout or DEFAULT_TIMEOUT
self.headers = {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
Expand All @@ -31,7 +37,7 @@ def __str__(self):
def _request(self, action, method, params={}):
url = 'https://' + self.host + '/' + self.version + '/' + self.apikey + '/' + action + '/' + method + '.json'
try:
content = requests.post(url , headers=self.headers,auth=None,data=params).content
content = requests.post(url , headers=self.headers,auth=None,data=params, timeout=self.timeout).content
try:
response = json.loads(content.decode("utf-8"))
if (response['return']['status']==200):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
setup(
name = "kavenegar",
py_modules = ['kavenegar'],
version = "1.1.1",
version = "1.1.3",
description = "Kavenegar Python library",
author = "Kavenegar Team",
author_email = "support@kavenegar.com",
Expand Down

0 comments on commit 13a19eb

Please sign in to comment.