loading
main.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
import json
import requests

hostname = "https://globalmagnet.amentum.space/wmm/magnetic_field"

payload = dict(
    altitude = 10, # [km]
    longitude = 100, # [deg]
    latitude = 45, 
    year = 2020.5 # decimal year, half-way through 2020
)

try: 
  response = requests.get(hostname, params=payload)
  # extract JSON payload of response as Python dictionary
  json_payload = response.json()
  # raise an Exception if we encoutnered any HTTP error codes like 404 
  response.raise_for_status()
except requests.exceptions.ConnectionError as e: 
  # handle any typo errors in url or endpoint, or just patchy internet connection
  print(e)
except requests.exceptions.HTTPError as e:  
  # handle HTTP error codes in the response
  print(e, json_payload['error'])
except requests.exceptions.RequestException as e:  
  # general error handling
  print(e, json_payload['error'])
else:
  json_payload = response.json()
  print(json.dumps(json_payload, indent=4, sort_keys=True))

Fetching token