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
7b400dc7
Commit
7b400dc7
authored
4 years ago
by
Robert Goldmann
Browse files
Options
Downloads
Patches
Plain Diff
added authorization:
- create/delete device
parent
a28bddd9
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/Dependencies.py
+13
-0
13 additions, 0 deletions
src/Dependencies.py
src/main.py
+5
-3
5 additions, 3 deletions
src/main.py
src/routers/DeviceRouter.py
+7
-5
7 additions, 5 deletions
src/routers/DeviceRouter.py
with
25 additions
and
8 deletions
src/Dependencies.py
+
13
−
0
View file @
7b400dc7
from
fastapi
import
Security
,
HTTPException
from
fastapi.security
import
APIKeyHeader
from
starlette.status
import
HTTP_403_FORBIDDEN
from
logic.databaseNew.Database
import
SessionLocal
from
logic.databaseNew.Database
import
SessionLocal
...
@@ -7,3 +11,12 @@ def get_database():
...
@@ -7,3 +11,12 @@ def get_database():
yield
db
yield
db
finally
:
finally
:
db
.
close
()
db
.
close
()
API_KEY_HEADER
=
APIKeyHeader
(
name
=
'
apiKey
'
)
async
def
check_api_key
(
apiKey
:
str
=
Security
(
API_KEY_HEADER
)):
from
main
import
API_KEY
if
apiKey
!=
API_KEY
:
raise
HTTPException
(
status_code
=
HTTP_403_FORBIDDEN
,
detail
=
'
apiKey invalid
'
)
This diff is collapsed.
Click to expand it.
src/main.py
+
5
−
3
View file @
7b400dc7
...
@@ -14,13 +14,15 @@ Models.Base.metadata.create_all(bind=engine)
...
@@ -14,13 +14,15 @@ Models.Base.metadata.create_all(bind=engine)
with
open
(
'
version.json
'
,
'
r
'
,
encoding
=
'
UTF-8
'
)
as
f
:
with
open
(
'
version.json
'
,
'
r
'
,
encoding
=
'
UTF-8
'
)
as
f
:
version
=
json
.
load
(
f
)[
'
version
'
]
version
=
json
.
load
(
f
)[
'
version
'
]
with
open
(
'
../settings.json
'
,
'
r
'
,
encoding
=
'
UTF-8
'
)
as
f
:
settings
=
json
.
load
(
f
)
API_KEY
=
settings
[
'
api
'
][
'
key
'
]
app
=
FastAPI
(
title
=
Constants
.
APP_NAME
,
app
=
FastAPI
(
title
=
Constants
.
APP_NAME
,
version
=
version
[
'
name
'
],
version
=
version
[
'
name
'
],
description
=
'
The StorageLeaf API
'
)
description
=
'
The StorageLeaf API
'
)
app
.
include_router
(
DeviceRouter
.
router
)
app
.
include_router
(
DeviceRouter
.
router
)
with
open
(
'
../settings.json
'
,
'
r
'
,
encoding
=
'
UTF-8
'
)
as
f
:
settings
=
json
.
load
(
f
)
if
__name__
==
'
__main__
'
:
if
__name__
==
'
__main__
'
:
uvicorn
.
run
(
app
,
host
=
settings
[
'
server
'
][
'
listen
'
],
port
=
settings
[
'
server
'
][
'
port
'
])
uvicorn
.
run
(
app
,
host
=
settings
[
'
server
'
][
'
listen
'
],
port
=
settings
[
'
server
'
][
'
port
'
])
This diff is collapsed.
Click to expand it.
src/routers/DeviceRouter.py
+
7
−
5
View file @
7b400dc7
...
@@ -3,7 +3,7 @@ from typing import List
...
@@ -3,7 +3,7 @@ from typing import List
from
fastapi
import
APIRouter
,
HTTPException
,
Depends
from
fastapi
import
APIRouter
,
HTTPException
,
Depends
from
sqlalchemy.orm
import
Session
from
sqlalchemy.orm
import
Session
from
Dependencies
import
get_database
from
Dependencies
import
get_database
,
check_api_key
from
logic.databaseNew
import
Schemas
,
Crud
from
logic.databaseNew
import
Schemas
,
Crud
from
logic.databaseNew.Schemas
import
Status
from
logic.databaseNew.Schemas
import
Status
...
@@ -32,8 +32,9 @@ async def read_device(deviceId: int, db: Session = Depends(get_database)):
...
@@ -32,8 +32,9 @@ async def read_device(deviceId: int, db: Session = Depends(get_database)):
@router.post
(
'
/
'
,
response_model
=
Schemas
.
Device
,
@router.post
(
'
/
'
,
response_model
=
Schemas
.
Device
,
summary
=
'
Adds a new device
'
,
summary
=
'
Adds a new device
'
,
responses
=
{
400
:
{
'
description
'
:
'
Device with this name already exists
'
}})
responses
=
{
400
:
{
'
description
'
:
'
Device with this name already exists
'
}},
async
def
create_user
(
device
:
Schemas
.
DeviceCreate
,
db
:
Session
=
Depends
(
get_database
)):
dependencies
=
[
Depends
(
check_api_key
)])
async
def
create_device
(
device
:
Schemas
.
DeviceCreate
,
db
:
Session
=
Depends
(
get_database
)):
createdDevice
=
Crud
.
get_device_by_name
(
db
,
device
.
name
)
createdDevice
=
Crud
.
get_device_by_name
(
db
,
device
.
name
)
if
createdDevice
:
if
createdDevice
:
raise
HTTPException
(
status_code
=
400
,
detail
=
'
Device with this name already exists
'
)
raise
HTTPException
(
status_code
=
400
,
detail
=
'
Device with this name already exists
'
)
...
@@ -42,8 +43,9 @@ async def create_user(device: Schemas.DeviceCreate, db: Session = Depends(get_da
...
@@ -42,8 +43,9 @@ async def create_user(device: Schemas.DeviceCreate, db: Session = Depends(get_da
@router.delete
(
'
/{deviceId}
'
,
response_model
=
Status
,
@router.delete
(
'
/{deviceId}
'
,
response_model
=
Status
,
summary
=
'
Gets a specific device
'
,
summary
=
'
Gets a specific device
'
,
responses
=
{
404
:
{
'
description
'
:
'
Device not found
'
}})
responses
=
{
404
:
{
'
description
'
:
'
Device not found
'
}},
async
def
read_device
(
deviceId
:
int
,
db
:
Session
=
Depends
(
get_database
)):
dependencies
=
[
Depends
(
check_api_key
)])
async
def
delete_device
(
deviceId
:
int
,
db
:
Session
=
Depends
(
get_database
)):
device
=
Crud
.
get_device
(
db
,
deviceId
=
deviceId
)
device
=
Crud
.
get_device
(
db
,
deviceId
=
deviceId
)
if
device
is
None
:
if
device
is
None
:
raise
HTTPException
(
status_code
=
404
,
detail
=
'
Device not found
'
)
raise
HTTPException
(
status_code
=
404
,
detail
=
'
Device not found
'
)
...
...
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