Python

Simple example to retrieve information about an airframe, by registration and display some of the resulting data. This sample works with Python version 3.

Code

import requests
import json

def fetch_json_from_url(url):
    try:
        # Make a GET request to the specified URL
        response = requests.get(url)

        # Check if the request was successful (status code 200)
        if response.status_code == 200:
            # Parse the response as JSON and return it
            json_data = response.json()
            return json_data
        else:
            # Print an error message if the request was not successful
            print(f"Error: Unable to fetch data. Status code: {response.status_code}")
            return None
    except Exception as e:
        # Handle any exceptions that might occur during the request
        print(f"An error occurred: {str(e)}")
        return None

# Specify the URL you want to call and your API credentials - the test credentials always return the same item
API_KEY ="TESTAPIKEY" #Replace this with your own API KEY
API_PASSWORD ="TESTAPIKEYPASSWORD" #Replace this with your own API PASSWORD
API_ACCESS_TOKEN ="TESTAPIACCESSTOKEN"  #Replace this with your own API ACCESS TOKEN
API_BASE_URL="https://www.avdelphi.com/api/1.0/";
API_ENDPOINT="airframes";
API_COMMAND="info";
REGISTRATION="HB-JNB";
ITEMLIST="type_short;type_long;manufacturer";

targetUrl = API_BASE_URL + API_ENDPOINT +".svc?api_key=" + API_KEY + "&api_access_token=" + API_ACCESS_TOKEN  + "&cmd=" + API_COMMAND + "&registration=" + REGISTRATION + "&itemlist=" + ITEMLIST;


# Call the function with the specified URL
result_json = fetch_json_from_url(targetUrl)

# Display the result
if result_json:
    print("Result JSON:")
    print(json.dumps(result_json, indent=2))


Copy to clipboard

Sample output