-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhubspot_wrapper.py
114 lines (100 loc) · 4.78 KB
/
hubspot_wrapper.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
104
105
106
107
108
109
110
111
112
113
114
import requests
import json
import logging
import re
class HubspotWrapper:
def __init__(self, api_key):
self.api_key = api_key
self.base_url = 'https://api.hubapi.com/'
def fire_get_request(self, url, filters=None):
url += '?'
if filters is not None:
for key, value in filters.iteritems():
url += '{0}={1}&'.format(key, value)
url += 'hapikey={0}'.format(self.api_key)
result = requests.get(url)
return result
@staticmethod
def convert_deal_properties_to_array_to_send(deal_properties):
array_to_send = {'properties': []}
for key, value in deal_properties.iteritems():
array_to_send['properties'].append({'name': key, 'value': value})
return array_to_send
@staticmethod
def convert_contact_properties_to_array_to_send(contact_properties):
array_to_send = {'properties': []}
for key, value in contact_properties.iteritems():
array_to_send['properties'].append({'property': key, 'value': value})
return array_to_send
def fire_put_request(self, url, array_to_send):
url += '?hapikey={0}'.format(self.api_key)
print array_to_send
headers = {'content-type': 'application/json'}
result = requests.put(url, data=json.dumps(array_to_send), headers=headers)
print result.content
return result
def fire_post_request(self, url, array_to_send):
url += '?hapikey={0}'.format(self.api_key)
print array_to_send
headers = {'content-type': 'application/json'}
result = requests.post(url, data=json.dumps(array_to_send), headers=headers)
print result.content
return result
def is_deal_in_reception_pipeline(self, deal_id):
url = self.base_url + 'deals/v1/deal/{0}'.format(deal_id)
result = self.fire_get_request(url)
if result.status_code != 200:
logging.error(result.content)
raise Exception
pipeline_id = result.json()['properties']['pipeline']['value']
return pipeline_id == '5467c9aa-d815-4855-90dd-725f4702a7f1'
def update_deal(self, deal_id, deal_properties):
url = self.base_url + 'deals/v1/deal/{0}'.format(deal_id)
array_to_send = HubspotWrapper.convert_deal_properties_to_array_to_send(deal_properties)
result = self.fire_put_request(url, array_to_send)
if result.status_code != 200:
logging.error(result.content)
def update_contact(self, contact_id, contact_properties):
url = self.base_url + 'contacts/v1/contact/vid/{0}/profile'.format(contact_id)
array_to_send = HubspotWrapper.convert_contact_properties_to_array_to_send(contact_properties)
result = self.fire_post_request(url, array_to_send)
if result.status_code != 204:
message = result.json()
logging.error(message)
def get_contact(self, contact_id):
url = self.base_url + 'contacts/v1/contact/vid/{0}/profile'.format(contact_id)
result = self.fire_get_request(url, {'property': 'contact_type'})
if result.status_code != 200:
logging.error(result.content)
raise Exception
return result.json()
def is_contact_a_trainee(self, contact_id):
contact = self.get_contact(contact_id)
return 'contact_type' in contact['properties'] and contact['properties']['contact_type']['value'] == 'Trainee'
def get_contacts(self):
contacts = {}
url = self.base_url + 'contacts/v1/lists/all/contacts/all'
filters = {'property': 'expa_id', 'property': 'expa_url'}
result = self.fire_get_request(url, filters).json()
while True:
for contact in result['contacts']:
expa_id = None
if 'expa_id' in contact['properties']:
expa_id = contact['properties']['expa_id']['value']
elif 'expa_url' in contact['properties']:
re_result = re.search('(?<=/)\d+$', contact['properties']['expa_url']['value'])
if re_result is not None:
expa_id = re_result.group(0)
if expa_id is not None:
contact_id = contact['vid']
if self.is_contact_a_trainee(contact_id):
logging.info('Adding contact {0} to the process queue...'.format(contact_id))
contacts[contact_id] = expa_id
else:
logging.info('Skipping contact {0} because he/she is not a trainee...'.format(contact_id))
if result['has-more']:
filters['vidOffset'] = result['vid-offset']
result = self.fire_get_request(url, filters).json()
else:
break
return contacts