Get a new score, add a company to a portfolio

In the following example we identify the default private portfolio, and then we add a company to that portfolio.

import os
import requests
import sys

token = os.getenv('SSC_API_TOKEN')
api_url = os.getenv('API_URL', 'https://api.securityscorecard.io')

headers = {
    'Accept': 'application/json; charset=utf-8',
    'Content-Type': 'application/json',
    'Authorization': 'Token ' + token,
    'cache-control': 'no-cache',
    }

# get portfolios
url = api_url + '/portfolios'
response = requests.get(api_url + '/portfolios', headers=headers)
response.raise_for_status()

portfolios = response.json()['entries']
my_portfolio = [
    item for item in portfolios
    # My Portfolio is read_only and private
    if 'read_only' in item and item['read_only'] == True and item['privacy'] == 'private'
][0]

# add google.com to the portfolio
url = api_url + '/portfolios/' + my_portfolio['id'] + '/companies/google.com'
response = requests.put(url, headers=headers)
response.raise_for_status()

# get scorecards in My Portfolio
url = api_url + '/portfolios/' + my_portfolio['id'] + '/companies'
response = requests.get(url, headers=headers)
response.raise_for_status()

scorecards = response.json()['entries']

# check google.com is in the portfolio
scorecard = [
    item for item in scorecards
    if item['domain'] == 'google.com'
]
assert len(scorecard) == 1, 'scorecard not found in the portfolio'

print ('google.com score is ' + str(scorecard[0]['score']))

At the end of the example we assert that the company was truly added to the default private portfolio,
and print the score.