def _send_contacts(self): self.processed_contacts = {} for contact in self.contacts_for_update: # better ask for forgiveness than permission try: self.processed_contacts[contact["contact_id"]] except KeyError: contact_payload = json.dumps( {"properties": [{"property": "ML", "value": contact["prediction"]}]} ) url = f"{self.contact_post_api_url}vid/{contact['contact_id']}/profile?" access_token = self.redis_client.get("access_token").decode("utf-8") headers = {"Authorization": f"Bearer {access_token}"} res = requests.post(url=url, headers=headers, data=contact_payload) logger.info( msg=f"status_code: {res.status_code}", extra={"full_msg": res.text} ) if res.status_code == 429: logger.error( msg="429 - Ten sencondly rolling limit reached", extra={"full_msg": res.text}, ) contact_wrapper = {self.queue_to_read: [contact]} self.redis_writer(contact_wrapper) self.processed_contacts[contact["contact_id"]] = True
def _extract_contact_main_info(self, contact_id, contact_properties): try: contact = { "contact_id": contact_id, "País de interesse": contact_properties["country_of_interest"]["value"], "Qual a duração do seu intercâmbio?": contact_properties["program_duration"]["value"], "Idade": int(contact_properties["idade"]["value"]), "Associated Deals": int(contact_properties["num_associated_deals"]["value"]), "Number of times contacted": int(contact_properties["num_contacted_notes"]["value"]), "Number of Sales Activities": int(contact_properties["num_notes"]["value"]), "Number of Unique Forms Submitted": int(contact_properties["num_unique_conversion_events"] ["value"]), "Number of Form Submissions": int(contact_properties["num_conversion_events"]["value"]), } self._convert_program_duration_to_int(contact) return contact except (KeyError, TypeError) as err: logger.error( msg="Contact is not predictable due to misinformation", extra={"Full msg error": err}, ) return None
def wrapper(*args, **kwargs): for retry_count in range(max_tries): try: return func(*args, **kwargs) except (ConnectionError, TimeoutError, WatchError): logger.warning( "Failed to execute redis trying again.", extra={ "retries": retry_count, "function": func.__name__, "function_arguments": args, }, exc_info=True, ) time.sleep(random.uniform(0.2, 1)) except Exception: logger.error( msg="Error to execute redis command.", extra={ "function": func.__name__, "function_arguments": args }, exc_info=True, ) raise logger.error( msg="Error to execute redis command. Connection lost.", extra={"function": func.__name__}, exc_info=True, ) raise RedisConnectionLost
def _fetch_deal_info(self, deal_id): """ Fetches a deal information from Hubspot database""" url = self._build_url(self.deal_api_url, deal_id) deal = self._request(url) try: contacts_id = deal["associations"]["associatedVids"] if contacts_id: self.contacts_id_to_fetch = self.contacts_id_to_fetch + contacts_id except (KeyError, TypeError) as err: logger.error( msg="Deal with no contact associated", extra={"full_msg_error": err}, )
def _request(self, url): """Sends a http request to HubSpot""" access_token = self.redis_client.get("access_token").decode("utf-8") headers = {"Authorization": f"Bearer {access_token}"} response = requests.get(url, headers=headers) if response.status_code == 200: return json.loads(response.text) # Add contacts to redis again if response.status_code == 429: logger.error( msg="429 - Ten sencondly rolling limit reached", extra={"full_msg": response.text}, ) self._add_items_to_redis_again(url) return None
def _convert_strings_to_int(self): try: for column in self.reference_dicts.keys(): for label in self.reference_dicts[column].keys(): self.dataframe.loc[ self.dataframe.loc[self.dataframe[column] == label].index, column, ] = self.reference_dicts[column][label] return True except KeyError as err: logger.error( msg="Unknown column. It's not possible to generate predictions", extra={"Full msg error": err}, ) return None