Chapter 4. Example Script

#!/usr/bin/env python
from __future__ import print_function
import sys
import requests
from datetime import datetime, timedelta

API_HOST = 'https://access.redhat.com/product-life-cycles/api/v1'


def get_data(query):

    full_query = API_HOST + query
    r = requests.get(full_query)

    if r.status_code != 200:
        print('ERROR: Invalid request; returned {} for the following '
              'query:\n{}'.format(r.status_code, full_query))
        sys.exit(1)

    if not r.json():
        print('No data returned with the following query:')
        print(full_query)
        sys.exit(0)

    return r.json()

# Get RHEL and Openshift Container Platform 4 life cycle data
endpoint = '/products'
params = 'name=Red Hat Enterprise Linux,Openshift Container Platform 4'

data = get_data(endpoint + '?' + params)
products = data['data']

for product in products:
    print(product)

# Get RHEL and Openshift Container Platform 4 life cycle data using legacy JSON endpoint
endpoint = '/plccapi/lifecycle.json'
params = 'products=Red Hat Enterprise Linux,Openshift Container Platform 4'

data = get_data(endpoint + '?' + params)

for product in data:
    print(product)

print('-----')