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
32efc562
Commit
32efc562
authored
May 11, 2021
by
Jonas Waeber
Browse files
Add headers to institution update message.
parent
47d42f0a
Pipeline
#26212
passed with stages
in 2 minutes and 2 seconds
Changes
1
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
import_api_app/resources/UpdateInstitution.py
View file @
32efc562
import
uuid
from
flask_restful
import
Resource
,
current_app
from
kafka
import
KafkaProducer
import
requests
...
...
@@ -8,6 +10,11 @@ from requests.auth import HTTPBasicAuth
class
UpdateInstitution
(
Resource
):
def
__init__
(
self
):
self
.
producer
=
KafkaProducer
(
bootstrap_servers
=
current_app
.
config
[
'kafka-broker-url'
],
value_serializer
=
lambda
m
:
json
.
dumps
(
m
,
ensure_ascii
=
False
)
.
encode
(
'utf-8'
))
def
get
(
self
,
institution_drupal_uuid
):
"""
Update the institution with the given drupal UUID in the backend.
...
...
@@ -36,7 +43,7 @@ class UpdateInstitution(Resource):
example: the value written into the topic
"""
topic_value
=
''
result
=
''
headers
=
{
'X-API-Key'
:
current_app
.
config
[
'drupal-api-key'
]}
user
=
current_app
.
config
[
'drupal-user'
]
password
=
current_app
.
config
[
'drupal-password'
]
...
...
@@ -97,7 +104,7 @@ class UpdateInstitution(Resource):
return
{
'status'
:
'FAILURE'
,
'message'
:
str
(
ex
),
'topic_value'
:
topic_value
'topic_value'
:
result
},
500
except
Exception
as
ex
:
msg
=
f
'Unknown Exception (
{
institution_drupal_uuid
}
):
{
ex
}
\n
{
traceback
.
format_exc
()
}
'
...
...
@@ -105,9 +112,9 @@ class UpdateInstitution(Resource):
return
{
'status'
:
'FAILURE'
,
'message'
:
str
(
ex
),
'topic_value'
:
topic_value
'topic_value'
:
result
},
500
topic_value
=
{
result
=
{
'type'
:
institution_data_de
[
'type'
],
'status'
:
institution_data_de
[
'attributes'
][
'status'
],
'title_de'
:
institution_data_de
[
'attributes'
][
'title'
],
...
...
@@ -132,29 +139,31 @@ class UpdateInstitution(Resource):
institution_data_de
[
'attributes'
][
'computed_teaser_image_url'
],
'computed_teaser_color'
:
institution_data_de
[
'attributes'
][
'computed_teaser_color'
],
}
# 2. write info into kafka topic
producer_topic
=
current_app
.
config
[
'topic-drupal-export'
]
headers
=
[
(
'recordSetId'
,
bytes
(
'none'
,
encoding
=
'utf-8'
)),
(
'sessionId'
,
bytes
(
str
(
uuid
.
uuid4
()),
encoding
=
'utf-8'
)),
(
'institutionId'
,
bytes
(
result
.
get
(
'field_memobase_id'
),
encoding
=
'utf-8'
)),
(
'isPublished'
,
bytes
(
str
(
result
[
'status'
]),
encoding
=
'utf-8'
))
]
try
:
producer
=
KafkaProducer
(
bootstrap_servers
=
current_app
.
config
[
'kafka-broker-url'
],
value_serializer
=
lambda
m
:
json
.
dumps
(
m
,
ensure_ascii
=
False
)
.
encode
(
'utf-8'
))
key
=
bytes
(
topic_value
.
get
(
'field_memobase_id'
),
encoding
=
'utf-8'
)
producer
.
send
(
current_app
.
config
[
'topic-drupal-export'
],
topic_value
,
key
)
key
=
bytes
(
result
.
get
(
'field_memobase_id'
),
encoding
=
'utf-8'
)
self
.
producer
.
send
(
producer_topic
,
result
,
key
,
headers
=
headers
)
except
Exception
as
ex
:
msg
=
'
Exception
for '
+
institution_drupal_uuid
+
': '
+
str
(
ex
)
+
'
\n
'
+
\
traceback
.
format_exc
()
current_app
.
logger
.
error
(
msg
)
msg
=
f
'Unknown
Exception
(
{
institution_drupal_uuid
}
):
{
ex
}
. '
\
f
'Check logs for more details.'
current_app
.
logger
.
error
(
f
'
{
msg
}
\n
{
traceback
.
format_exc
()
}
'
)
return
{
'status'
:
'FAILURE'
,
'topic_key'
:
topic_value
.
get
(
'field_memobase_id'
),
'topic_value'
:
topic_value
'topic_key'
:
result
.
get
(
'field_memobase_id'
),
'topic_value'
:
result
},
500
current_app
.
logger
.
debug
(
'success for '
+
institution_drupal_uuid
)
return
{
'status'
:
'SUCCESS'
,
'topic_key'
:
topic_value
.
get
(
'field_memobase_id'
),
'topic_value'
:
topic_value
'topic_key'
:
result
.
get
(
'field_memobase_id'
),
'topic_value'
:
result
},
200
@
staticmethod
...
...
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