-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPIrequests.py
More file actions
57 lines (57 loc) · 2.95 KB
/
APIrequests.py
File metadata and controls
57 lines (57 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import requests
import json
import logging
import socket
#The APIrequests class is responsible for making the web requests to the provided API. As soon as an object of this
#type is instantiated, the API URL's and credentials are loaded into the memory to be used by member funtions.
#Here, self.config_obj contains the dictionary with URL's and the API key.
class APIrequests:
def __init__(self):
logging.basicConfig(filename="APIrequests.log",
format='%(asctime)s %(message)s',
filemode='a')
self.logger=logging.getLogger()
self.logger.setLevel(logging.DEBUG)
self.config = open('keys/foodtofork.json', 'r')
self.config_obj = json.load(self.config)
self.config.close()
#takes available ingredients as an input string and returns the recipe ID of the top rated recipe
#which contains the ingredients
def checkConnectivity(self):
try:
socket.create_connection(("www.google.com", 80))
self.logger.error("Network check-Passed")
return True
except OSError:
pass
self.logger.error("Network check- Failed")
return False
def foodSearch(self,ingredients,sortBy='rating'):
ingredients=ingredients.replace(',','%20')
uriString=self.config_obj['searchUri']+'?'+'key='+self.config_obj['apiKey']+'&'+'q='+ingredients+'&'+'sort='+sortBy[0]
recipes=requests.get(uriString)
if not recipes.status_code==200:
print('There was some error')
print("Response code was not 2xx, was {}".format(recipes.status_code))
self.logger.error("Response code was not 2xx, was {}".format(recipes.status_code))
else:
if len(recipes.json()['recipes'])>0:
recipe_id=recipes.json()['recipes'][0]['recipe_id']
return recipe_id
else:
print('No matching recipe found, maybe check for typos?')
self.logger.error('No matching recipes found for given combination of ingredients')
#Takes recipe ID as an input and provides complete details about the recipe includng ingredients, name, publisher etc
#Here only the name and ingredients list is returned as they are used to provide the user with the missing ingredients info
def getRecipeDetails(self,recipe_id):
if recipe_id:
uriString = self.config_obj['recepieUri']+'?'+'key='+self.config_obj['apiKey']+'&'+'rId='+str(recipe_id)
response = requests.get(uriString)
if not response.status_code==200:
print('There was some error')
self.logger.error("Response code was not 2xx, was {}".format(response.status_code))
else:
return {'ingredients': response.json()['recipe']['ingredients'],
'name':response.json()['recipe']['title'],
'responseBody':response.json()
}