Move from querry lib urllib3 to requests
This commit is contained in:
@@ -11,16 +11,16 @@ import georideapilib.api as GeorideApi
|
|||||||
def example():
|
def example():
|
||||||
""" simple example function """
|
""" simple example function """
|
||||||
token = "<your_token>"# pylint: disable=C0301
|
token = "<your_token>"# pylint: disable=C0301
|
||||||
account = GeorideAccount(0, "<your_email>", False, token)
|
"""account = GeorideAccount(0, "<your_email>", False, token)"""
|
||||||
|
|
||||||
"""
|
|
||||||
GeorideApi.getAuthorisationToken("<your_email>", "<your_password>")
|
account = GeorideApi.get_authorisation_token("<your_email>", "<your_password>")
|
||||||
print("token 1: ", account.auth_token)
|
print("token 1: ", account.auth_token)
|
||||||
""" # pylint: disable=W0105
|
# pylint: disable=W0105
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
account.auth_token = GeorideApi.renewToken(account.auth_token)
|
account.auth_token = GeorideApi.renew_token(account.auth_token)
|
||||||
print("token 2: ", account.auth_token)
|
print("token 2: ", account.auth_token)
|
||||||
""" # pylint: disable=W0105
|
""" # pylint: disable=W0105
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ Georide api lib
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import urllib3
|
import logging
|
||||||
|
import requests
|
||||||
|
|
||||||
from georideapilib.objects import (
|
from georideapilib.objects import (
|
||||||
GeorideTracker,
|
GeorideTracker,
|
||||||
@@ -28,70 +29,65 @@ GEORIDE_API_ENDPOINT_TOGGLE_LOCK = "/tracker/:trackerId/toggleLock"
|
|||||||
GEORIDE_API_ENDPOINT_POSITIONS = "/tracker/:trackerId/trips/positions"
|
GEORIDE_API_ENDPOINT_POSITIONS = "/tracker/:trackerId/trips/positions"
|
||||||
GEORIDE_API_ENDPOINT_TRIP_SHARE = "/tracker/:trackerId/share/trip"
|
GEORIDE_API_ENDPOINT_TRIP_SHARE = "/tracker/:trackerId/share/trip"
|
||||||
|
|
||||||
|
_SESSION = requests.Session()
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
def get_authorisation_token(email, password):
|
def get_authorisation_token(email, password):
|
||||||
""" return an authorization token """
|
""" return an authorization token """
|
||||||
http = urllib3.PoolManager()
|
|
||||||
data = {"email": email, "password": password}
|
data = {"email": email, "password": password}
|
||||||
encoded_data = json.dumps(data).encode('utf-8')
|
encoded_data = json.dumps(data).encode('utf-8')
|
||||||
response = http.request(
|
response = _SESSION.post(
|
||||||
'POST',
|
|
||||||
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_LOGIN,
|
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_LOGIN,
|
||||||
body=encoded_data,
|
data=encoded_data,
|
||||||
headers={'Content-Type': 'application/json'})
|
headers={'Content-Type': 'application/json'})
|
||||||
response_data = json.loads(response.data.decode('utf-8'))
|
response_data = response.json()
|
||||||
|
_LOGGER.debug(response_data)
|
||||||
account = GeorideAccount.from_json(response_data)
|
account = GeorideAccount.from_json(response_data)
|
||||||
return account
|
return account
|
||||||
|
|
||||||
|
|
||||||
def renew_token(token):
|
def renew_token(token):
|
||||||
""" renew the authorization token """
|
""" renew the authorization token """
|
||||||
http = urllib3.PoolManager()
|
|
||||||
headers = {"Authorization": "Bearer " + token}
|
headers = {"Authorization": "Bearer " + token}
|
||||||
response = http.request(
|
response = _SESSION.get(
|
||||||
'GET',
|
|
||||||
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_NEW_TOKEN,
|
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_NEW_TOKEN,
|
||||||
headers=headers)
|
headers=headers)
|
||||||
response_data = json.loads(response.data.decode('utf-8'))
|
response_data = response.json()
|
||||||
|
_LOGGER.debug(response_data)
|
||||||
new_token = response_data['authToken']
|
new_token = response_data['authToken']
|
||||||
return new_token
|
return new_token
|
||||||
|
|
||||||
|
|
||||||
def revoke_token(token):
|
def revoke_token(token):
|
||||||
""" invalidate the authorization token """
|
""" invalidate the authorization token """
|
||||||
http = urllib3.PoolManager()
|
|
||||||
headers = {"Authorization": "Bearer " + token}
|
headers = {"Authorization": "Bearer " + token}
|
||||||
response = http.request(
|
response = _SESSION.post(
|
||||||
'POST',
|
|
||||||
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_LOGOUT,
|
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_LOGOUT,
|
||||||
headers=headers)
|
headers=headers)
|
||||||
if response.status != 204:
|
if response.status_code != 204:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def get_user(token):
|
def get_user(token):
|
||||||
""" get the georide user info """
|
""" get the georide user info """
|
||||||
http = urllib3.PoolManager()
|
|
||||||
headers = {"Authorization": "Bearer " + token}
|
headers = {"Authorization": "Bearer " + token}
|
||||||
response = http.request(
|
response = _SESSION.get(
|
||||||
'GET',
|
|
||||||
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_USER,
|
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_USER,
|
||||||
headers=headers)
|
headers=headers)
|
||||||
response_data = json.loads(response.data.decode('utf-8'))
|
response_data = response.json()
|
||||||
|
_LOGGER.debug(response_data)
|
||||||
account = GeorideUser.from_json(response_data)
|
account = GeorideUser.from_json(response_data)
|
||||||
return account
|
return account
|
||||||
|
|
||||||
def get_trackers(token):
|
def get_trackers(token):
|
||||||
""" get user trackers """
|
""" get user trackers """
|
||||||
http = urllib3.PoolManager()
|
|
||||||
headers = {"Authorization": "Bearer " + token}
|
headers = {"Authorization": "Bearer " + token}
|
||||||
response = http.request(
|
response = _SESSION.get(
|
||||||
'GET',
|
|
||||||
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_TRAKERS,
|
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_TRAKERS,
|
||||||
headers=headers)
|
headers=headers)
|
||||||
|
|
||||||
response_data = json.loads(response.data.decode('utf-8'))
|
response_data = response.json()
|
||||||
trackers = []
|
trackers = []
|
||||||
for json_tracker in response_data:
|
for json_tracker in response_data:
|
||||||
trackers.append(GeorideTracker.from_json(json_tracker))
|
trackers.append(GeorideTracker.from_json(json_tracker))
|
||||||
@@ -100,15 +96,13 @@ def get_trackers(token):
|
|||||||
|
|
||||||
def get_trips(token, tracker_id, from_date, to_date):
|
def get_trips(token, tracker_id, from_date, to_date):
|
||||||
""" return all trips between two dates """
|
""" return all trips between two dates """
|
||||||
http = urllib3.PoolManager()
|
|
||||||
headers = {"Authorization": "Bearer " + token}
|
headers = {"Authorization": "Bearer " + token}
|
||||||
response = http.request(
|
response = _SESSION.get(
|
||||||
'GET',
|
|
||||||
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_TRIPS.replace(':trackerId', str(tracker_id)),
|
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_TRIPS.replace(':trackerId', str(tracker_id)),
|
||||||
fields={'from': from_date, 'to': to_date},
|
params={'from': from_date, 'to': to_date},
|
||||||
headers=headers)
|
headers=headers)
|
||||||
|
|
||||||
response_data = json.loads(response.data.decode('utf-8'))
|
response_data = response.json()
|
||||||
trips = []
|
trips = []
|
||||||
for json_trip in response_data:
|
for json_trip in response_data:
|
||||||
trips.append(GeorideTrackerTrip.from_json(json_trip))
|
trips.append(GeorideTrackerTrip.from_json(json_trip))
|
||||||
@@ -116,15 +110,13 @@ def get_trips(token, tracker_id, from_date, to_date):
|
|||||||
|
|
||||||
def get_positions(token, tracker_id, from_date, to_date):
|
def get_positions(token, tracker_id, from_date, to_date):
|
||||||
""" return all trips between two dates """
|
""" return all trips between two dates """
|
||||||
http = urllib3.PoolManager()
|
|
||||||
headers = {"Authorization": "Bearer " + token}
|
headers = {"Authorization": "Bearer " + token}
|
||||||
response = http.request(
|
response = _SESSION.get(
|
||||||
'GET',
|
|
||||||
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_POSITIONS.replace(':trackerId', str(tracker_id)),
|
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_POSITIONS.replace(':trackerId', str(tracker_id)),
|
||||||
fields={'from': from_date, 'to': to_date},
|
params={'from': from_date, 'to': to_date},
|
||||||
headers=headers)
|
headers=headers)
|
||||||
|
|
||||||
response_data = json.loads(response.data.decode('utf-8'))
|
response_data = response.json()
|
||||||
positions = []
|
positions = []
|
||||||
for json_position in response_data:
|
for json_position in response_data:
|
||||||
positions.append(GeorideTrackerPosition.from_json(json_position))
|
positions.append(GeorideTrackerPosition.from_json(json_position))
|
||||||
@@ -154,56 +146,46 @@ def _share_a_trip(token, tracker_id, trip_id=None, from_date=None, # pylint: dis
|
|||||||
data = {"tripMergedId": trip_merged_id}
|
data = {"tripMergedId": trip_merged_id}
|
||||||
|
|
||||||
encoded_data = json.dumps(data).encode('utf-8')
|
encoded_data = json.dumps(data).encode('utf-8')
|
||||||
print("Trip data: ", encoded_data)
|
|
||||||
|
|
||||||
http = urllib3.PoolManager()
|
|
||||||
headers = {
|
headers = {
|
||||||
"Authorization": "Bearer " + token,
|
"Authorization": "Bearer " + token,
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
}
|
}
|
||||||
response = http.request(
|
response = _SESSION.post(
|
||||||
'POST',
|
|
||||||
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_TRIP_SHARE.replace(':trackerId', str(tracker_id)),
|
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_TRIP_SHARE.replace(':trackerId', str(tracker_id)),
|
||||||
body=encoded_data,
|
data=encoded_data,
|
||||||
headers=headers)
|
headers=headers)
|
||||||
|
|
||||||
response_data = json.loads(response.data.decode('utf-8'))
|
response_data = response.json()
|
||||||
print("Trip data: ", response_data)
|
|
||||||
return GeorideSharedTrip.from_json(response_data)
|
return GeorideSharedTrip.from_json(response_data)
|
||||||
|
|
||||||
def lock_tracker(token, tracker_id):
|
def lock_tracker(token, tracker_id):
|
||||||
""" used to lock a tracker """
|
""" used to lock a tracker """
|
||||||
http = urllib3.PoolManager()
|
|
||||||
headers = {"Authorization": "Bearer " + token}
|
headers = {"Authorization": "Bearer " + token}
|
||||||
response = http.request(
|
response = _SESSION.post(
|
||||||
'POST',
|
|
||||||
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_LOCK.replace(':trackerId', str(tracker_id)),
|
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_LOCK.replace(':trackerId', str(tracker_id)),
|
||||||
headers=headers)
|
headers=headers)
|
||||||
if response.status != 204:
|
if response.status_code != 204:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def unlock_tracker(token, tracker_id):
|
def unlock_tracker(token, tracker_id):
|
||||||
""" used to unlock a tracker """
|
""" used to unlock a tracker """
|
||||||
http = urllib3.PoolManager()
|
|
||||||
headers = {"Authorization": "Bearer " + token}
|
headers = {"Authorization": "Bearer " + token}
|
||||||
response = http.request(
|
response = _SESSION.post(
|
||||||
'POST',
|
|
||||||
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_UNLOCK.replace(':trackerId', str(tracker_id)),
|
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_UNLOCK.replace(':trackerId', str(tracker_id)),
|
||||||
headers=headers)
|
headers=headers)
|
||||||
if response.status != 204:
|
if response.status_code != 204:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def toogle_lock_tracker(token, tracker_id):
|
def toogle_lock_tracker(token, tracker_id):
|
||||||
""" used to toggle lock a tracker """
|
""" used to toggle lock a tracker """
|
||||||
http = urllib3.PoolManager()
|
|
||||||
headers = {"Authorization": "Bearer " + token}
|
headers = {"Authorization": "Bearer " + token}
|
||||||
response = http.request(
|
response = _SESSION.post(
|
||||||
'POST',
|
|
||||||
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_TOGGLE_LOCK.replace(':trackerId', str(tracker_id)),
|
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_TOGGLE_LOCK.replace(':trackerId', str(tracker_id)),
|
||||||
headers=headers)
|
headers=headers)
|
||||||
response_data = json.loads(response.data.decode('utf-8'))
|
response_data = response.json()
|
||||||
return response_data['locked']
|
return response_data['locked']
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
{
|
|
||||||
"domain": "georide",
|
|
||||||
"name": "Georide",
|
|
||||||
"config_flow": true,
|
|
||||||
"documentation": "https://georide.fr",
|
|
||||||
"dependencies": [
|
|
||||||
"urllib3"
|
|
||||||
],
|
|
||||||
"codeowners": []
|
|
||||||
}
|
|
||||||
2
setup.py
2
setup.py
@@ -19,7 +19,7 @@ CURRENT_DIR = os.path.dirname(__file__)
|
|||||||
setup(
|
setup(
|
||||||
name='georideapilib',
|
name='georideapilib',
|
||||||
packages=['georideapilib'], # this must be the same as the name above
|
packages=['georideapilib'], # this must be the same as the name above
|
||||||
version='0.1.0',
|
version='0.2.0',
|
||||||
description='Lib to control georide tracker devices with their rest api',
|
description='Lib to control georide tracker devices with their rest api',
|
||||||
author='Matthieu DUVAL',
|
author='Matthieu DUVAL',
|
||||||
author_email='georideapilib@duval-dev.fr',
|
author_email='georideapilib@duval-dev.fr',
|
||||||
|
|||||||
Reference in New Issue
Block a user