forked from shakee93/fonoapi
-
Notifications
You must be signed in to change notification settings - Fork 7
/
fonAPI.py
73 lines (59 loc) · 2.05 KB
/
fonAPI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import sys
import json
import requests
"""
Class FonApi
Author @jesusperiago
Version 1
"""
class FonApi:
__ApiUrl = 'https://fonoapi.freshpixl.com/v1/'
def __init__(self, apikey, url=None):
self.__ApiUrl = FonApi.__ApiUrl
if url is not None:
self.__ApiUrl = url
self.__ApiKey = apikey
def getdevice(self, device, position=None, brand=None):
"""
Get device data object and return a json list
:param device:
:param position:
:param brand:
:return device list:
"""
url = self.__ApiUrl + 'getdevice'
postdata = {'brand': brand,
'device': device,
'position': position,
'token': self.__ApiKey}
headers = {'content-type': 'application/json'}
result = self.sendpostdata(url, postdata, headers)
try:
return result.json()
except AttributeError:
return result
def sendpostdata(self, url, postdata, headers, result = None):
"""
Send data to the server
:param url:
:param postdata:
:param headers:
:return requests.post result:
"""
try:
result = requests.post(url, data=json.dumps(postdata), headers=headers)
# Consider any status other than 2xx an error
if not result.status_code // 100 == 2:
return "Error status page: " + str(result)
# Try send the result text else send the error
try:
if result.json()['status'] == 'error':
if result.json()['message'] == 'Invalid Token. Generate a Token at fonoapi.freshpixl.com.':
return "Check __ApiKey"
return result.json()['message']
except:
pass
return result
except requests.exceptions.RequestException as e:
# A serious problem happened, like an SSLError or InvalidURL
return "Connect error. Check URL"