-
-
Save Greg-Fordyce/0cc9b549425634b8713670034354f40d to your computer and use it in GitHub Desktop.
A simple example showing how to request details for a company and create a contact using the FreeAgent API in Python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a simple example showing how to connect to the FreeAgent API in | |
# Python, request a contact list. | |
import requests | |
import json | |
ACCESS_TOKEN = 'your access token goes here' | |
## Helper Methods | |
class FreeAgentRequest: | |
""" A request against the FreeAgent API. """ | |
# Public: Initialize a request | |
# This class uses the requests module. | |
# (http://docs.python-requests.org/en/latest/) | |
# | |
# resource - the name of the resource you are requesting | |
# (anything after the /v2) | |
# api_token - your access token | |
# sandbox - whether to use the API sandbox. Defaults to true. | |
def __init__(self, resource, api_token, sandbox=True): | |
if sandbox: | |
self.url = "https://api.sandbox.freeagent.com/v2/{resource}".format(resource=resource) | |
else: | |
self.url = "https://api.freeagent.com/v2/{resource}".format(resource=resource) | |
self.headers = { | |
"Content-Type": "application/json", | |
"Accept": "application/json", | |
"Authorization": "Bearer {token}".format(token=api_token) | |
} | |
# Public: get a record | |
# | |
# id - the ID of the record you want to retrieve | |
# | |
# Returns a FreeAgentResponse | |
def get(self, id=None): | |
url = self.url | |
if id is not None: | |
url = "{url}/{record}".format(url=self.url, record=id) | |
return FreeAgentResponse(requests.get(self.url, headers=self.headers)) | |
# Public: creates a new record | |
# | |
# record - the data for the record you want to create | |
# | |
# Returns a FreeAgentResponse | |
def post(self, record): | |
return FreeAgentResponse(requests.post(self.url, data=json.dumps(record), headers=self.headers)) | |
class FreeAgentResponse: | |
""" A response from the FreeAgent API. """ | |
# Private: Initialize a response. | |
# This is called by the FreeAgentRequest class when returning a response. | |
# | |
# response - a response from the Requests module. | |
def __init__(self, response): | |
self.response = response.json() | |
# Public: format the JSON response in a pretty format for output. | |
# | |
# sort_keys - order the keys by name. Defaults to true. | |
# indent - the number of spaces to indent nested levels by. Defaults to 2. | |
# separators - a tuple of separators for consecutive keys and keys/values. | |
# | |
# These three arguments are passed to the underlying JSON module. | |
# | |
# Returns a string | |
def pretty_print(self, sort_keys=True, indent=2, separators=(',', ': ')): | |
return json.dumps(self.response, sort_keys=sort_keys, indent=indent, separators=separators) | |
def contactList(contact): #List of contacts in FA | |
clist = [] | |
contact = vars(contact) | |
contact = contact['response'] | |
contact = contact['contacts'] | |
for contacts in contact: | |
try: | |
clist.append(contacts['organisation_name']) | |
except: | |
pass | |
return(clist) | |
## Requesting Contacts Information | |
print("Showing Contacts Information\n") | |
# Call out to FreeAgent | |
response = FreeAgentRequest('contacts', ACCESS_TOKEN).get() | |
print(response.pretty_print()) | |
# Print list of contacts | |
cList = contactList(response) | |
print(cList) | |
""" | |
## Create a Contact | |
print("\n\nCreating a Contact\n") | |
# Set up the contact | |
contact = { | |
"contact": { | |
"first_name": "Walter", | |
"last_name": "White", | |
"email": "walter.white@a1carwash.example.com", | |
"town": "Alberquerque", | |
"region": "New Mexico", | |
"country": "United States" | |
} | |
} | |
# Call out to FreeAgent | |
response = FreeAgentRequest('contacts', ACCESS_TOKEN).post(contact) | |
print(response.pretty_print()) | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment