Skip to content

Commit

Permalink
Merge pull request #9 from OmarMAshour/labels
Browse files Browse the repository at this point in the history
api barcode first commit
  • Loading branch information
OmarMAshour committed Oct 23, 2022
2 parents e9bd61d + dda3460 commit 3845052
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions data_fetcher/fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,34 @@ def __init__(self) -> None:

api_details = dict(self.api_conf.items("Wine Info"))

self.url = api_details['url']
self.info_url = api_details['info_url']
self.barcode_url = api_details["barcode_url"]
self.api_key = api_details['api_key']

# read keys configuration for label and info sheet
self.keys_conf = configparser.RawConfigParser()
self.keys_conf.read("../conf/keys.conf")
self.label_keys = ast.literal_eval(self.keys_conf.get("Keys", "label"))

def find_article_id_by_barcode(self, barcode: str) -> str:
return self.find_by_barcode(barcode=barcode)["code"]

def find_by_barcode(self, barcode: str) -> Dict:
"""
Fetch wine brief by a barcode
"""

params = {"lang": "de", "gtin": barcode}
headers = {"APIKey": self.api_key}

response = requests.get(
self.barcode_url,
params=params,
headers=headers
)

return response.json()

def find_for_labels(self, article_ids: List[str]) -> List[Dict]:
return self.find_by_article_ids(article_ids, self.label_keys)

Expand All @@ -37,7 +57,7 @@ def find_by_article_ids(self, article_ids: List[str], keys: List[str] = None) ->
headers = {"APIKey": self.api_key}

response = requests.get(
self.url,
self.info_url,
params=params,
headers=headers
)
Expand All @@ -52,5 +72,4 @@ def find_by_article_ids(self, article_ids: List[str], keys: List[str] = None) ->

if __name__ == "__main__":
fetcher = Fetcher()
items = fetcher.find_for_labels(article_ids=["1007906010"])
print(items)
print(fetcher.find_article_id_by_barcode("3760126362587"))

0 comments on commit 3845052

Please sign in to comment.