-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathanalyze-mapbox
More file actions
executable file
·48 lines (39 loc) · 1.36 KB
/
analyze-mapbox
File metadata and controls
executable file
·48 lines (39 loc) · 1.36 KB
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
#!/usr/bin/env python3
import json
import os
import requests
import sys
def analyze(token):
"""
Checks if a Mapbox access token is valid by attempting to fetch a map tile.
"""
results = {
"valid": False,
"analysis": {},
}
url = f"https://api.mapbox.com/v4/mapbox.mapbox-streets-v8/0/0/0.mvt?access_token={token}"
try:
resp = requests.get(url, timeout=5)
results["analysis"]["status_code"] = resp.status_code
if resp.status_code == 200:
results["valid"] = True
results["analysis"][
"details"
] = "Token is active and can be used to fetch map resources."
elif resp.status_code == 401:
results["valid"] = False
results["analysis"]["details"] = "Token is invalid or has been revoked."
else:
results["analysis"]["details"] = "Received an unexpected status code."
results["analysis"]["raw"] = resp.text[:128]
except requests.exceptions.RequestException as e:
results["error"] = {"message": f"Request failed: {e}"}
return results
if __name__ == "__main__":
if len(sys.argv) != 2:
print(
f"USAGE\n\t{os.path.basename(sys.argv[0])} <mapbox-token>", file=sys.stderr
)
sys.exit(1)
output = analyze(sys.argv[1])
print(json.dumps(output, indent=2))