Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
Communications Backbone System
communications-backbone
Commits
40590158
Unverified
Commit
40590158
authored
2 years ago
by
Dan Jones
Browse files
Options
Download
Email Patches
Plain Diff
refactor: change clients endpoint to marshmallow
parent
23cc052e
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
40 additions
and
41 deletions
+40
-41
endpoints/clients.py
endpoints/clients.py
+35
-36
endpoints/notify.py
endpoints/notify.py
+2
-2
endpoints/receive.py
endpoints/receive.py
+1
-1
endpoints/send.py
endpoints/send.py
+2
-2
No files found.
endpoints/clients.py
View file @
40590158
from
flask_restful
import
Resource
,
reqparse
,
abort
,
fields
,
marshal_with
from
flask_restful
import
Resource
,
request
,
abort
from
marshmallow
import
Schema
,
fields
import
json
import
os
import
random
import
string
class
ClientSchema
(
Schema
):
client_id
=
fields
.
Str
(
required
=
True
)
client_name
=
fields
.
Str
(
required
=
True
)
subscription
=
fields
.
Str
(
required
=
True
)
class
ClientsFile
:
file
=
"clients.json"
mtime
=
0
...
...
@@ -13,7 +20,6 @@ class ClientsFile:
def
__init__
(
self
):
self
.
get
()
self
.
setup_request_parser
()
def
get
(
self
):
try
:
...
...
@@ -36,7 +42,7 @@ class ClientsFile:
return
client
def
add
(
self
,
client
):
client
.
secret
=
self
.
secret
()
client
[
'
secret
'
]
=
self
.
secret
()
self
.
clients
[
client
[
"client_id"
]]
=
client
self
.
save
()
return
client
...
...
@@ -69,70 +75,63 @@ class ClientsFile:
)
return
str
(
res
)
def
setup_request_parser
(
self
):
parser
=
reqparse
.
RequestParser
()
parser
.
add_argument
(
"client_id"
,
type
=
str
,
help
=
"A unique name to identify the client"
)
parser
.
add_argument
(
"client_name"
,
type
=
str
,
help
=
"A human friendly name to identify the client"
)
parser
.
add_argument
(
"subscription"
,
type
=
str
,
help
=
"A dot delimited string identify topics to subscribe to"
,
)
self
.
parser
=
parser
def
parse
(
self
):
return
self
.
parser
.
parse_args
()
resource_fields
=
{
"client_id"
:
fields
.
String
,
"client_name"
:
fields
.
String
,
"subscription"
:
fields
.
String
,
}
clients_file
=
ClientsFile
()
# Client
class
Client
(
Resource
):
clients_file
=
None
def
__init__
(
self
):
self
.
schema
=
ClientSchema
()
self
.
clients_file
=
ClientsFile
()
def
get
(
self
,
client_id
):
client
=
clients_file
.
find
(
client_id
)
client
=
self
.
clients_file
.
find
(
client_id
)
del
client
[
'secret'
]
if
not
client
:
abort
(
404
,
message
=
"No client with id: {}"
.
format
(
client_id
))
return
client
def
delete
(
self
,
todo_id
):
client
=
clients_file
.
find
(
client_id
)
client
=
self
.
clients_file
.
find
(
client_id
)
if
not
client
:
abort
(
404
,
message
=
"No client with id: {}"
.
format
(
client_id
))
else
:
clients_file
.
remove
(
client
)
self
.
clients_file
.
remove
(
client
)
return
client
,
204
def
put
(
self
,
client_id
):
args
=
clients_file
.
parse
()
client
=
clients_file
.
find
(
client_id
)
args
=
request
.
get_json
()
errors
=
self
.
schema
.
validate
(
args
)
if
errors
:
abort
(
400
,
message
=
str
(
errors
))
client
=
self
.
clients_file
.
find
(
client_id
)
if
not
client
:
abort
(
404
,
message
=
"No client with id: {}"
.
format
(
client_id
))
else
:
client
=
clients_file
.
update
(
args
)
client
=
self
.
clients_file
.
update
(
args
)
return
client
,
201
# ClientList
class
ClientList
(
Resource
):
def
__init__
(
self
):
self
.
schema
=
ClientSchema
()
self
.
clients_file
=
ClientsFile
()
def
get
(
self
):
return
{
client_id
:
(
client
,
client
.
pop
(
"secret"
,
None
))[
0
]
for
client_id
,
client
in
clients_file
.
get
().
items
()
for
client_id
,
client
in
self
.
clients_file
.
get
().
items
()
}
def
post
(
self
):
args
=
clients_file
.
parse
()
args
=
request
.
get_json
()
errors
=
self
.
schema
.
validate
(
args
)
if
errors
:
abort
(
400
,
message
=
str
(
errors
))
client
=
clients_file
.
find
(
args
[
"client_id"
])
if
client
:
abort
(
403
,
message
=
"Duplicate client id: {}"
.
format
(
client_id
))
...
...
This diff is collapsed.
Click to expand it.
endpoints/notify.py
View file @
40590158
from
flask_restful
import
Resource
,
req
parse
,
abort
from
flask_restful
import
Resource
,
req
uest
,
abort
from
marshmallow
import
Schema
,
fields
import
json
...
...
@@ -19,7 +19,7 @@ class Notify(Resource):
def
post
(
self
):
errors
=
self
.
schema
.
validate
(
request
.
args
)
if
errors
:
abort
(
400
,
str
(
errors
))
abort
(
400
,
message
=
str
(
errors
))
messages
=
[]
allow
=
False
...
...
This diff is collapsed.
Click to expand it.
endpoints/receive.py
View file @
40590158
...
...
@@ -22,7 +22,7 @@ class Receive(Resource):
def
get
(
self
):
errors
=
self
.
schema
.
validate
(
request
.
args
)
if
errors
:
abort
(
400
,
str
(
errors
))
abort
(
400
,
message
=
str
(
errors
))
messages
=
[]
allow
=
False
...
...
This diff is collapsed.
Click to expand it.
endpoints/send.py
View file @
40590158
from
flask_restful
import
Resource
,
req
parse
,
abort
from
flask_restful
import
Resource
,
req
uest
,
abort
from
marshmallow
import
Schema
,
fields
import
json
...
...
@@ -20,7 +20,7 @@ class Send(Resource):
def
post
(
self
):
errors
=
self
.
schema
.
validate
(
request
.
args
)
if
errors
:
abort
(
400
,
str
(
errors
))
abort
(
400
,
message
=
str
(
errors
))
messages
=
[]
allow
=
False
...
...
This diff is collapsed.
Click to expand it.
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