Created
August 6, 2024 14:58
-
-
Save bennett39/86524a67756d85a0e8e3568ae03faff7 to your computer and use it in GitHub Desktop.
Code for pulling bills from Ramp
This file contains hidden or 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
import requests | |
import base64 | |
from datetime import datetime | |
def get_access_token(): | |
"""Get a client credentials token.""" | |
endpoint = "https://api.ramp.com/developer/v1/token" | |
client_id = "<client_id>" | |
client_secret = "<client_secret>" | |
secret = base64.b64encode(str.encode(f"{client_id}:{client_secret}")).decode() | |
headers = { | |
"Accept": "application/json", | |
"Authorization": f"Basic {secret}", | |
"Content-Type": "application/x-www-form-urlencoded", | |
} | |
payload = { | |
"grant_type": "client_credentials", | |
"scope": "bills:read", | |
} | |
response = requests.post( | |
endpoint, | |
headers=headers, | |
data=payload, | |
) | |
access_token = response.json()['access_token'] | |
return access_token | |
def get_bills(access_token: str): | |
"""Get a list of bills.""" | |
endpoint = "https://api.ramp.com/developer/v1/bills?page_size=10&payment_status=PAID" | |
headers = { | |
"Accept": "application/json", | |
"Authorization": f"Bearer {access_token}", | |
} | |
response = requests.get( | |
endpoint, | |
headers=headers, | |
) | |
return response.json() | |
def print_bills(bills): | |
"""Format & pretty print bills.""" | |
print(f"{'Created At':<20}{'Issued At':<20}{'Invoice No':<20}{'Vendor':<20}{'Bill Amt':<20}{'Status':<20}{'Paid Amt':<20}{'Paid At':<20}") | |
for bill in bills["data"]: | |
created_at = datetime.fromisoformat(bill["created_at"]).strftime("%Y-%m-%d") | |
issued_at = datetime.fromisoformat(bill["issued_at"]).strftime("%Y-%m-%d") | |
invoice_number = bill["invoice_number"] | |
vendor = bill["vendor"]["remote_name"][:18] | |
bill_amount = bill["amount"]["amount"] / 100 | |
status = bill["status"] | |
payment_amount, paid_at = "", "" | |
if status == "PAID": | |
payment_amount = bill["payment"]["amount"]["amount"] / 100 | |
paid_at = datetime.fromisoformat(bill["payment"]["payment_date"]).strftime("%Y-%m-%d") | |
print(f"{created_at:<20}{issued_at:<20}{invoice_number:<20}{vendor:<20}{bill_amount:<20,.2f}{status:<20}{payment_amount:<20,.2f}{paid_at:<20}") | |
access_token = get_access_token() | |
bills = get_bills(access_token) | |
print_bills(bills) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment