Skip to content

Commit

Permalink
better xml response parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
TralahM committed May 17, 2020
1 parent 9267659 commit 1251b9d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 25 deletions.
25 changes: 25 additions & 0 deletions badsms.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<b/>
<br>
<br/>
<?xml version="1.0" encoding="utf-8" ?>

<query>

<query_result>

<status>Success</status>

<status>Send_SMS</status>

<to>27110000000</to>

<sms_id>000</sms_id>

</query_result>

<query_status>DONE</query_status>

<query_code>D0011</query_code>

</query>

1 change: 1 addition & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
lxml
xmltodict
sphinx_rtd_theme
55 changes: 31 additions & 24 deletions greydot/sms.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,11 @@
</query>
"""
import requests
from lxml import etree
from io import StringIO, BytesIO
import os
import xmltodict
from json import dumps, loads
import urllib.parse

API_URL = "https://greydotapi.me/"
Expand All @@ -63,6 +60,31 @@
# GREYDOT_APP_KEY = "GREYDOT_APP_KEY"
# raise ValueError("GREYDOT_APP_KEY environment variable is not set.")

SAMPLE = """
<?xml version="1.0" encoding="utf-8" ?>
<query>
<query_result>
<status>Success</status>
<status>Send_SMS</status>
<to>27110000000</to>
<sms_id>000</sms_id>
</query_result>
<query_status>DONE</query_status>
<query_code>D0011</query_code>
</query>
"""


def parse_xml_response(response):
"""
Expand All @@ -82,27 +104,12 @@ def parse_xml_response(response):
}
"""
print(response)
parser = etree.XMLParser(recover=True)
response = bytes(response, encoding="utf-8")
base = etree.parse(BytesIO(response), parser)
# base = etree.fromstring(response)
# print(response)
try:
query_result = base.xpath("//query/query_result/status")
query_status = base.xpath("//query/query_status")[0].text
query_code = base.xpath("//query/query_code")[0].text
to = base.xpath("//query/query_result/to")[0].text
sms_id = base.xpath("//query/query_result/sms_id")[0].text
result_status = query_result[0].text
return {
"result_status": result_status,
"to": to,
"sms_id": sms_id,
"query_status": query_status,
"query_code": query_code,
}
doc = dumps(xmltodict.parse(response[response.find("<?xml"):]))
print(doc)
return loads(doc)
except Exception:
return {
"result_status": "result_status",
Expand Down
22 changes: 22 additions & 0 deletions samplesms.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8" ?>

<query>

<query_result>

<status>Success</status>

<status>Send_SMS</status>

<to>27110000000</to>

<sms_id>000</sms_id>

</query_result>

<query_status>DONE</query_status>

<query_code>D0011</query_code>

</query>

3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = greydot-api
version = 0.5
version = 0.6
description = Unofficial Python SDK for GreyDot.me API
long_description = file: README.md
long_description_content_type=text/markdown
Expand Down Expand Up @@ -29,6 +29,7 @@ packages = find:
install_requires =
lxml
requests
xmltodict
[options.extras_require]
docs=
sphinx
Expand Down

0 comments on commit 1251b9d

Please sign in to comment.