Skip to content

Commit

Permalink
Merge pull request #4 from endbro/master
Browse files Browse the repository at this point in the history
xml string to json conversion route
  • Loading branch information
GabriellCVig authored Jul 20, 2021
2 parents 76cc181 + 45f2012 commit 7291ed6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
6 changes: 3 additions & 3 deletions service/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
xmltodict==0.11.0
xmltodict==0.12.0
requests==2.10.0
Flask==0.11
PyYAML==5.1
Flask==2.0.1
PyYAML==5.1
51 changes: 51 additions & 0 deletions service/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

app = Flask(__name__)

CONFIG = {
"default_encoding" : "utf-8"
}

##Helper function for yielding on batch fetch
def stream_json(entities):
logger.info("streaming started")
Expand Down Expand Up @@ -97,6 +101,9 @@ def get_folder():

@app.route('/', methods=["POST"])
def post():
"""
Accepts and parses args.url, then sends new request to the given url param
"""
url = request.args["url"]
xml = xmltodict.unparse(request.get_json(), pretty=True, full_document=False).encode('utf-8')
r = requests.post(url, xml)
Expand All @@ -106,5 +113,49 @@ def post():
return Response(response="Great Success!")


@app.route('/json_string_to_xml_file', methods=["POST"])
def json_string_to_xml_file():
"""
Accepts and parses args.url, then sends new request to the given url param
"""
url = request.args["url"]
xml = xmltodict.unparse(request.get_json(), pretty=True, full_document=False).encode('utf-8')
r = requests.post(url, xml)
if r.status_code != 200:
return Response(response=r.text, status=r.status_code)
else:
return Response(response="Great Success!")

@app.route('/xml_string_to_json', methods=["POST"])
def xml_string_to_json():

"""
- http request args:
xml_payload_node : what json key holds the xml string that needs convertion
xml_encoding : the encoding to use when parsing the xml data
- accepts a application/json HTTP body
sesam namespaces needs to be removed
- xml attributes will be prefixed by "@" in the json data
"""

xml_node = request.args["xml_payload_node"]
xml_encoding = request.args["xml_payload_encoding"]

if xml_encoding.strip() == "":
xml_encoding = CONFIG["default_encoding"]

request_payload = request.get_json()

try:
data_dict = xmltodict.parse(request_payload[xml_node],encoding=xml_encoding, xml_attribs=True)
json_data = json.dumps(data_dict)
except Exception as ex:

logger.error(ex)


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


if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=int(os.environ.get('PORT',5000)))

0 comments on commit 7291ed6

Please sign in to comment.