Skip to content

Instantly share code, notes, and snippets.

@kinkerl
Created February 18, 2018 14:51
Show Gist options
  • Save kinkerl/d26af4829fedf9bfd69040787b576ad2 to your computer and use it in GitHub Desktop.
Save kinkerl/d26af4829fedf9bfd69040787b576ad2 to your computer and use it in GitHub Desktop.
Connection to the divio rest API to get information about your website
import requests
import os
import json
class NullAuth(requests.auth.AuthBase):
'''force requests to ignore the ``.netrc``
Some sites do not support regular authentication, but we still
want to store credentials in the ``.netrc`` file and submit them
as form elements. Without this, requests would otherwise use the
.netrc which leads, on some sites, to a 401 error.
Use with::
requests.get(url, auth=NullAuth())
'''
def __call__(self, r):
return r
TOKEN = 'MY TOKEN' # get access token here https://control.divio.com/account/desktop-app/access-token/
PROJECT_ID = 123456 # your project ID
headers = {'Authorization': 'Basic {}'.format(TOKEN)}
################################
# USING THE SESSION OBJECT
################################
s = requests.Session()
s.trust_env=False # important!
s.headers = headers
response = s.request(
'GET',
'https://control.divio.com/api/v1/website/{}/detail/'.format(PROJECT_ID))
print json.dumps(response.json(),sort_keys=True,indent=4, )
################################
# USING REQUESTS WITH NULL AUTH
################################
response = requests.get(
'https://control.divio.com/api/v1/website/{}/detail/'.format(PROJECT_ID),
headers=headers,
auth=NullAuth())
print json.dumps(response.json(),sort_keys=True,indent=4, )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment