Skip to content

Commit

Permalink
Merge pull request #6 from kruand-sesam/master
Browse files Browse the repository at this point in the history
XML to JSON now reads a base64 string and returns a JSON dict
  • Loading branch information
JonasAls89 authored Aug 10, 2021
2 parents 5d82a87 + 4f934f9 commit f5e6cdc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
FROM python:3-alpine
MAINTAINER Baard H. Rehn Johansen "ashkan.vahidishams@sesam.io"
LABEL author="Endre Brønnum"
LABEL author.email="endre.bronnum@sesam.io"

RUN apk update
RUN apk add openssl-dev libffi-dev musl-dev gcc make
Expand Down
26 changes: 15 additions & 11 deletions service/service.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import base64
import json
import xmltodict
import yaml
Expand Down Expand Up @@ -150,30 +151,33 @@ def xml_string_to_json():
logger.info("Received args: " + str(dict(request.args)) + " Preserve entity: " + str(preserve_entity))

request_payload = request.get_json()

result = []

try:
result = []

#Sesam packs entities in an array before firing off a request and expects an array back. 
for item in yield_entities(request_payload):
xmlString = item[xml_payload_node]

if(item[xml_payload_node].startswith("~b")):
xmlString = xmlString[2:] # substring starting from position 2

xmlString = base64.b64decode(xmlString).decode(xml_payload_encoding) # decode from base64 to binary, then to string

if preserve_entity is True:
#Keep all incoming properties in addition to the parsed xml
item["xml_as_json"] = xmltodict.parse(item[xml_payload_node],encoding=xml_payload_encoding, xml_attribs=True)
result.append(item)
#Keep all incoming properties in addition to the parsed xml
item["xml_as_json"] = xmltodict.parse(xmlString,encoding=xml_payload_encoding, xml_attribs=True)
result.append(item.copy())

else:
xml_as_dict = {}
xml_as_dict = preserve_sesam_special_fields(xml_as_dict, item)
xml_as_dict["xml_as_json"] = xmltodict.parse(item[xml_payload_node],encoding=xml_payload_encoding, xml_attribs=True)
xml_as_dict["xml_as_json"] = xmltodict.parse(xmlString,encoding=xml_payload_encoding, xml_attribs=True)

result.append(xml_as_dict)

json_data = json.dumps(result)
#print(json_data)
except Exception as ex:
logger.error(f"Exiting with error: {ex}")

return Response(response=json_data, mimetype='application/json')
return Response(response=json.dumps(result), mimetype='application/json')

def preserve_sesam_special_fields(target, original):
"""
Expand Down

0 comments on commit f5e6cdc

Please sign in to comment.