Skip to content

Commit

Permalink
Patial Refactor between Taxonomies and Companies
Browse files Browse the repository at this point in the history
  • Loading branch information
miballe committed Sep 24, 2024
1 parent 42d64de commit 81ab9bb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
44 changes: 22 additions & 22 deletions src/factiva/analytics/taxonomy/company_identifiers.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
from ..common import tools
from ..common import req
from .. import (UserKey, factiva_logger, get_factiva_logger)
from ..common import (API_COMPANIES_IDENTIFIER_TYPE, API_HOST,
API_SNAPSHOTS_COMPANIES_BASEPATH,
API_SNAPSHOTS_COMPANIES_PIT,
API_SNAPSHOTS_TAXONOMY_BASEPATH,
DOWNLOAD_DEFAULT_FOLDER,
TICKER_COMPANY_IDENTIFIER)
from ..common import log
from ..common import tools
from ..common import const
from ..auth import UserKey


class Company():
class FactivaCompany():
"""Class that represents the company available within the Snapshots API.
Parameters
Expand All @@ -29,19 +26,20 @@ class Company():
>>> c = Company(user_key=u)
"""

__API_ENDPOINT_TAXONOMY = f'{API_HOST}{API_SNAPSHOTS_TAXONOMY_BASEPATH}'
__API_ENDPOINT_COMPANY = f'{API_HOST}{API_SNAPSHOTS_COMPANIES_BASEPATH}'
__API_ENDPOINT_TAXONOMY = f'{const.API_HOST}{const.API_SNAPSHOTS_TAXONOMY_BASEPATH}'
__API_ENDPOINT_COMPANY = f'{const.API_HOST}{const.API_SNAPSHOTS_COMPANIES_BASEPATH}'
__TICKER_COMPANY_IDENTIFIER_NAME = 'ticker_exchange'

user_key=None

def __init__(self, user_key=None):
"""Class initializar"""
self.user_key = UserKey(user_key, True)
self.log= get_factiva_logger()
# self.log = log.get_factiva_logger()
self.__log = log.get_factiva_logger()


@factiva_logger
@log.factiva_logger
def get_identifiers(self) -> list:
"""Request for a list of available taxonomy categories.
Expand Down Expand Up @@ -70,7 +68,7 @@ def get_identifiers(self) -> list:
'user-key': self.user_key.key
}

endpoint = f'{common.API_HOST}{common.API_SNAPSHOTS_COMPANY_IDENTIFIERS_BASEPATH}'
endpoint = f'{const.API_HOST}{const.API_SNAPSHOTS_COMPANY_IDENTIFIERS_BASEPATH}'

response = req.api_send_request(method='GET', endpoint_url=endpoint, headers=headers_dict)

Expand All @@ -80,7 +78,7 @@ def get_identifiers(self) -> list:
raise RuntimeError('API Request returned an unexpected HTTP status')


@factiva_logger
@log.factiva_logger
def validate_point_time_request(self, identifier):
"""Validate if the user is allowes to perform company operation and if the identifier given is valid
Expand All @@ -98,9 +96,9 @@ def validate_point_time_request(self, identifier):
if (not len(self.user_key.enabled_company_identifiers)):
raise ValueError('User is not allowed to perform this operation')

tools.validate_field_options(identifier, API_COMPANIES_IDENTIFIER_TYPE)
tools.validate_field_options(identifier, const.API_COMPANIES_IDENTIFIER_TYPE)

if (identifier == TICKER_COMPANY_IDENTIFIER):
if (identifier == const.TICKER_COMPANY_IDENTIFIER):
identifier = self.__TICKER_COMPANY_IDENTIFIER_NAME

identifier_description = list(
Expand All @@ -109,7 +107,7 @@ def validate_point_time_request(self, identifier):
if (not len(identifier_description)):
raise ValueError('User is not allowed to perform this operation')

@factiva_logger
@log.factiva_logger
def point_in_time_download_all(self,
identifier,
file_name,
Expand Down Expand Up @@ -148,14 +146,14 @@ def point_in_time_download_all(self,
to_save_path = DOWNLOAD_DEFAULT_FOLDER

headers_dict = {'user-key': self.user_key.key}
endpoint = f'{self.__API_ENDPOINT_TAXONOMY}{API_SNAPSHOTS_COMPANIES_PIT}/{identifier}/{file_format}'
endpoint = f'{self.__API_ENDPOINT_TAXONOMY}{const.API_SNAPSHOTS_COMPANIES_PIT}/{identifier}/{file_format}'

local_file_name = req.download_file(endpoint, headers_dict, file_name,
file_format, to_save_path,
add_timestamp)
return local_file_name

@factiva_logger
@log.factiva_logger
def point_in_time_query(self, identifier, value) -> dict:
"""Returns the resolved Factiva code and date ranges when the instrument from the identifier, was valid.
Expand All @@ -179,7 +177,7 @@ def point_in_time_query(self, identifier, value) -> dict:

self.validate_point_time_request(identifier)
headers_dict = {'user-key': self.user_key.key}
endpoint = f'{self.__API_ENDPOINT_COMPANY}{API_SNAPSHOTS_COMPANIES_PIT}/{identifier}/{value}'
endpoint = f'{self.__API_ENDPOINT_COMPANY}{const.API_SNAPSHOTS_COMPANIES_PIT}/{identifier}/{value}'

response = req.api_send_request(endpoint_url=endpoint,
headers=headers_dict)
Expand Down Expand Up @@ -227,7 +225,7 @@ def get_single_company(self, code_type, company_code) -> pd.DataFrame:
'user-key': self.user_key.key
}

endpoint = f'{common.API_HOST}{common.API_SNAPSHOTS_COMPANIES_BASEPATH}/{code_type}/{company_code}'
endpoint = f'{const.API_HOST}{const.API_SNAPSHOTS_COMPANIES_BASEPATH}/{code_type}/{company_code}'

response = req.api_send_request(method='GET', endpoint_url=endpoint, headers=headers_dict)

Expand All @@ -237,6 +235,7 @@ def get_single_company(self, code_type, company_code) -> pd.DataFrame:

raise RuntimeError('API Request returned an unexpected HTTP status')


@log.factiva_logger
def get_multiple_companies(self, code_type, company_codes) -> pd.DataFrame:
"""
Expand Down Expand Up @@ -286,7 +285,7 @@ def get_multiple_companies(self, code_type, company_codes) -> pd.DataFrame:
}
}

endpoint = f'{common.API_HOST}{common.API_SNAPSHOTS_COMPANIES_BASEPATH}/{code_type}'
endpoint = f'{const.API_HOST}{const.API_SNAPSHOTS_COMPANIES_BASEPATH}/{code_type}'

response = req.api_send_request(method='POST', endpoint_url=endpoint, headers=headers_dict, payload=payload_dict)

Expand All @@ -295,6 +294,7 @@ def get_multiple_companies(self, code_type, company_codes) -> pd.DataFrame:
return pd.DataFrame.from_records(response_data['data']['attributes']['successes'])
raise RuntimeError(f'API Request returned an unexpected HTTP status with message: {response.text}')


@log.factiva_logger
def get_company(self, code_type, company_codes) -> pd.DataFrame:
"""Request information about either a single company or a list of companies.
Expand Down
4 changes: 3 additions & 1 deletion src/factiva/analytics/taxonomy/factiva_taxonomies.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ def __init__(self, user_key=None):
self.user_key = user_key
else:
self.user_key = UserKey(user_key)
self.log= log.get_factiva_logger()
# self.log= log.get_factiva_logger()
self.__log = log.get_factiva_logger()
self.all_subjects = None
self.all_regions = None
self.all_industries = None
Expand Down Expand Up @@ -260,6 +261,7 @@ def download_raw_category(self, category:FactivaTaxonomyCategories, path=None, f
return True


@log.factiva_logger
def lookup_code(self, code:str, category:FactivaTaxonomyCategories) -> dict:
"""
Finds the descriptor and other details based on the provide code and
Expand Down

0 comments on commit 81ab9bb

Please sign in to comment.