Move from querry lib urllib3 to requests
This commit is contained in:
@@ -11,16 +11,16 @@ import georideapilib.api as GeorideApi
|
||||
def example():
|
||||
""" simple example function """
|
||||
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)
|
||||
""" # 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)
|
||||
""" # pylint: disable=W0105
|
||||
|
||||
|
||||
@@ -4,7 +4,8 @@ Georide api lib
|
||||
"""
|
||||
|
||||
import json
|
||||
import urllib3
|
||||
import logging
|
||||
import requests
|
||||
|
||||
from georideapilib.objects import (
|
||||
GeorideTracker,
|
||||
@@ -28,70 +29,65 @@ GEORIDE_API_ENDPOINT_TOGGLE_LOCK = "/tracker/:trackerId/toggleLock"
|
||||
GEORIDE_API_ENDPOINT_POSITIONS = "/tracker/:trackerId/trips/positions"
|
||||
GEORIDE_API_ENDPOINT_TRIP_SHARE = "/tracker/:trackerId/share/trip"
|
||||
|
||||
_SESSION = requests.Session()
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
def get_authorisation_token(email, password):
|
||||
""" return an authorization token """
|
||||
http = urllib3.PoolManager()
|
||||
data = {"email": email, "password": password}
|
||||
encoded_data = json.dumps(data).encode('utf-8')
|
||||
response = http.request(
|
||||
'POST',
|
||||
response = _SESSION.post(
|
||||
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_LOGIN,
|
||||
body=encoded_data,
|
||||
data=encoded_data,
|
||||
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)
|
||||
return account
|
||||
|
||||
|
||||
def renew_token(token):
|
||||
""" renew the authorization token """
|
||||
http = urllib3.PoolManager()
|
||||
headers = {"Authorization": "Bearer " + token}
|
||||
response = http.request(
|
||||
'GET',
|
||||
response = _SESSION.get(
|
||||
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_NEW_TOKEN,
|
||||
headers=headers)
|
||||
response_data = json.loads(response.data.decode('utf-8'))
|
||||
response_data = response.json()
|
||||
_LOGGER.debug(response_data)
|
||||
new_token = response_data['authToken']
|
||||
return new_token
|
||||
|
||||
|
||||
def revoke_token(token):
|
||||
""" invalidate the authorization token """
|
||||
http = urllib3.PoolManager()
|
||||
headers = {"Authorization": "Bearer " + token}
|
||||
response = http.request(
|
||||
'POST',
|
||||
response = _SESSION.post(
|
||||
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_LOGOUT,
|
||||
headers=headers)
|
||||
if response.status != 204:
|
||||
if response.status_code != 204:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def get_user(token):
|
||||
""" get the georide user info """
|
||||
http = urllib3.PoolManager()
|
||||
headers = {"Authorization": "Bearer " + token}
|
||||
response = http.request(
|
||||
'GET',
|
||||
response = _SESSION.get(
|
||||
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_USER,
|
||||
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)
|
||||
return account
|
||||
|
||||
def get_trackers(token):
|
||||
""" get user trackers """
|
||||
http = urllib3.PoolManager()
|
||||
headers = {"Authorization": "Bearer " + token}
|
||||
response = http.request(
|
||||
'GET',
|
||||
response = _SESSION.get(
|
||||
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_TRAKERS,
|
||||
headers=headers)
|
||||
|
||||
response_data = json.loads(response.data.decode('utf-8'))
|
||||
response_data = response.json()
|
||||
trackers = []
|
||||
for json_tracker in response_data:
|
||||
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):
|
||||
""" return all trips between two dates """
|
||||
http = urllib3.PoolManager()
|
||||
headers = {"Authorization": "Bearer " + token}
|
||||
response = http.request(
|
||||
'GET',
|
||||
response = _SESSION.get(
|
||||
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)
|
||||
|
||||
response_data = json.loads(response.data.decode('utf-8'))
|
||||
response_data = response.json()
|
||||
trips = []
|
||||
for json_trip in response_data:
|
||||
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):
|
||||
""" return all trips between two dates """
|
||||
http = urllib3.PoolManager()
|
||||
headers = {"Authorization": "Bearer " + token}
|
||||
response = http.request(
|
||||
'GET',
|
||||
response = _SESSION.get(
|
||||
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)
|
||||
|
||||
response_data = json.loads(response.data.decode('utf-8'))
|
||||
response_data = response.json()
|
||||
positions = []
|
||||
for json_position in response_data:
|
||||
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}
|
||||
|
||||
encoded_data = json.dumps(data).encode('utf-8')
|
||||
print("Trip data: ", encoded_data)
|
||||
|
||||
http = urllib3.PoolManager()
|
||||
headers = {
|
||||
"Authorization": "Bearer " + token,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
response = http.request(
|
||||
'POST',
|
||||
response = _SESSION.post(
|
||||
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_TRIP_SHARE.replace(':trackerId', str(tracker_id)),
|
||||
body=encoded_data,
|
||||
data=encoded_data,
|
||||
headers=headers)
|
||||
|
||||
response_data = json.loads(response.data.decode('utf-8'))
|
||||
print("Trip data: ", response_data)
|
||||
response_data = response.json()
|
||||
return GeorideSharedTrip.from_json(response_data)
|
||||
|
||||
def lock_tracker(token, tracker_id):
|
||||
""" used to lock a tracker """
|
||||
http = urllib3.PoolManager()
|
||||
headers = {"Authorization": "Bearer " + token}
|
||||
response = http.request(
|
||||
'POST',
|
||||
response = _SESSION.post(
|
||||
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_LOCK.replace(':trackerId', str(tracker_id)),
|
||||
headers=headers)
|
||||
if response.status != 204:
|
||||
if response.status_code != 204:
|
||||
return False
|
||||
return True
|
||||
|
||||
def unlock_tracker(token, tracker_id):
|
||||
""" used to unlock a tracker """
|
||||
http = urllib3.PoolManager()
|
||||
headers = {"Authorization": "Bearer " + token}
|
||||
response = http.request(
|
||||
'POST',
|
||||
response = _SESSION.post(
|
||||
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_UNLOCK.replace(':trackerId', str(tracker_id)),
|
||||
headers=headers)
|
||||
if response.status != 204:
|
||||
if response.status_code != 204:
|
||||
return False
|
||||
return True
|
||||
|
||||
def toogle_lock_tracker(token, tracker_id):
|
||||
""" used to toggle lock a tracker """
|
||||
http = urllib3.PoolManager()
|
||||
headers = {"Authorization": "Bearer " + token}
|
||||
response = http.request(
|
||||
'POST',
|
||||
response = _SESSION.post(
|
||||
GEORIDE_API_HOST + GEORIDE_API_ENDPOINT_TOGGLE_LOCK.replace(':trackerId', str(tracker_id)),
|
||||
headers=headers)
|
||||
response_data = json.loads(response.data.decode('utf-8'))
|
||||
response_data = response.json()
|
||||
return response_data['locked']
|
||||
|
||||
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(
|
||||
name='georideapilib',
|
||||
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',
|
||||
author='Matthieu DUVAL',
|
||||
author_email='georideapilib@duval-dev.fr',
|
||||
|
||||
Reference in New Issue
Block a user