Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Metagenomics
metagenedb
Commits
86a4744d
Commit
86a4744d
authored
Aug 26, 2019
by
Kenzo-Hugo Hillion
♻
Browse files
Add logger to requests
parent
6743c3d3
Pipeline
#13970
passed with stages
in 1 minute and 40 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
backend/metagenedb/common/utils/api/baseapi.py
View file @
86a4744d
import
logging
import
requests
from
urllib.parse
import
urljoin
logging
.
basicConfig
(
level
=
logging
.
INFO
)
_LOGGER
=
logging
.
getLogger
(
__name__
)
class
LoggedSession
(
requests
.
Session
):
def
request
(
self
,
method
,
url
,
**
kwargs
):
_LOGGER
.
info
(
f
"
{
method
.
upper
()
}
{
url
}
"
)
response
=
super
().
request
(
method
,
url
,
**
kwargs
)
_LOGGER
.
info
(
f
"STATUS CODE:
{
response
.
status_code
}
"
)
return
response
class
BaseAPI
(
object
):
BASE_URL
=
''
...
...
@@ -9,6 +22,7 @@ class BaseAPI(object):
'Content-type'
:
'application/json'
,
'Accept'
:
'*/*'
}
SESSION
=
LoggedSession
def
__init__
(
self
):
if
not
getattr
(
self
,
'base_url'
,
None
):
...
...
@@ -16,25 +30,27 @@ class BaseAPI(object):
if
not
getattr
(
self
,
'route'
,
None
):
self
.
route
=
self
.
ROUTE
self
.
url
=
urljoin
(
self
.
base_url
,
self
.
route
)
self
.
session
=
self
.
SESSION
()
self
.
session
.
headers
.
update
(
self
.
HEADERS
)
def
get_all
(
self
):
response
=
requests
.
get
(
self
.
url
)
response
=
self
.
session
.
get
(
self
.
url
)
response
.
raise_for_status
()
return
response
.
json
()
def
get
(
self
,
entry_id
):
full_url
=
urljoin
(
self
.
url
,
entry_id
)
response
=
requests
.
get
(
full_url
)
response
=
self
.
session
.
get
(
full_url
)
response
.
raise_for_status
()
return
response
.
json
()
def
post
(
self
,
data
):
response
=
requests
.
post
(
f
"
{
self
.
url
}
"
,
json
=
data
,
headers
=
self
.
HEADERS
)
response
=
self
.
session
.
post
(
f
"
{
self
.
url
}
"
,
json
=
data
)
response
.
raise_for_status
()
return
response
.
json
()
def
put
(
self
,
entry_id
,
data
):
full_url
=
urljoin
(
self
.
url
,
entry_id
)
response
=
requests
.
put
(
f
"
{
full_url
}
/"
,
json
=
data
,
headers
=
self
.
HEADERS
)
response
=
self
.
session
.
put
(
f
"
{
full_url
}
/"
,
json
=
data
)
response
.
raise_for_status
()
return
response
.
json
()
backend/metagenedb/common/utils/api/togows.py
View file @
86a4744d
...
...
@@ -11,6 +11,7 @@ class TogoWSEntry(TogoWS):
TYPE
=
'entry'
def
__init__
(
self
,
database
,
entry_format
=
'json'
):
super
().
__init__
()
self
.
database
=
database
self
.
format
=
entry_format
self
.
route
=
f
"
{
self
.
TYPE
}
/
{
self
.
database
}
/"
...
...
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment