-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEaternity_class.py
More file actions
74 lines (67 loc) · 2.56 KB
/
Eaternity_class.py
File metadata and controls
74 lines (67 loc) · 2.56 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import requests
from requests.auth import HTTPBasicAuth
from datetime import datetime
class Eaternity_class():
def __init__(self, key="SR001CHbX2t3wv0oFpA6TPWjrOUmILzR"):
self.AUTH = HTTPBasicAuth(key, "")
self.BASE_URL = "https://co2.eaternity.ch"
def create_kitchen(self, name, kitchen_id, location):
"""
Initialize a kitchen
name: name user
kitchen_id: id kitchen
location: location kitchen
"""
url = "{}/api/kitchens/{}".format(self.BASE_URL, kitchen_id)
body = {
"kitchen": {
"name": name,
"location": location
}
}
response = requests.put(url, json=body, auth=self.AUTH)
if response.status_code not in [200, 201, 202]:
error_msg = f"ERROR: Failed PUTting kitchen {kitchen_id}" + \
f" with status {response.status_code}: '{response.text}'"
print(error_msg)
return error_msg, True
else:
print(f"SUCCESS: PUT kitchen {kitchen_id}")
return response.json(), False
def put_recipe(self, recipe_id, kitchen_id,
name_pizza, list_ingredientes, location):
"""
Insert a recipe to optain info like co2
recipe_id: id recipe
kitchen_id: kitchen id
name_pizza: pizza id
list_ingredientes: list of ingredients
location: location of kitchens
"""
url = "{}/api/kitchens/{}/recipes/{}".format(self.BASE_URL,
kitchen_id, recipe_id)
body = {
"recipe": {
"titles": [
{
"language": "en",
"value": name_pizza
}
],
"date": datetime.now().strftime("%Y-%m-%d"),
"location": location,
"servings": 1,
"ingredients": list_ingredientes
}
}
response = requests.put(url, json=body, auth=self.AUTH)
if response.status_code not in [200, 201, 202]:
error_msg = f"ERROR: Failed PUTting recipe {recipe_id} with" + \
f" status {response.status_code}: '{response.text}'"
print(error_msg)
return error_msg, True
else:
print(f"SUCCESS: PUT recipe {recipe_id}")
response = response.json()
response['recipe']['ingredients'] = list_ingredientes
return response, False