Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
project-api
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
project-api
Commits
1f92d7d4
Commit
1f92d7d4
authored
Mar 26, 2018
by
fangzhipeng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial commit
parents
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
60 additions
and
0 deletions
+60
-0
application.py
application.py
+28
-0
common.py
common.py
+7
-0
weather/__init__.py
weather/__init__.py
+0
-0
weather/service.py
weather/service.py
+25
-0
No files found.
application.py
0 → 100644
View file @
1f92d7d4
from
flask
import
Flask
from
weather.service
import
weather
app
=
Flask
(
__name__
)
class
PrefixMiddleware
(
object
):
def
__init__
(
self
,
app
,
prefix
=
''
):
self
.
app
=
app
self
.
prefix
=
prefix
def
__call__
(
self
,
environ
,
start_response
):
print
(
environ
[
'PATH_INFO'
])
if
environ
[
'PATH_INFO'
]
.
startswith
(
self
.
prefix
):
environ
[
'PATH_INFO'
]
=
environ
[
'PATH_INFO'
][
len
(
self
.
prefix
):]
environ
[
'SCRIPT_NAME'
]
=
self
.
prefix
return
self
.
app
(
environ
,
start_response
)
else
:
start_response
(
'404'
,
[(
'Content-Type'
,
'text/plain'
)])
return
[
"This url does not belong to the app."
.
encode
()]
app
.
wsgi_app
=
PrefixMiddleware
(
app
.
wsgi_app
,
prefix
=
'/api'
)
app
.
register_blueprint
(
weather
)
if
__name__
==
'__main__'
:
app
.
run
(
debug
=
True
)
\ No newline at end of file
common.py
0 → 100644
View file @
1f92d7d4
class
BaseRes
:
def
__init__
(
self
,
data
=
None
,
status
=
200
,
message
=
'OK'
):
self
.
status
=
status
self
.
message
=
message
self
.
data
=
data
\ No newline at end of file
weather/__init__.py
0 → 100644
View file @
1f92d7d4
weather/service.py
0 → 100644
View file @
1f92d7d4
from
flask
import
Blueprint
,
render_template
,
redirect
from
flask
import
request
import
requests
import
json
from
common
import
BaseRes
weather
=
Blueprint
(
'weather'
,
__name__
,
url_prefix
=
'/weather'
)
weather_url
=
'https://www.sojson.com/open/api/weather/json.shtml?city='
@
weather
.
route
(
'/'
)
def
hello_world
():
city
=
request
.
args
.
get
(
'city'
)
if
(
city
==
''
):
return
json
.
dumps
(
BaseRes
(
status
=
404
,
message
=
'城市不能为空!'
),
default
=
lambda
obj
:
obj
.
__dict__
)
response
=
requests
.
get
(
weather_url
+
city
)
response
=
json
.
loads
(
response
.
text
)
if
(
'status'
in
response
and
response
[
'status'
]
==
200
):
data
=
response
[
'data'
]
return
json
.
dumps
(
BaseRes
(
data
[
'forecast'
]),
default
=
lambda
obj
:
obj
.
__dict__
)
return
'无该城市数据,请检查参数是否正确!'
\ No newline at end of file
Write
Preview
Markdown
is supported
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