-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintercom-companies.py
169 lines (148 loc) · 5.18 KB
/
intercom-companies.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# ---
# name: intercom-companies
# deployed: true
# config: index
# title: Intercom Companies
# description: Returns a list of companies from Intercom
# params:
# - name: properties
# type: array
# description: The properties to return (defaults to all properties). See "Returns" for a listing of the available properties.
# required: false
# - name: filter
# type: string
# description: Filter to apply with key/values specified as a URL query string where the keys correspond to the properties to filter.
# required: false
# returns:
# - name: id
# type: string
# description: The unique identifier for the company which is given by Intercom
# - name: company_id
# type: string
# description: The company id you have defined for the company
# - name: name
# type: string
# description: The name of the company
# - name: created_at
# type: string
# description: The time the company was added to Intercom
# - name: remote_created_at
# type: string
# description: The time the company was created by the user business
# - name: updated_at
# type: string
# description: The last time the company was updated
# - name: last_request_at
# type: string
# description: The time the company last recorded making a request
# - name: session_count
# type: integer
# description: How many sessions the company has recorded
# - name: monthly_spend
# type: integer
# description: How much revenue the company generates for the user business
# - name: user_count
# type: integer
# description: The number of users in the company
# - name: size
# type: integer
# description: The number of employees in the company
# - name: website
# type: string
# description: The URL for the company website
# - name: industry
# type: string
# description: The industry that the company operates in
# examples:
# - '""'
# - '"compay_id, name"'
# ---
import json
import urllib
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
from datetime import *
from decimal import *
from collections import OrderedDict
# main function entry point
def flexio_handler(flex):
flex.output.content_type = 'application/x-ndjson'
for data in get_data(flex.vars):
flex.output.write(data)
def get_data(params):
# get the api key from the variable input
auth_token = dict(params).get('intercom_connection',{}).get('access_token')
# see here for more info:
# https://developers.intercom.com/intercom-api-reference/reference#company-model
# https://developers.intercom.com/intercom-api-reference/reference#pagination
headers = {
'Accept': 'application/json',
'Authorization': 'Bearer ' + auth_token,
'Intercom-Version': '2.0' # api version
}
url = 'https://api.intercom.io/companies'
page_size = 50
url_query_params = {"per_page": page_size}
url_query_str = urllib.parse.urlencode(url_query_params)
page_url = url + '?' + url_query_str
while True:
response = requests_retry_session().get(page_url, headers=headers)
response.raise_for_status()
content = response.json()
data = content.get('data',[])
if len(data) == 0: # sanity check in case there's an issue with cursor
break
buffer = ''
for item in data:
item = get_item_info(item)
buffer = buffer + json.dumps(item, default=to_string) + "\n"
yield buffer
page_url = content.get('pages',{}).get('next')
if page_url is None:
break
def requests_retry_session(
retries=3,
backoff_factor=0.3,
status_forcelist=(429, 500, 502, 503, 504),
session=None,
):
session = session or requests.Session()
retry = Retry(
total=retries,
read=retries,
connect=retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
return session
def to_date(ts):
if ts is None or ts == '':
return ''
return datetime.utcfromtimestamp(int(ts)/1000).strftime('%Y-%m-%dT%H:%M:%S')
def to_string(value):
if isinstance(value, (date, datetime)):
return value.isoformat()
if isinstance(value, (Decimal)):
return str(value)
return value
def get_item_info(item):
# map this function's property names to the API's property names
info = OrderedDict()
info['id'] = item.get('id')
info['company_id'] = item.get('company_id')
info['name'] = item.get('name')
info['created_at'] = to_date(item.get('created_at'))
info['remote_created_at'] = to_date(item.get('remote_created_at'))
info['updated_at'] = to_date(item.get('updated_at'))
info['last_request_at'] = to_date(item.get('last_request_at'))
info['session_count'] = item.get('session_count')
info['monthly_spend'] = item.get('monthly_spend')
info['user_count'] = item.get('user_count')
info['size'] = item.get('size')
info['website'] = item.get('website')
info['industry'] = item.get('industry')
return info