|
| 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