Skip to content
This repository was archived by the owner on Feb 3, 2024. It is now read-only.

Commit 91b54d2

Browse files
authored
Merge pull request #11 from devinmatte/master
Using API Keys
2 parents 887b01f + b24467a commit 91b54d2

File tree

2 files changed

+125
-82
lines changed

2 files changed

+125
-82
lines changed

README.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@ A RESTful API for interacting with CSH Quotefault, no webgui required!
1515
}
1616
```
1717

18-
## `/all`
18+
## `<api_key>/all` : `GET`
1919
**Allowed Parameters: `date`, `submitter`**
2020

2121
Example Request: ``
2222

23-
## `/random`
23+
## `<api_key>/random` : `GET`
2424
**Allowed Parameters: `date`, `submitter`**
2525

2626
Example Request: `/random?submitter=dante`
2727

28-
## `/newest`
28+
## `<api_key>/newest` : `GET`
2929
**Allowed Parameters: `date`, `submitter`**
3030

31-
### Example Request: `/newest?submitter=matted`
31+
### Example Request: `<api_key>/newest?submitter=matted`
3232
#### Output:
3333
Returns the newest result from the submitter = `matted`
3434
```json
@@ -42,5 +42,15 @@ Returns the newest result from the submitter = `matted`
4242
}
4343
```
4444

45-
## `/between/<start>/<limit>`
46-
**Allowed Parameters: None :cry:**
45+
## `<api_key>/between/<start>/<limit>` : `GET`
46+
**Allowed Parameters: None :cry:**
47+
48+
## `<api_key>/create` : `PUT`
49+
**Required Parameters: JSON Object**
50+
```json
51+
{
52+
"quote": "This is an example Quote",
53+
"submitter": "matted",
54+
"speaker": "Example Speaker"
55+
}
56+
```

quotefaultAPI/__init__.py

Lines changed: 109 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -55,102 +55,129 @@ def __init__(self, owner):
5555
self.owner = owner
5656

5757

58-
@app.route('/between/<start>/<limit>', methods=['GET'])
59-
def between(start, limit):
60-
if datetime.strptime(start, "%Y-%m-%d") < datetime.strptime(limit, "%Y-%m-%d"):
61-
quotes = Quote.query.filter(Quote.quoteTime.between(start, limit)).all()
58+
def get_metadata():
59+
uuid = str(session["userinfo"].get("sub", ""))
60+
uid = str(session["userinfo"].get("preferred_username", ""))
61+
metadata = {
62+
"uuid": uuid,
63+
"uid": uid
64+
}
65+
return metadata
66+
67+
68+
@app.route('/<api_key>/between/<start>/<limit>', methods=['GET'])
69+
def between(start, limit, api_key):
70+
if check_key(api_key):
71+
if datetime.strptime(start, "%Y-%m-%d") < datetime.strptime(limit, "%Y-%m-%d"):
72+
quotes = Quote.query.filter(Quote.quoteTime.between(start, limit)).all()
73+
return jsonify(parse_as_json(quotes))
74+
quotes = Quote.query.all
6275
return jsonify(parse_as_json(quotes))
63-
quotes = Quote.query.all
64-
return jsonify(parse_as_json(quotes))
65-
66-
67-
@app.route('/create', methods=['PUT'])
68-
def create_quote():
69-
data = json.loads(request.data.decode('utf-8'))
70-
71-
if data['quote'] and data['submitter'] and data['speaker']:
72-
quote = data['quote']
73-
submitter = data['submitter']
74-
speaker = data['speaker']
75-
76-
if Quote.query.filter(Quote.quote == quote).first() is not None:
77-
return "that quote has already been said, asshole"
78-
elif quote is '' or speaker is '':
79-
return "you didn't fill in one of your fields. You literally only had two responsibilities, and somehow" \
80-
"you fucked them up."
81-
else:
82-
new_quote = Quote(submitter=submitter, quote=quote, speaker=speaker)
83-
db.session.add(new_quote)
84-
db.session.flush()
85-
db.session.commit()
86-
87-
88-
@app.route('/all', methods=['GET'])
89-
def index():
90-
db.create_all()
91-
92-
date = request.args.get('date')
93-
submitter = request.args.get('submitter')
94-
95-
if date is not None and submitter is not None:
96-
quotes = Quote.query.filter_by(quoteTime=date, submitter=submitter).all()
76+
else:
77+
return "Invalid API Key!"
78+
79+
80+
@app.route('/<api_key>/create', methods=['PUT'])
81+
def create_quote(api_key):
82+
if check_key(api_key):
83+
data = json.loads(request.data.decode('utf-8'))
84+
85+
if data['quote'] and data['speaker']:
86+
quote = data['quote']
87+
submitter = APIKey.query.filter_by(hash=api_key).all()[0].owner
88+
speaker = data['speaker']
89+
90+
if Quote.query.filter(Quote.quote == quote).first() is not None:
91+
return "that quote has already been said, asshole"
92+
elif quote is '' or speaker is '':
93+
return "you didn't fill in one of your fields. You literally only had two responsibilities, and somehow" \
94+
"you fucked them up."
95+
else:
96+
new_quote = Quote(submitter=submitter, quote=quote, speaker=speaker)
97+
db.session.add(new_quote)
98+
db.session.flush()
99+
db.session.commit()
100+
return return_json(new_quote)
101+
else:
102+
return "Invalid API Key!"
103+
104+
105+
@app.route('/<api_key>/all', methods=['GET'])
106+
def index(api_key):
107+
if check_key(api_key):
108+
db.create_all()
109+
110+
date = request.args.get('date')
111+
submitter = request.args.get('submitter')
112+
113+
if date is not None and submitter is not None:
114+
quotes = Quote.query.filter_by(quoteTime=date, submitter=submitter).all()
115+
return jsonify(parse_as_json(quotes))
116+
117+
elif date is not None:
118+
quotes = Quote.query.filter_by(quoteTime=date).all()
119+
return jsonify(parse_as_json(quotes))
120+
121+
elif submitter is not None:
122+
quotes = Quote.query.filter_by(submitter=submitter)
123+
return jsonify(parse_as_json(quotes))
124+
125+
quotes = Quote.query.all() # collect all quote rows in the Quote db
97126
return jsonify(parse_as_json(quotes))
127+
else:
128+
return "Invalid API Key!"
98129

99-
elif date is not None:
100-
quotes = Quote.query.filter_by(quoteTime=date).all()
101-
return jsonify(parse_as_json(quotes))
102130

103-
elif submitter is not None:
104-
quotes = Quote.query.filter_by(submitter=submitter)
105-
return jsonify(parse_as_json(quotes))
131+
@app.route('/<api_key>/random', methods=['GET'])
132+
def random_quote(api_key):
133+
if check_key(api_key):
134+
date = request.args.get('date')
135+
submitter = request.args.get('submitter')
106136

107-
quotes = Quote.query.all() # collect all quote rows in the Quote db
108-
return jsonify(parse_as_json(quotes))
137+
if date is not None and submitter is not None:
138+
quotes = Quote.query.filter_by(quoteTime=date, submitter=submitter).all()
139+
random_index = random.randint(0, len(quotes))
140+
return jsonify(return_json(quotes[random_index]))
109141

142+
elif date is not None:
143+
quotes = Quote.query.filter_by(quoteTime=date).all()
144+
random_index = random.randint(0, len(quotes))
145+
return jsonify(return_json(quotes[random_index]))
110146

111-
@app.route('/random', methods=['GET'])
112-
def random_quote():
113-
date = request.args.get('date')
114-
submitter = request.args.get('submitter')
147+
elif submitter is not None:
148+
quotes = Quote.query.filter_by(submitter=submitter).all()
149+
random_index = random.randint(0, len(quotes))
150+
return jsonify(return_json(quotes[random_index]))
115151

116-
if date is not None and submitter is not None:
117-
quotes = Quote.query.filter_by(quoteTime=date, submitter=submitter).all()
152+
quotes = Quote.query.all()
118153
random_index = random.randint(0, len(quotes))
119154
return jsonify(return_json(quotes[random_index]))
155+
else:
156+
return "Invalid API Key!"
120157

121-
elif date is not None:
122-
quotes = Quote.query.filter_by(quoteTime=date).all()
123-
random_index = random.randint(0, len(quotes))
124-
return jsonify(return_json(quotes[random_index]))
125-
126-
elif submitter is not None:
127-
quotes = Quote.query.filter_by(submitter=submitter).all()
128-
random_index = random.randint(0, len(quotes))
129-
return jsonify(return_json(quotes[random_index]))
130158

131-
quotes = Quote.query.all()
132-
random_index = random.randint(0, len(quotes))
133-
return jsonify(return_json(quotes[random_index]))
159+
@app.route('/<api_key>/newest', methods=['GET'])
160+
def newest(api_key):
161+
if check_key(api_key):
162+
date = request.args.get('date')
163+
submitter = request.args.get('submitter')
134164

165+
if date is not None:
166+
return jsonify(return_json(Quote.query.order_by(Quote.id.desc()).filter_by(date=date).first()))
135167

136-
@app.route('/newest', methods=['GET'])
137-
def newest():
138-
date = request.args.get('date')
139-
submitter = request.args.get('submitter')
168+
elif submitter is not None:
169+
return jsonify(return_json(Quote.query.order_by(Quote.id.desc()).filter_by(submitter=submitter).first()))
140170

141-
if date is not None:
142-
return jsonify(return_json(Quote.query.order_by(Quote.id.desc()).filter_by(date=date).first()))
143-
144-
elif submitter is not None:
145-
return jsonify(return_json(Quote.query.order_by(Quote.id.desc()).filter_by(submitter=submitter).first()))
146-
147-
return jsonify(return_json(Quote.query.order_by(Quote.id.desc()).first()))
171+
return jsonify(return_json(Quote.query.order_by(Quote.id.desc()).first()))
172+
else:
173+
return "Invalid API Key!"
148174

149175

150176
@auth.oidc_auth
151177
@app.route('/generatekey')
152178
def generate_API_key():
153-
new_key = APIKey(str(session["userinfo"].get("preferred_username", "")))
179+
metadata = get_metadata()
180+
new_key = APIKey(metadata['uid'])
154181
db.session.add(new_key)
155182
db.session.flush()
156183
db.session.commit()
@@ -172,3 +199,9 @@ def parse_as_json(quotes, quote_json=None):
172199
for quote in quotes:
173200
quote_json.append(return_json(quote))
174201
return quote_json
202+
203+
204+
def check_key(api_key):
205+
keys = APIKey.query.filter_by(hash=api_key).all()
206+
if len(keys) > 0:
207+
return True

0 commit comments

Comments
 (0)