Skip to content

Commit

Permalink
Merge pull request #22 from Moesif/feature-update-companies
Browse files Browse the repository at this point in the history
Add: Update company and batch companies functions
  • Loading branch information
dgilling authored May 4, 2019
2 parents d084486 + b39d017 commit f8598e7
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 7 deletions.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ MOESIF_MIDDLEWARE = {
## Update User

### update_user method
A method is attached to the moesif middleware object to update the users profile or metadata.
A method is attached to the moesif middleware object to update the user profile or metadata.
The metadata field can be any custom data you want to set on the user. The `user_id` field is required.

```python
Expand All @@ -213,6 +213,35 @@ update_users = middleware.update_users_batch([{
}])
```

## Update Company

### update_company method
A method is attached to the moesif middleware object to update the company profile or metadata.
The metadata field can be any custom data you want to set on the company. The `company_id` field is required.

```python
middleware = MoesifMiddleware(None)
update_company = middleware.update_company({
'company_id': '1',
'metadata': {'email': 'abc@email.com', 'name': 'abcde', 'image': '1234'}
})
```

### update_companies_batch method
A method is attached to the moesif middleware object to update the companies profile or metadata in batch.
The metadata field can be any custom data you want to set on the company. The `company_id` field is required.

```python
middleware = MoesifMiddleware(None)
update_companies = middleware.update_companies_batch([{
'company_id': '1',
'metadata': {'email': 'abc@email.com', 'name': 'abcdefg', 'image': '123'}
}, {
'company_id': '2',
'metadata': {'email': 'abc@email.com', 'name': 'abcdefg', 'image': '123'}
}])
```

## How to test

1. Manually clone the git repo
Expand Down
9 changes: 8 additions & 1 deletion moesifdjango/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .masks import *
from .client_ip import *
from .update_users import *
from .update_companies import *
from io import BytesIO
from moesifpythonrequest.start_capture.start_capture import StartCapture
from datetime import datetime, timedelta
Expand Down Expand Up @@ -332,4 +333,10 @@ def update_user(self, user_profile):
update_user(user_profile, self.api_client, self.DEBUG)

def update_users_batch(self, user_profiles):
update_users_batch(user_profiles, self.api_client, self.DEBUG)
update_users_batch(user_profiles, self.api_client, self.DEBUG)

def update_company(self, company_profile):
update_company(company_profile, self.api_client, self.DEBUG)

def update_companies_batch(self, companies_profiles):
update_companies_batch(companies_profiles, self.api_client, self.DEBUG)
9 changes: 8 additions & 1 deletion moesifdjango/middleware_pre19.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from .masks import *
from .client_ip import *
from .update_users import *
from .update_companies import *
from io import BytesIO
from moesifpythonrequest.start_capture.start_capture import StartCapture
from datetime import datetime, timedelta
Expand Down Expand Up @@ -296,4 +297,10 @@ def update_user(self, user_profile):
update_user(user_profile, self.api_client, self.DEBUG)

def update_users_batch(self, user_profiles):
update_users_batch(user_profiles, self.api_client, self.DEBUG)
update_users_batch(user_profiles, self.api_client, self.DEBUG)

def update_company(self, company_profile):
update_company(company_profile, self.api_client, self.DEBUG)

def update_companies_batch(self, companies_profiles):
update_companies_batch(companies_profiles, self.api_client, self.DEBUG)
112 changes: 112 additions & 0 deletions moesifdjango/update_companies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
from moesifapi.models import *
from moesifapi.exceptions.api_exception import *
from moesifapi.api_helper import *


def update_company(company_profile, api_client, DEBUG):
if not company_profile:
print('Expecting the input to be either of the type - CompanyModel, dict or json while updating user')
else:
if isinstance(company_profile, dict):
if 'company_id' in company_profile:
try:
api_client.update_company(CompanyModel.from_dictionary(company_profile))
if DEBUG:
print('Company Profile updated successfully')
except APIException as inst:
if 401 <= inst.response_code <= 403:
print("Unauthorized access sending event to Moesif. Please check your Appplication Id.")
if DEBUG:
print("Error while updating company, with status code:")
print(inst.response_code)
else:
print('To update a company, a company_id field is required')

elif isinstance(company_profile, CompanyModel):
if company_profile.company_id is not None:
try:
api_client.update_company(company_profile)
if DEBUG:
print('Company Profile updated successfully')
except APIException as inst:
if 401 <= inst.response_code <= 403:
print("Unauthorized access sending event to Moesif. Please check your Appplication Id.")
if DEBUG:
print("Error while updating company, with status code:")
print(inst.response_code)
else:
print('To update a company, a company_id field is required')
else:
try:
company_profile_json = APIHelper.json_deserialize(company_profile)
if 'company_id' in company_profile_json:
try:
api_client.update_company(CompanyModel.from_dictionary(company_profile_json))
if DEBUG:
print('Company Profile updated successfully')
except APIException as inst:
if 401 <= inst.response_code <= 403:
print("Unauthorized access sending event to Moesif. Please check your Appplication Id.")
if DEBUG:
print("Error while updating company, with status code:")
print(inst.response_code)
else:
print('To update a company, a company_id field is required')
except:
print('Error while deserializing the json, please make sure the json is valid')


def update_companies_batch(companies_profiles, api_client, DEBUG):
if not companies_profiles:
print('Expecting the input to be either of the type - List of CompanyModel, dict or json while updating users')
else:
if all(isinstance(company, dict) for company in companies_profiles):
if all('company_id' in company for company in companies_profiles):
try:
batch_profiles = [CompanyModel.from_dictionary(d) for d in companies_profiles]
api_client.update_companies_batch(batch_profiles)
if DEBUG:
print('Companies Profile updated successfully')
except APIException as inst:
if 401 <= inst.response_code <= 403:
print("Unauthorized access sending event to Moesif. Please check your Appplication Id.")
if DEBUG:
print("Error while updating companies, with status code:")
print(inst.response_code)
else:
print('To update companies, an company_id field is required')

elif all(isinstance(company, CompanyModel) for company in companies_profiles):
if all(company.company_id is not None for company in companies_profiles):
try:
api_client.update_companies_batch(companies_profiles)
if DEBUG:
print('Companies Profile updated successfully')
except APIException as inst:
if 401 <= inst.response_code <= 403:
print("Unauthorized access sending event to Moesif. Please check your Appplication Id.")
if DEBUG:
print("Error while updating companues, with status code:")
print(inst.response_code)
else:
print('To update companies, a company_id field is required')
else:
try:
company_profiles_json = [APIHelper.json_deserialize(d) for d in companies_profiles]
if all(isinstance(company, dict) for company in company_profiles_json) and all(
'company_id' in company for company in company_profiles_json):
try:
batch_profiles = [CompanyModel.from_dictionary(d) for d in company_profiles_json]
api_client.update_companies_batch(batch_profiles)
if DEBUG:
print('Companies Profile updated successfully')
except APIException as inst:
if 401 <= inst.response_code <= 403:
print("Unauthorized access sending event to Moesif. Please check your Appplication Id.")
if DEBUG:
print("Error while updating companies, with status code:")
print(inst.response_code)
else:
print('To update companies, an company_id field is required')
except:
print('Error while deserializing the json, please make sure the json is valid')
6 changes: 3 additions & 3 deletions moesifdjango/update_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def update_users_batch(user_profiles, api_client, DEBUG):
batch_profiles = [UserModel.from_dictionary(d) for d in user_profiles]
api_client.update_users_batch(batch_profiles)
if DEBUG:
print('User Profile updated successfully')
print('Users Profile updated successfully')
except APIException as inst:
if 401 <= inst.response_code <= 403:
print("Unauthorized access sending event to Moesif. Please check your Appplication Id.")
Expand All @@ -81,7 +81,7 @@ def update_users_batch(user_profiles, api_client, DEBUG):
try:
api_client.update_users_batch(user_profiles)
if DEBUG:
print('User Profile updated successfully')
print('Users Profile updated successfully')
except APIException as inst:
if 401 <= inst.response_code <= 403:
print("Unauthorized access sending event to Moesif. Please check your Appplication Id.")
Expand All @@ -99,7 +99,7 @@ def update_users_batch(user_profiles, api_client, DEBUG):
batch_profiles = [UserModel.from_dictionary(d) for d in user_profiles_json]
api_client.update_users_batch(batch_profiles)
if DEBUG:
print('User Profile updated successfully')
print('Users Profile updated successfully')
except APIException as inst:
if 401 <= inst.response_code <= 403:
print("Unauthorized access sending event to Moesif. Please check your Appplication Id.")
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# Versions should comply with PEP440. For a discussion on single-sourcing
# the version across setup.py and the project code, see
# https://packaging.python.org/en/latest/single_source_version.html
version='1.5.2',
version='1.5.3',

description='Moesif Middleware for Python Django',
long_description=long_description,
Expand Down
19 changes: 19 additions & 0 deletions tests/middleware_pre19_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ def testUpdateUsersBatch(self):
'metadata': {'email': 'abc@email.com', 'name': 'abcdefg', 'image': '123'}
}])

class UpdateCompanyTest(SimpleTestCase):

def testUpdateCompany(self):
MoesifMiddlewarePre19().update_company({
'company_id': '1',
'metadata': {'email': 'abc@email.com', 'name': 'abcde', 'image': '1234'}
})

class UpdateCompaniesBatchTest(SimpleTestCase):

def testUpdateCompaniesBatch(self):
MoesifMiddlewarePre19().update_companies_batch([{
'company_id': '1',
'metadata': {'email': 'abc@email.com', 'name': 'abcdefg', 'image': '123'}
}, {
'company_id': '2',
'metadata': {'email': 'abc@email.com', 'name': 'abcdefg', 'image': '123'}
}])


class MaskTests(SimpleTestCase):
def setUp(self):
Expand Down
22 changes: 22 additions & 0 deletions tests/middleware_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,28 @@ def testUpdateUsersBatch(self):
'metadata': {'email': 'abc@email.com', 'name': 'abcdefg', 'image': '123'}
}])

class UpdateCompanyTest(SimpleTestCase):

def testUpdateCompany(self):
middleware = moesif_middleware(None)
middleware.update_company({
'company_id': '1',
'metadata': {'email': 'abc@email.com', 'name': 'abcde', 'image': '1234'}
})


class UpdateCompaniesBatchTest(SimpleTestCase):

def testUpdateCompaniesBatch(self):
middleware = moesif_middleware(None)
middleware.update_companies_batch([{
'company_id': '1',
'metadata': {'email': 'abc@email.com', 'name': 'abcdefg', 'image': '123'}
}, {
'company_id': '2',
'metadata': {'email': 'abc@email.com', 'name': 'abcdefg', 'image': '123'}
}])


class MaskTests(SimpleTestCase):
def setUp(self):
Expand Down

0 comments on commit f8598e7

Please sign in to comment.