Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
StorageLeaf
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ProjectLeaf
StorageLeaf
Commits
12f99ca3
Commit
12f99ca3
authored
Jan 12, 2021
by
Robert Goldmann
Browse files
Options
Downloads
Patches
Plain Diff
split routing into specific router
parent
b7b7f397
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
src/Dependencies.py
+9
-0
9 additions, 0 deletions
src/Dependencies.py
src/main.py
+5
-42
5 additions, 42 deletions
src/main.py
src/routers/DeviceRouter.py
+34
-0
34 additions, 0 deletions
src/routers/DeviceRouter.py
src/routers/__init__.py
+0
-0
0 additions, 0 deletions
src/routers/__init__.py
with
48 additions
and
42 deletions
src/Dependencies.py
0 → 100644
+
9
−
0
View file @
12f99ca3
from
logic.databaseNew.Database
import
SessionLocal
def
get_database
():
db
=
SessionLocal
()
try
:
yield
db
finally
:
db
.
close
()
This diff is collapsed.
Click to expand it.
src/main.py
+
5
−
42
View file @
12f99ca3
import
json
import
json
from
typing
import
List
import
uvicorn
import
uvicorn
from
fastapi
import
FastAPI
,
Depends
,
HTTPException
from
fastapi
import
FastAPI
from
pydantic
import
BaseModel
from
sqlalchemy.orm
import
Session
from
logic.databaseNew
import
Models
,
Schemas
,
Crud
from
logic.databaseNew
import
Models
from
logic.databaseNew.Database
import
engine
,
SessionLocal
from
logic.databaseNew.Database
import
engine
from
routers
import
DeviceRouter
Models
.
Base
.
metadata
.
create_all
(
bind
=
engine
)
Models
.
Base
.
metadata
.
create_all
(
bind
=
engine
)
app
=
FastAPI
()
app
=
FastAPI
()
app
.
include_router
(
DeviceRouter
.
router
)
with
open
(
'
../settings.json
'
,
'
r
'
,
encoding
=
'
UTF-8
'
)
as
f
:
with
open
(
'
../settings.json
'
,
'
r
'
,
encoding
=
'
UTF-8
'
)
as
f
:
settings
=
json
.
load
(
f
)
settings
=
json
.
load
(
f
)
# Dependency
def
get_db
():
db
=
SessionLocal
()
try
:
yield
db
finally
:
db
.
close
()
class
Device
(
BaseModel
):
id
:
int
name
:
str
@app.get
(
'
/devices/
'
,
response_model
=
List
[
Schemas
.
Device
])
def
read_devices
(
skip
:
int
=
0
,
limit
:
int
=
100
,
db
:
Session
=
Depends
(
get_db
)):
return
Crud
.
get_devices
(
db
,
skip
=
skip
,
limit
=
limit
)
@app.get
(
'
/devices/{deviceId}
'
,
response_model
=
Schemas
.
Device
)
def
read_device
(
deviceId
:
int
,
db
:
Session
=
Depends
(
get_db
)):
device
=
Crud
.
get_device
(
db
,
deviceId
=
deviceId
)
if
device
is
None
:
raise
HTTPException
(
status_code
=
404
,
detail
=
'
Device not found
'
)
return
device
@app.post
(
"
/devices/
"
,
response_model
=
Schemas
.
Device
)
def
create_user
(
device
:
Schemas
.
DeviceCreate
,
db
:
Session
=
Depends
(
get_db
)):
createdDevice
=
Crud
.
get_device_by_name
(
db
,
device
.
name
)
if
createdDevice
:
raise
HTTPException
(
status_code
=
400
,
detail
=
"
Device with this name already exists
"
)
return
Crud
.
create_device
(
db
=
db
,
device
=
device
)
if
__name__
==
'
__main__
'
:
if
__name__
==
'
__main__
'
:
uvicorn
.
run
(
app
,
host
=
'
0.0.0.0
'
,
port
=
8000
)
uvicorn
.
run
(
app
,
host
=
'
0.0.0.0
'
,
port
=
8000
)
This diff is collapsed.
Click to expand it.
src/routers/DeviceRouter.py
0 → 100644
+
34
−
0
View file @
12f99ca3
from
typing
import
List
from
fastapi
import
APIRouter
,
HTTPException
,
Depends
from
sqlalchemy.orm
import
Session
from
Dependencies
import
get_database
from
logic.databaseNew
import
Schemas
,
Crud
router
=
APIRouter
(
prefix
=
'
/devices
'
,
tags
=
[
'
devices
'
],
responses
=
{
404
:
{
'
description
'
:
'
Not found
'
}},
)
@router.get
(
'
/
'
,
response_model
=
List
[
Schemas
.
Device
])
async
def
read_devices
(
skip
:
int
=
0
,
limit
:
int
=
100
,
db
:
Session
=
Depends
(
get_database
)):
return
Crud
.
get_devices
(
db
,
skip
=
skip
,
limit
=
limit
)
@router.get
(
'
/{deviceId}
'
,
response_model
=
Schemas
.
Device
,
responses
=
{
404
:
{
'
description
'
:
'
Device not found
'
}})
async
def
read_device
(
deviceId
:
int
,
db
:
Session
=
Depends
(
get_database
)):
device
=
Crud
.
get_device
(
db
,
deviceId
=
deviceId
)
if
device
is
None
:
raise
HTTPException
(
status_code
=
404
,
detail
=
'
Device not found
'
)
return
device
@router.post
(
'
/
'
,
response_model
=
Schemas
.
Device
,
responses
=
{
400
:
{
'
description
'
:
'
Device with this name already exists
'
}})
async
def
create_user
(
device
:
Schemas
.
DeviceCreate
,
db
:
Session
=
Depends
(
get_database
)):
createdDevice
=
Crud
.
get_device_by_name
(
db
,
device
.
name
)
if
createdDevice
:
raise
HTTPException
(
status_code
=
400
,
detail
=
'
Device with this name already exists
'
)
return
Crud
.
create_device
(
db
=
db
,
device
=
device
)
This diff is collapsed.
Click to expand it.
src/routers/__init__.py
0 → 100644
+
0
−
0
View file @
12f99ca3
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment