-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.py
More file actions
132 lines (107 loc) · 4.08 KB
/
api.py
File metadata and controls
132 lines (107 loc) · 4.08 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import requests
from os import environ
from flask import session, request
import stripe
from model import db, Customer, Order
import arrow
def split_params(param_list):
params = []
for param in param_list:
param = param.split()
for adj in ['Organic', 'Fresh', '(Frozen)', 'Pre-Washed']:
if adj in param:
param.remove(adj)
params.append(param)
return params
def get_recipes(param_list):
recipe_list = []
app_id = '9b84c19d'
app_key = 'e0c3dbf9871c26f6dbb900aaa0c71bd8'
params = []
for param in param_list:
params.append(param[-1])
param_string = '+'.join(params)
payload = {'app_id': app_id,
'app_key': app_key,
'q': param_string,
'to': 4}
r = requests.get(
"https://api.edamam.com/search",
params=payload)
try:
r.raise_for_status()
except Exception as exc:
print("There was a problem: {}".format(exc))
edamam = r.json()
recipes = edamam["hits"]
for recipe in recipes:
name = recipe["recipe"]["label"]
image = recipe["recipe"]["image"]
url = recipe["recipe"]["url"]
ingredients = recipe["recipe"]["ingredientLines"]
recipe_list.append({"name": name, "ingredients": ingredients, "image": image, "url": url})
if len(recipe_list) < 4 and len(params) > 1:
get_recipes(params[1:])
return recipe_list[:4]
def pay_for_cart():
"""Utilize stripe API"""
placed_at = arrow.utcnow()
print("placed at ", placed_at)
customer = db.session.query(Customer).filter(Customer.email == session['email']).one()
print("customer ", customer)
order = Order(customer_id=customer.user_id, placed_at=placed_at,
total=session["cart_total"], pickup_id=1) # change pickup!!!
print("order = ", order)
db.session.add(order)
print("added order")
db.session.commit()
print("committed order")
order_id = db.session.query(Order.order_id).filter(Order.customer_id == customer.user_id,
Order.placed_at == placed_at).one()
print(order_id, " is the order_id!!!!!!!!!!!!")
token = request.form.get('stripeToken')
print("token is ", token)
stripe.api_key = 'pk_test_TYooMQauvdEDq54NiTphI7jx'
try:
stripe.Charge.create(amount=int(session["cart_total"] * 100),
currency="usd",
source=token, # obtained with Stripe.js
metadata={'order_id': order_id[0],
'customer_id': customer.user_id},
statement_descriptor="Farm to Front Door CSA",
description="Farm to Front Door CSA"
)
del session['cart']
del session['cart_total']
except stripe.error.CardError as e:
# Since it's a decline, stripe.error.CardError will be caught
body = e.json_body
err = body['error']
print("Status is: %s" % e.http_status)
print("Type is: %s" % err['type'])
print("Code is: %s" % err['code'])
# param is '' in this case
print("Param is: %s" % err['param'])
print("Message is: %s" % err['message'])
except stripe.error.RateLimitError as e:
# Too many requests made to the API too quickly
pass
except stripe.error.InvalidRequestError as e:
# Invalid parameters were supplied to Stripe's API
pass
except stripe.error.AuthenticationError as e:
# Authentication with Stripe's API failed
# (maybe you changed API keys recently)
pass
except stripe.error.APIConnectionError as e:
# Network communication with Stripe failed
pass
except stripe.error.StripeError as e:
# Display a very generic error to the user, and maybe send
# yourself an email
pass
except Exception as e:
# Something else happened, completely unrelated to Stripe
pass
if __name__ == "__main__":
print(get_recipes([1]))