Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Pump, Cedric
lab-rest
Commits
c48daf25
Commit
c48daf25
authored
Mar 28, 2021
by
CedricPump
Browse files
- tutorial step Finding Specific Resources
parent
c58048a1
Pipeline
#38133
passed with stages
in 1 minute and 9 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
api/api.py
View file @
c48daf25
...
@@ -30,9 +30,32 @@ def home():
...
@@ -30,9 +30,32 @@ def home():
<p>A prototype API for distant reading of science fiction novels.</p>'''
<p>A prototype API for distant reading of science fiction novels.</p>'''
# A route to return all of the available entries in our catalog.
@
app
.
route
(
'/api/v1/resources/books/all'
,
methods
=
[
'GET'
])
@
app
.
route
(
'/api/v1/resources/books/all'
,
methods
=
[
'GET'
])
def
api_all
():
def
api_all
():
return
jsonify
(
books
)
return
jsonify
(
books
)
app
.
run
(
host
=
"0.0.0.0"
)
\ No newline at end of file
@
app
.
route
(
'/api/v1/resources/books'
,
methods
=
[
'GET'
])
def
api_id
():
# Check if an ID was provided as part of the URL.
# If ID is provided, assign it to a variable.
# If no ID is provided, display an error in the browser.
if
'id'
in
request
.
args
:
id
=
int
(
request
.
args
[
'id'
])
else
:
return
"Error: No id field provided. Please specify an id."
# Create an empty list for our results
results
=
[]
# Loop through the data and match results that fit the requested ID.
# IDs are unique, but other fields might return many results
for
book
in
books
:
if
book
[
'id'
]
==
id
:
results
.
append
(
book
)
# Use the jsonify function from Flask to convert our list of
# Python dictionaries to the JSON format.
return
jsonify
(
results
)
app
.
run
()
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment