diff options
-rw-r--r-- | gsxws/comptia.py | 54 | ||||
-rw-r--r-- | gsxws/core.py | 38 | ||||
-rw-r--r-- | gsxws/products.py | 4 |
3 files changed, 54 insertions, 42 deletions
diff --git a/gsxws/comptia.py b/gsxws/comptia.py index 8d56fc0..02276a8 100644 --- a/gsxws/comptia.py +++ b/gsxws/comptia.py @@ -1,5 +1,6 @@ import os import json +import logging from core import GsxObject, GsxError, GsxCache @@ -33,38 +34,37 @@ GROUPS = ( class CompTIA(GsxObject): "Stores and accesses CompTIA codes." + _namespace = "glob:" + def __init__(self): """ - Initialize CompTIA symptoms from JSON file + Initialize CompTIA symptoms from the local JSON file """ - df = open(os.path.join(os.path.dirname(__file__), 'comptia.json')) - self._data = json.load(df) + self._cache = GsxCache("comptia") + #df = open(os.path.join(os.path.dirname(__file__), 'comptia.json')) + #self._data = json.load(df) def fetch(self): """ + Description: The CompTIA Codes Lookup API retrieves a list of CompTIA groups and modifiers. - Here we must resort to raw XML parsing since SUDS throws this: - suds.TypeNotFound: Type not found: 'comptiaDescription' - when calling CompTIACodes()... + Context: + The CompTIA Codes (Symptom Codes) are the current available selections based on + the component group code for parts. + The API can be executed only after valid Authentication. + Users can use the API at any point to retrieve the CompTIA code and modifier details, + in order to create or update repairs. - >>> CompTIA().fetch() - {'A': {'972': 'iPod not recognized',... + >>> CompTIA().fetch() # doctest: +ELLIPSIS + {'A': {'989': 'Remote Inoperable', ... """ - global COMPTIA_CACHE - if COMPTIA_CACHE.get("comptia"): - return COMPTIA_CACHE.get("comptia") - - CLIENT.set_options(retxml=True) - dt = CLIENT.factory.create("ns3:comptiaCodeLookupRequestType") - dt.userSession = SESSION + self._submit("ComptiaCodeLookupRequest", "ComptiaCodeLookup", "comptiaInfo", True) - try: - xml = CLIENT.service.CompTIACodes(dt) - except suds.WebFault, e: - raise GsxError(fault=e) + if self._cache.get(): + return self._cache.get() - root = ET.fromstring(xml).findall('.//%s' % 'comptiaInfo')[0] + root = self._req.objects[0] for el in root.findall(".//comptiaGroup"): group = {} @@ -73,10 +73,10 @@ class CompTIA(GsxObject): for ci in el.findall('comptiaCodeInfo'): group[ci[0].text] = ci[1].text - self.data[comp_id] = group + self._data[comp_id] = group - COMPTIA_CACHE.put("comptia", self.data) - return self.data + self._cache.set(self._data) + return self._data def symptoms(self, component=None): """ @@ -91,3 +91,11 @@ class CompTIA(GsxObject): r[g].append((k, v,)) return r[component] if component else r + +if __name__ == '__main__': + import sys + import doctest + from core import connect + logging.basicConfig(level=logging.DEBUG) + connect(*sys.argv[1:4]) + doctest.testmod() diff --git a/gsxws/core.py b/gsxws/core.py index a33f755..014ae3f 100644 --- a/gsxws/core.py +++ b/gsxws/core.py @@ -212,7 +212,21 @@ class GsxRequest(object): self.data = v.to_xml(self._request) self._response = k.replace("Request", "Response") - def _submit(self, method, response=None): + def _send(self, url, method, xmldata): + parsed = urlparse(url) + + ws = httplib.HTTPSConnection(parsed.netloc) + ws.putrequest("POST", parsed.path) + ws.putheader("User-Agent", "py-gsxws 0.9") + ws.putheader("Content-type", 'text/xml; charset="UTF-8"') + ws.putheader("Content-length", "%d" % len(xmldata)) + ws.putheader("SOAPAction", '"%s"' % method) + ws.endheaders() + ws.send(xmldata) + + return ws.getresponse() + + def _submit(self, method, response=None, raw=False): "Construct and submit the final SOAP message" global GSX_ENV, GSX_REGION, GSX_SESSION @@ -227,7 +241,7 @@ class GsxRequest(object): request.append(GSX_SESSION) if self._request == request_name: - "Some requests don't have a top-level container." + "Some requests don't have a top-level container" self.data = list(self.data)[0] request.append(self.data) @@ -235,18 +249,7 @@ class GsxRequest(object): data = ET.tostring(self.env, "UTF-8") logging.debug(data) - parsed = urlparse(url) - - ws = httplib.HTTPSConnection(parsed.netloc) - ws.putrequest("POST", parsed.path) - ws.putheader("User-Agent", "py-gsxws 0.9") - ws.putheader("Content-type", 'text/xml; charset="UTF-8"') - ws.putheader("Content-length", "%d" % len(data)) - ws.putheader("SOAPAction", '"%s"' % method) - ws.endheaders() - ws.send(data) - - res = ws.getresponse() + res = self._send(url, method, data) xml = res.read() if res.status > 200: @@ -256,7 +259,8 @@ class GsxRequest(object): response = response or self._response for r in ET.fromstring(xml).findall("*//%s" % response): - self.objects.append(GsxObject.from_xml(r)) + o = r if raw else GsxObject.from_xml(r) + self.objects.append(o) return self.objects @@ -304,9 +308,9 @@ class GsxObject(object): except KeyError: raise AttributeError("Invalid attribute: %s" % name) - def _submit(self, arg, method, ret=None): + def _submit(self, arg, method, ret=None, raw=False): self._req = GsxRequest(**{arg: self}) - result = self._req._submit(method, ret) + result = self._req._submit(method, ret, raw) return result if len(result) > 1 else result[0] def to_xml(self, root): diff --git a/gsxws/products.py b/gsxws/products.py index a82f9d4..f77957d 100644 --- a/gsxws/products.py +++ b/gsxws/products.py @@ -20,7 +20,7 @@ class Product(GsxObject): Returns the model description of this Product >>> Product('DGKFL06JDHJP').model().configDescription - 'iMac (27-inch, Mid 2011)' + u'iMac (27-inch, Mid 2011)' """ result = self._submit("productModelRequest", "FetchProductModel") @@ -38,7 +38,7 @@ class Product(GsxObject): warranty status request, the unit level warranty information is returned. >>> Product('DGKFL06JDHJP').warranty().warrantyStatus - 'Out Of Warranty (No Coverage)' + u'Out Of Warranty (No Coverage)' """ self._submit("unitDetail", "WarrantyStatus", "warrantyDetailInfo") self.warrantyDetails = self._req.objects[0] |