Skip to content

Commit

Permalink
Merge pull request #8 from OmarMAshour/ClosingDayOne
Browse files Browse the repository at this point in the history
end of day 1
  • Loading branch information
OmarMAshour committed Oct 22, 2022
2 parents 99c7575 + 45bc15a commit 32343c0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
56 changes: 56 additions & 0 deletions coop_challenge/fetcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
Fetcher module, this is where all the data fetch from Wine API happens
"""

import requests
import configparser
from typing import List, Dict
import ast


class Fetcher:

def __init__(self) -> None:
# read api configuration (secret)
self.api_conf = configparser.RawConfigParser()
self.api_conf.read("../conf/api.conf")

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

self.url = api_details['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_for_labels(self, article_ids: List[str]) -> List[Dict]:
return self.find_by_article_ids(article_ids, self.label_keys)

def find_by_article_ids(self, article_ids: List[str], keys: List[str] = None) -> List[Dict]:
"""
Fetch a list of wine info by a list of article_ids
"""

params = {"codes": article_ids}
headers = {"APIKey": self.api_key}

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

products = response.json()["products"]

if keys is None:
return products

return [{key: product[key] for key in keys} for product in products]


def fetchData(article_id):
fetcher = Fetcher()
items = fetcher.find_for_labels(article_ids=[article_id])
return items[0]
2 changes: 1 addition & 1 deletion coop_challenge/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ def viewIndex(request):
tryPDF()
return FileResponse(open(tryPDF(), 'rb'), content_type='application/pdf')

# return render(request, 'coop_challenge/home.html', {})
# return render(request, 'coop_challenge/base.html', {})

0 comments on commit 32343c0

Please sign in to comment.