-
Notifications
You must be signed in to change notification settings - Fork 0
/
hubspot_client.py
103 lines (93 loc) · 3.67 KB
/
hubspot_client.py
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
import requests
import json
class HubSpotClient:
def __init__(self, api_token, account_id):
self.api_token = api_token
self.account_id = account_id
self.contact_url = "https://api.hubapi.com/crm/v3/objects/contacts?count=100"
self.company_url = "https://api.hubapi.com/crm/v3/objects/companies?count=100"
self.deal_url = "https://api.hubapi.com/deals/v1/deal"
self.pipeline_url = "https://api.hubapi.com/crm-pipelines/v1/pipelines/deals"
self.headers = headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {self.api_token}'
}
def create_contact(self, email, firstname, lastname):
data = {
"properties": {
"email": email,
"firstname": firstname,
"lastname": lastname
}
}
response = requests.post(self.contact_url, headers=self.headers, json=data)
if response.json().get("id"):
return response.json().get("id")
else:
raise Exception(response.content)
def create_company(self, name, domain):
data = {
"properties": {
"name": name,
"domain": domain
}
}
response = requests.post(self.company_url, headers=self.headers, json=data)
if response.json().get("id"):
return response.json().get("id")
else:
raise Exception(response.content)
def create_deal(self, deal_name, deal_stage, pipeline_id, company_id, contact_id):
data = {
"properties": [
{
"name": "dealname",
"value": deal_name
},
{
"name": "dealstage",
"value": deal_stage
},
{
"name": "pipeline",
"value": pipeline_id
}
],
"associations": {
"associatedCompanyIds": [int(company_id)],
"associatedVids": [int(contact_id)]
}
}
response = requests.post(self.deal_url, headers=self.headers, json=data)
deal_id = response.json().get("dealId")
if deal_id:
return deal_id
else:
raise Exception(response.content)
def get_contact(self):
response = requests.get(self.contact_url, headers=self.headers)
results = response.json().get("results")
next_page = response.json().get("paging")
while next_page:
response = requests.get(next_page.get("next").get("link"), headers=self.headers)
results.extend(response.json().get("results"))
next_page = response.json().get("paging")
return results
def get_company(self):
response = requests.get(self.company_url, headers=self.headers)
results = response.json().get("results")
next_page = response.json().get("paging")
while next_page:
response = requests.get(next_page.get("next").get("link"), headers=self.headers)
results.extend(response.json().get("results"))
next_page = response.json().get("paging")
return results
def get_pipeline(self):
response = requests.get(self.pipeline_url, headers=self.headers)
data = response.json().get("results")[2]
pipeline_id = data.get("pipelineId")
stages = [{"stage_id": s.get("stageId"), "label": s.get("label")} for s in data.get("stages")]
return {
"pipeline_id": pipeline_id,
"stages": stages
}