-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathramp_api.py
More file actions
116 lines (76 loc) · 2.6 KB
/
Copy pathramp_api.py
File metadata and controls
116 lines (76 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import requests
import base64
from config import (
CLIENT_ID,
CLIENT_SECRET,
BASE_URL
)
class RampAPI:
def __init__(self):
self.token = None
def authenticate(self):
auth = f"{CLIENT_ID}:{CLIENT_SECRET}"
encoded = base64.b64encode(
auth.encode()
).decode()
response = requests.post(
f"{BASE_URL}/developer/v1/token",
headers={
"Authorization":
f"Basic {encoded}",
"Content-Type":
"application/x-www-form-urlencoded"
},
data={
"grant_type": "client_credentials",
"scope": "bills:read bills:write entities:read vendors:read"
}
)
response.raise_for_status()
self.token = response.json()["access_token"]
return self.token
def headers(self):
if not self.token:
self.authenticate()
return {
"Authorization":
f"Bearer {self.token}",
"Content-Type":
"application/json"
}
def get_draft_bills(self):
# We will fetch all bills and filter for Drafts inside Python
all_bills = []
url = f"{BASE_URL}/developer/v1/bills/drafts?page_size=100"
while url:
response = requests.get(url, headers=self.headers())
try:
response.raise_for_status()
except requests.exceptions.HTTPError as e:
print(f"API Error fetching bills: {response.text}")
raise e
data = response.json()
all_bills.extend(data.get("data", []))
# Follow pagination to get all pages
url = data.get("page", {}).get("next")
# Return a structure that mimics the original single-page response for compatibility
return {"data": all_bills}
def delete_bill(self, bill_id):
response = requests.delete(
f"{BASE_URL}/developer/v1/bills/{bill_id}",
headers=self.headers()
)
# Ramp returns 204 No Content for successful deletion
response.raise_for_status()
return True
def create_and_pay_bill(self, bill_payload):
response = requests.post(
f"{BASE_URL}/developer/v1/bills",
headers=self.headers(),
json=bill_payload
)
try:
response.raise_for_status()
except requests.exceptions.HTTPError as e:
raise e
return response.json()