Skip to content

Commit 5302e1f

Browse files
author
Keyur
committed
Add: Functions to update User and Company profiles
Add: Functions to update User and Company profiles Refactor: Update README.md Bump version to 1.0.5
1 parent 284875c commit 5302e1f

File tree

5 files changed

+441
-2
lines changed

5 files changed

+441
-2
lines changed

README.md

+184
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,190 @@ Type: `Boolean`
179179

180180
`LOG_BODY` is default to true, set to false to remove logging request and response body to Moesif.
181181

182+
## Update User
183+
184+
### Update A Single User
185+
Create or update a user profile in Moesif.
186+
The metadata field can be any customer demographic or other info you want to store.
187+
Only the `user_id` field is required.
188+
For details, visit the [Python API Reference](https://www.moesif.com/docs/api?python#update-a-user).
189+
190+
```python
191+
from moesif_aws_lambda.middleware import *
192+
193+
moesif_options = {
194+
'LOG_BODY': True,
195+
'DEBUG': True,
196+
}
197+
198+
# Only user_id is required.
199+
# Campaign object is optional, but useful if you want to track ROI of acquisition channels
200+
# See https://www.moesif.com/docs/api#users for campaign schema
201+
# metadata can be any custom object
202+
user = {
203+
'user_id': '12345',
204+
'company_id': '67890', # If set, associate user with a company object
205+
'campaign': {
206+
'utm_source': 'google',
207+
'utm_medium': 'cpc',
208+
'utm_campaign': 'adwords',
209+
'utm_term': 'api+tooling',
210+
'utm_content': 'landing'
211+
},
212+
'metadata': {
213+
'email': '[email protected]',
214+
'first_name': 'John',
215+
'last_name': 'Doe',
216+
'title': 'Software Engineer',
217+
'sales_info': {
218+
'stage': 'Customer',
219+
'lifetime_value': 24000,
220+
'account_owner': '[email protected]'
221+
},
222+
}
223+
}
224+
225+
update_user(user, moesif_options)
226+
```
227+
228+
### Update Users in Batch
229+
Similar to update_user, but used to update a list of users in one batch.
230+
Only the `user_id` field is required.
231+
For details, visit the [Python API Reference](https://www.moesif.com/docs/api?python#update-users-in-batch).
232+
233+
```python
234+
from moesif_aws_lambda.middleware import *
235+
236+
moesif_options = {
237+
'LOG_BODY': True,
238+
'DEBUG': True,
239+
}
240+
241+
userA = {
242+
'user_id': '12345',
243+
'company_id': '67890', # If set, associate user with a company object
244+
'metadata': {
245+
'email': '[email protected]',
246+
'first_name': 'John',
247+
'last_name': 'Doe',
248+
'title': 'Software Engineer',
249+
'sales_info': {
250+
'stage': 'Customer',
251+
'lifetime_value': 24000,
252+
'account_owner': '[email protected]'
253+
},
254+
}
255+
}
256+
257+
userB = {
258+
'user_id': '54321',
259+
'company_id': '67890', # If set, associate user with a company object
260+
'metadata': {
261+
'email': '[email protected]',
262+
'first_name': 'Mary',
263+
'last_name': 'Jane',
264+
'title': 'Software Engineer',
265+
'sales_info': {
266+
'stage': 'Customer',
267+
'lifetime_value': 48000,
268+
'account_owner': '[email protected]'
269+
},
270+
}
271+
}
272+
update_users_batch([userA, userB], moesif_options)
273+
```
274+
275+
## Update Company
276+
277+
### Update A Single Company
278+
Create or update a company profile in Moesif.
279+
The metadata field can be any company demographic or other info you want to store.
280+
Only the `company_id` field is required.
281+
For details, visit the [Python API Reference](https://www.moesif.com/docs/api?python#update-a-company).
282+
283+
```python
284+
from moesif_aws_lambda.middleware import *
285+
286+
moesif_options = {
287+
'LOG_BODY': True,
288+
'DEBUG': True,
289+
}
290+
291+
# Only company_id is required.
292+
# Campaign object is optional, but useful if you want to track ROI of acquisition channels
293+
# See https://www.moesif.com/docs/api#update-a-company for campaign schema
294+
# metadata can be any custom object
295+
company = {
296+
'company_id': '67890',
297+
'company_domain': 'acmeinc.com', # If domain is set, Moesif will enrich your profiles with publicly available info
298+
'campaign': {
299+
'utm_source': 'google',
300+
'utm_medium': 'cpc',
301+
'utm_campaign': 'adwords',
302+
'utm_term': 'api+tooling',
303+
'utm_content': 'landing'
304+
},
305+
'metadata': {
306+
'org_name': 'Acme, Inc',
307+
'plan_name': 'Free',
308+
'deal_stage': 'Lead',
309+
'mrr': 24000,
310+
'demographics': {
311+
'alexa_ranking': 500000,
312+
'employee_count': 47
313+
},
314+
}
315+
}
316+
317+
update_company(company, moesif_options)
318+
```
319+
320+
### Update Companies in Batch
321+
Similar to update_company, but used to update a list of companies in one batch.
322+
Only the `company_id` field is required.
323+
For details, visit the [Python API Reference](https://www.moesif.com/docs/api?python#update-companies-in-batch).
324+
325+
```python
326+
from moesif_aws_lambda.middleware import *
327+
328+
moesif_options = {
329+
'LOG_BODY': True,
330+
'DEBUG': True,
331+
}
332+
333+
companyA = {
334+
'company_id': '67890',
335+
'company_domain': 'acmeinc.com', # If domain is set, Moesif will enrich your profiles with publicly available info
336+
'metadata': {
337+
'org_name': 'Acme, Inc',
338+
'plan_name': 'Free',
339+
'deal_stage': 'Lead',
340+
'mrr': 24000,
341+
'demographics': {
342+
'alexa_ranking': 500000,
343+
'employee_count': 47
344+
},
345+
}
346+
}
347+
348+
companyB = {
349+
'company_id': '09876',
350+
'company_domain': 'contoso.com', # If domain is set, Moesif will enrich your profiles with publicly available info
351+
'metadata': {
352+
'org_name': 'Contoso, Inc',
353+
'plan_name': 'Free',
354+
'deal_stage': 'Lead',
355+
'mrr': 48000,
356+
'demographics': {
357+
'alexa_ranking': 500000,
358+
'employee_count': 53
359+
},
360+
}
361+
}
362+
363+
update_companies_batch([companyA, companyB], moesif_options)
364+
```
365+
182366
## Examples
183367

184368
- [A complete example is available on GitHub](https://github.com/Moesif/moesif-aws-lambda-python-example).

moesif_aws_lambda/middleware.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from moesifapi.exceptions.api_exception import *
66
from moesifapi.models import *
77
from .client_ip import ClientIp
8+
from .update_companies import Company
9+
from .update_users import User
810
from datetime import *
911
import base64
1012
import json
@@ -16,6 +18,25 @@
1618
except ImportError:
1719
from urllib.parse import urlencode
1820

21+
# Initialized the client
22+
if os.environ["MOESIF_APPLICATION_ID"]:
23+
api_client = MoesifAPIClient(os.environ["MOESIF_APPLICATION_ID"]).api
24+
else:
25+
raise Exception('Moesif Application ID is required in settings')
26+
27+
def update_user(user_profile, moesif_options):
28+
User().update_user(user_profile, api_client, moesif_options)
29+
30+
def update_users_batch(user_profiles, moesif_options):
31+
User().update_users_batch(user_profiles, api_client, moesif_options)
32+
33+
def update_company(company_profile, moesif_options):
34+
Company().update_company(company_profile, api_client, moesif_options)
35+
36+
def update_companies_batch(companies_profiles, moesif_options):
37+
Company().update_companies_batch(companies_profiles, api_client, moesif_options)
38+
39+
1940
def MoesifLogger(moesif_options):
2041
class log_data(LambdaDecorator):
2142
def __init__(self, handler):
@@ -267,7 +288,7 @@ def after(self, retval):
267288
# Send event to Moesif
268289
if self.DEBUG:
269290
print('Moesif Event Model:')
270-
pprint(self.event)
291+
print(json.dumps(self.event))
271292

272293
event_send = self.api_client.create_event(event_model)
273294
if self.DEBUG:

moesif_aws_lambda/update_companies.py

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
from moesifapi.moesif_api_client import *
2+
from moesifapi.api_helper import *
3+
from moesifapi.exceptions.api_exception import *
4+
from moesifapi.models import *
5+
6+
7+
class Company:
8+
9+
10+
def update_company(self, company_profile, api_client, moesif_options):
11+
DEBUG = moesif_options.get('DEBUG', False)
12+
if not company_profile:
13+
print('Expecting the input to be either of the type - CompanyModel, dict or json while updating company')
14+
else:
15+
if isinstance(company_profile, dict):
16+
if 'company_id' in company_profile:
17+
try:
18+
api_client.update_company(CompanyModel.from_dictionary(company_profile))
19+
if DEBUG:
20+
print('Company Profile updated successfully')
21+
except APIException as inst:
22+
if 401 <= inst.response_code <= 403:
23+
print("Unauthorized access sending event to Moesif. Please check your Appplication Id.")
24+
if DEBUG:
25+
print("Error while updating company, with status code:")
26+
print(inst.response_code)
27+
else:
28+
print('To update an company, an company_id field is required')
29+
30+
elif isinstance(company_profile, CompanyModel):
31+
if company_profile.company_id is not None:
32+
try:
33+
api_client.update_company(company_profile)
34+
if DEBUG:
35+
print('Company Profile updated successfully')
36+
except APIException as inst:
37+
if 401 <= inst.response_code <= 403:
38+
print("Unauthorized access sending event to Moesif. Please check your Appplication Id.")
39+
if DEBUG:
40+
print("Error while updating company, with status code:")
41+
print(inst.response_code)
42+
else:
43+
print('To update a company, a company_id field is required')
44+
else:
45+
try:
46+
company_profile_json = APIHelper.json_deserialize(company_profile)
47+
if 'company_id' in company_profile_json:
48+
try:
49+
api_client.update_company(CompanyModel.from_dictionary(company_profile_json))
50+
if DEBUG:
51+
print('Company Profile updated successfully')
52+
except APIException as inst:
53+
if 401 <= inst.response_code <= 403:
54+
print("Unauthorized access sending event to Moesif. Please check your Appplication Id.")
55+
if DEBUG:
56+
print("Error while updating company, with status code:")
57+
print(inst.response_code)
58+
else:
59+
print('To update a company, an company_id field is required')
60+
except:
61+
print('Error while deserializing the json, please make sure the json is valid')
62+
63+
def update_companies_batch(self, company_profiles, api_client, moesif_options):
64+
DEBUG = moesif_options.get('DEBUG', False)
65+
if not company_profiles:
66+
print('Expecting the input to be either of the type - List of CompanyModel, dict or json while updating companies')
67+
else:
68+
if all(isinstance(company, dict) for company in company_profiles):
69+
if all('company_id' in company for company in company_profiles):
70+
try:
71+
batch_profiles = [CompanyModel.from_dictionary(d) for d in company_profiles]
72+
api_client.update_companies_batch(batch_profiles)
73+
if DEBUG:
74+
print('Company Profile updated successfully')
75+
except APIException as inst:
76+
if 401 <= inst.response_code <= 403:
77+
print("Unauthorized access sending event to Moesif. Please check your Appplication Id.")
78+
if DEBUG:
79+
print("Error while updating companies, with status code:")
80+
print(inst.response_code)
81+
else:
82+
print('To update companies, an company_id field is required')
83+
84+
elif all(isinstance(company, CompanyModel) for company in company_profiles):
85+
if all(company.company_id is not None for company in company_profiles):
86+
try:
87+
api_client.update_companies_batch(company_profiles)
88+
if DEBUG:
89+
print('Company Profile updated successfully')
90+
except APIException as inst:
91+
if 401 <= inst.response_code <= 403:
92+
print("Unauthorized access sending event to Moesif. Please check your Appplication Id.")
93+
if DEBUG:
94+
print("Error while updating companies, with status code:")
95+
print(inst.response_code)
96+
else:
97+
print('To update companies, an company_id field is required')
98+
else:
99+
try:
100+
company_profiles_json = [APIHelper.json_deserialize(d) for d in company_profiles]
101+
if all(isinstance(company, dict) for company in company_profiles_json) and all(
102+
'company_id' in user for user in company_profiles_json):
103+
try:
104+
batch_profiles = [CompanyModel.from_dictionary(d) for d in company_profiles_json]
105+
api_client.update_companies_batch(batch_profiles)
106+
if DEBUG:
107+
print('Company Profile updated successfully')
108+
except APIException as inst:
109+
if 401 <= inst.response_code <= 403:
110+
print("Unauthorized access sending event to Moesif. Please check your Appplication Id.")
111+
if DEBUG:
112+
print("Error while updating companies, with status code:")
113+
print(inst.response_code)
114+
else:
115+
print('To update companies, an company_id field is required')
116+
except:
117+
print('Error while deserializing the json, please make sure the json is valid')

0 commit comments

Comments
 (0)