Skip to content

Commit

Permalink
Merge pull request #2 from mapup/refactor-scripts
Browse files Browse the repository at this point in the history
Refactor scripts to extract variables to global scope
  • Loading branch information
vishalbd authored Mar 11, 2024
2 parents e2fe793 + 57c430c commit d3eeb50
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 24 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# toll-for-gps-tracks-csv-upload
Use TollGuru "GPS tracks Toll API" endpoint to calculate tolls after you make the trip.

Use TollGuru "GPS tracks Toll API" endpoint to calculate tolls after you make the trip.

You can upload your GPS tracks (in CSV format) to receive tolls for the route matched using the GPS tracks. You can
* You can see vehicles in use in each country, [vehicle type support](https://github.com/mapup/tollguru_vehicle_coverage/wiki/Vehicle-types-supported-by-TollGuru). For example, you can receive tolls for vehicles based on axle counts for cars, SUV, pick-up, trucks (up to 9-axles), motorcycle, bus, motorhome, RV, limousine.
* Specify parameters such as weight and height to receive tolls based on the specified parameters
* Specify the timestamp of each GPS trace. If you do not specify the timestamp, tolls are likely to be inaccurate on time-based-toll facilities.
* Specify whether you want to receive toll information immediately (isAsync=false) or can wait (isAsync=true). Use the asynchronous mode when uploading multiple or large GPS track files. Response in asynchronous mode comes with a requestId and a requestedTimestamp. If you are setting isAsync=true, pass the result you are getting from the function `gps_tracks_csv_upload` to the function `gps_tracks_csv_dwonload` to get the result. These results would be available for download for up to 30 days.

For more examples of the different ways in which the responses from this endpoint can be configured, you can refer to: [Our API parameter examples repository.](https://github.com/mapup/tollguru-api-parameter-examples/tree/main/request-bodies/03-TollTally-GPS-Tracks-To-Toll)
69 changes: 46 additions & 23 deletions gps-tracks-csv-upload.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,67 @@
import json
from time import sleep
import requests
import os

from urllib.parse import urlencode

TOLLGURU_API_KEY = os.environ.get("TOLLGURU_API_KEY")
TOLLGURU_API_URL = "https://apis.tollguru.com/toll/v2"

GPS_UPLOAD_ENDPOINT = "gps-tracks-csv-upload"
GPS_DOWNLOAD_ENDPOINT = "gps-tracks-csv-download"

# Configurable parameters for GPS upload endpoint
PARAMETERS = {
"vehicle": {
"weight": 3000,
"height": 10,
"type": "5AxlesTruck",
},
}

# API key for Tollguru
TOLLGURU_API_KEY = "YOUR_TOLLGURU_API_KEY"

# Upload CSV file - You can use the CSV format file as shown in the table below.
def gps_tracks_csv_upload(isAsync="false"):
payload = 'lat-long-france-sample.csv'
def gps_tracks_csv_upload(is_async: bool = False):
payload = "Sample-GPS-tracks/france-GPS-Track-sample.csv"

url = f"https://apis.tollguru.com/toll/v2/gps-tracks-csv-upload?isAsync={isAsync}&weight=30000&height=10&vehicleType=5AxlesTruck&vehicleName=123456"
url = f"{TOLLGURU_API_URL}/{GPS_UPLOAD_ENDPOINT}?{urlencode({**PARAMETERS['vehicle'], 'isAsync': is_async})}"

headers = {
'x-api-key': TOLLGURU_API_KEY,
'Content-Type': 'text/csv'
}
headers = {"x-api-key": TOLLGURU_API_KEY, "Content-Type": "text/csv"}

with open(payload, 'rb') as f:
with open(payload, "rb") as f:
response = requests.request("POST", url, headers=headers, data=f)
# response = json.loads(response.text)
return response


# When isAsync is set to true, use this funcation to get the result by passing the return value from the gps_tracks_csv_upload
def gps_tracks_csv_download(response_from_gps_tracks_csv_upload, retry, delay: int):
count = retry # number of retries before stoping
def gps_tracks_csv_download(
response_from_gps_tracks_csv_upload, retry: int = 5, delay: int = 5
):
count = retry # number of retries before stoping
status = "ERROR"
while count >= 0 and status == "ERROR" :
url = "https://apis.tollguru.com/toll/v2/gps-tracks-csv-download"
response = None

while count >= 0 and status == "ERROR":
url = f"{TOLLGURU_API_URL}/{GPS_DOWNLOAD_ENDPOINT}"

headers = {
'x-api-key': TOLLGURU_API_KEY,
'Content-Type': 'application/json'
}
headers = {"x-api-key": TOLLGURU_API_KEY, "Content-Type": "application/json"}

response = requests.request("POST", url, headers=headers, data=response_from_gps_tracks_csv_upload)
response = requests.request(
"POST", url, headers=headers, data=response_from_gps_tracks_csv_upload
)
response = json.loads(response.text)
status = response['status']
status = response["status"]
count -= 1
sleep(delay) # delay between each request.
sleep(delay) # delay between each request.

return response

# print(gps_tracks_csv_upload())
# print(gps_tracks_csv_download(response_from_gps_tracks_csv_upload=gps_tracks_csv_upload(isAsync="true"), retry=5, delay=5))

print(gps_tracks_csv_upload())
print(
gps_tracks_csv_download(
response_from_gps_tracks_csv_upload=gps_tracks_csv_upload(is_async=True)
)
)

0 comments on commit d3eeb50

Please sign in to comment.