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
memoriav
Memobase 2020
services
Import Process
Import API
Commits
d7c25564
Commit
d7c25564
authored
Jun 29, 2020
by
Lionel Walter
Browse files
Refactor Drupal Write
parent
a73d29b0
Changes
2
Hide whitespace changes
Inline
Side-by-side
import_api_app/models/Drupal.py
0 → 100644
View file @
d7c25564
from
import_api_app.models.Error
import
ImportApiError
from
flask_restful
import
current_app
import
requests
import
json
def
write_results
(
job_log_drupal_uuid
,
report
):
headers
=
{
'Content-Type'
:
'application/vnd.api+json'
,
'Accept'
:
'application/vnd.api+json'
,
'X-API-Key'
:
current_app
.
config
[
'drupal-api-key'
]
}
data
=
{
"data"
:
{
"id"
:
job_log_drupal_uuid
,
"type"
:
"paragraph--job_log_result"
,
"attributes"
:
{
"field_summary"
:
report
}
}
}
url
=
current_app
.
config
[
'DRUPAL_API_URL'
]
+
\
'/paragraph/job_log_result/'
+
job_log_drupal_uuid
try
:
response
=
requests
.
patch
(
url
,
headers
=
headers
,
data
=
json
.
dumps
(
data
)
)
except
requests
.
exceptions
.
RequestException
:
message
=
"It was not possible to write to Drupal API
\
via the following url "
+
url
current_app
.
logger
.
error
(
message
)
raise
ImportApiError
(
message
)
if
response
.
status_code
==
200
:
return
{
'message'
:
'Updated: '
+
url
}
elif
response
.
status_code
==
403
:
message
=
"Not authorized to write to: "
+
url
current_app
.
logger
.
error
(
message
)
raise
ImportApiError
(
message
)
elif
response
.
status_code
==
404
:
message
=
'Not Found: '
+
url
current_app
.
logger
.
error
(
message
)
raise
ImportApiError
(
message
)
else
:
message
=
"Unknown response status code for drupal api for url "
+
url
current_app
.
logger
.
error
(
message
)
raise
ImportApiError
(
message
)
import_api_app/resources/WriteJobResultsToDrupal.py
View file @
d7c25564
from
flask_restful
import
Resource
,
current_app
from
flask_restful
import
Resource
import
requests
import
json
from
import_api_app.models.Drupal
import
write_results
from
import_api_app.models.Error
import
ImportApiError
class
WriteJobResultToDrupal
(
Resource
):
class
WriteJobResultToDrupal
(
Resource
):
...
@@ -26,47 +27,12 @@ class WriteJobResultToDrupal(Resource):
...
@@ -26,47 +27,12 @@ class WriteJobResultToDrupal(Resource):
responses:
responses:
200:
200:
description: It was successful
description: It was successful
404:
description: No log result with such a uuid
500:
500:
description: There was a problem
description: There was a problem
"""
"""
headers
=
{
'Content-Type'
:
'application/vnd.api+json'
,
'Accept'
:
'application/vnd.api+json'
,
'X-API-Key'
:
current_app
.
config
[
'drupal-api-key'
]
}
data
=
{
"data"
:
{
"id"
:
job_log_drupal_uuid
,
"type"
:
"paragraph--job_log_result"
,
"attributes"
:
{
"field_summary"
:
report
}
}
}
url
=
current_app
.
config
[
'DRUPAL_API_URL'
]
+
\
'/paragraph/job_log_result/'
+
job_log_drupal_uuid
try
:
try
:
response
=
requests
.
patch
(
return
write_results
(
job_log_drupal_uuid
,
report
)
url
,
except
ImportApiError
as
e
:
headers
=
headers
,
return
{
'error'
:
e
.
message
},
500
data
=
json
.
dumps
(
data
)
)
except
requests
.
exceptions
.
RequestException
:
current_app
.
logger
.
error
(
"It was not possible to write to Drupal API via the following url "
+
url
)
return
{
'error'
:
'It was not possible to write to Drupal
\
API via the following url'
+
url
},
500
if
response
.
status_code
==
200
:
return
{
'message'
:
'Updated: '
+
url
},
200
elif
response
.
status_code
==
404
:
return
{
'message'
:
'Not Found: '
+
url
},
404
else
:
return
{
'error'
:
'It was not possible to write to Drupal API
\
via the following url'
+
url
},
500
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