From a79c7b431b50d66e30274e46c1264ddfa75dc09f Mon Sep 17 00:00:00 2001 From: Joa Cortez Date: Wed, 19 Apr 2023 01:10:19 +0200 Subject: [PATCH] completed using libraries --- challenge.py | 66 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 16 deletions(-) diff --git a/challenge.py b/challenge.py index 691197f..e9383d6 100644 --- a/challenge.py +++ b/challenge.py @@ -1,9 +1,12 @@ -""" Mini-Challenge """ +""" Mini-Challenge""" +from os.path import join +from xml.etree import ElementTree as ET from twisted.internet import defer -class Adapter(): - """ Adapter class + +class Adapter: + """Adapter class Aufgabe: Asynchron XML-Daten lesen, konvertieren und zurückliefern. """ @@ -11,25 +14,56 @@ class Adapter(): def __init__(self, path): self.path = path + def _read(self, name): + with open(join(self.path, name + ".xml")) as f: + return f.read() + + def _parse(self, xml_data): + root = ET.fromstring(xml_data) + token = root.find("{*}Token").text + return token + + def _convert(self, token): + return '{"token": %s}' % token + def retrieve_token(self, name): """ - Retrieve the content from file name.xml in adapters path directory - in a synchronous way, extract the DataReceived/Token and return it - as a JSON-Object (stringified) '{"token": value}'. + Retrieve the content from file name.xml in adapters path directory + in a synchronous way, extract the DataReceived/Token and return it + as a JSON-Object (stringified) '{"token": value}'. """ - # return '{"token": 42}' - don't do this, just a test - raise NotImplementedError + try: + xml_data = self._read(name) + token = self._parse(xml_data) + return self._convert(token) + except Exception as e: + raise ValueError("Could not retrieve token: %s" % e) def retrieve_token_async(self, name): """ - Retrieve the content from file name.xml in adapters path directory - in an asynchronous way, extract the DataReceived/Token and return it - as a Deferred which ends up in a JSON-Object (as above). + Retrieve the content from file name.xml in adapters path directory + in an asynchronous way, extract the DataReceived/Token and return it + as a Deferred which ends up in a JSON-Object (as above). - see: https://twistedmatrix.com/documents/8.0.0/api/twisted.internet.defer.Deferred.html + see: https://twistedmatrix.com/documents/8.0.0/api/twisted.internet.defer.Deferred.html - Hint: The simple way is to use defer.succeed(), bonus points for doing - it with a callback chain read->parse->convert. + Hint: The simple way is to use defer.succeed(), bonus points for doing + it with a callback chain read->parse->convert. """ - #return defer.succeed('{"token": 42}') - don't do this, just a test - raise NotImplementedError + d = defer.Deferred() + + def on_success(xml_data): + try: + token = self._parse(xml_data) + converted = self._convert(token) + d.callback(converted) + except Exception as e: + d.errback(e) + + def on_error(error): + d.errback(error) + + deferred = defer.maybeDeferred(self._read, name) + deferred.addCallbacks(on_success, on_error) + + return d