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
beat
beat.cmdline
Commits
f02299b6
Commit
f02299b6
authored
Oct 15, 2019
by
Samuel GAIST
Browse files
[webapi] Add custom exception to generate common message
parent
6ec7fddc
Changes
1
Hide whitespace changes
Inline
Side-by-side
beat/cmdline/webapi.py
View file @
f02299b6
...
...
@@ -40,6 +40,14 @@ import simplejson as json
from
urllib.parse
import
urlparse
class
WebAPIError
(
RuntimeError
):
def
__init__
(
self
,
cmd
,
url
,
answer
):
message
=
"{cmd} error {status}: {url}
\n
Error:{text}"
.
format
(
cmd
=
cmd
,
status
=
answer
.
status_code
,
url
=
url
,
text
=
answer
.
text
)
super
().
__init__
(
message
)
class
WebAPI
(
object
):
"""Central class for all remote service related calls"""
...
...
@@ -82,7 +90,7 @@ class WebAPI(object):
# if answer.status_code < 200 or answer.status_code >= 300:
if
answer
.
status_code
not
in
[
200
,
204
]:
raise
Runtime
Error
(
"GET
error %i : %s"
%
(
answer
.
status_code
,
url
)
)
raise
WebAPI
Error
(
"GET
"
,
url
,
answer
)
return
answer
.
json
()
...
...
@@ -91,7 +99,7 @@ class WebAPI(object):
answer
=
requests
.
post
(
url
,
json
=
data
,
headers
=
self
.
_make_headers
())
if
answer
.
status_code
not
in
[
200
,
201
]:
raise
Runtime
Error
(
"POST
error %i : %s"
%
(
answer
.
status_code
,
url
)
)
raise
WebAPI
Error
(
"POST
"
,
url
,
answer
)
try
:
return
answer
.
json
()
...
...
@@ -103,7 +111,7 @@ class WebAPI(object):
answer
=
requests
.
put
(
url
,
json
=
data
,
headers
=
self
.
_make_headers
())
if
answer
.
status_code
not
in
[
200
,
204
]:
raise
Runtime
Error
(
"PUT
error %i : %s"
%
(
answer
.
status_code
,
url
)
)
raise
WebAPI
Error
(
"PUT
"
,
url
,
answer
)
try
:
return
answer
.
json
()
...
...
@@ -116,4 +124,4 @@ class WebAPI(object):
# Should respond a 204 status and an empty body
if
answer
.
status_code
not
in
[
200
,
204
]:
raise
Runtime
Error
(
"DELETE
error %i : %s"
%
(
answer
.
status_code
,
url
)
)
raise
WebAPI
Error
(
"DELETE
"
,
url
,
answer
)
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