def make_call(api_url, query_args=None): # api_url is expected to be the fully constructed URL, with any needed arguments appended. # This function will simply make the call, and return the response as an ElementTree object for parsing, # If response cannot be parsed because it is not valid XML, this function assumes an API error and raises an # APIException, passing forward the pages contents (which generally gives some indication of the error. if query_args is not None: get_params = urlencode_no_plus.urlencode_no_plus(query_args) response = urllib.request.urlopen(api_url+'%s' % get_params) else: response = urllib.request.urlopen(api_url) page = response.read() # Make sure the XML Parser doesn't return a ParsError. If it does, it's probably and API Issue, so raise an # exception, printing the response from the API call. try: xml_response = ET.fromstring(page) except ET.ParseError: raise APIException(page) return xml_response
def make_call(api_url, query_args=None): # api_url is expected to be the fully constructed URL, with any needed arguments appended. # This function will simply make the call, and return the response as an ElementTree object for parsing, # If response cannot be parsed because it is not valid XML, this function assumes an API error and raises an # APIException, passing forward the pages contents (which generally gives some indication of the error. opener = urllib2.build_opener() opener.addheaders = [('User-agent', 'Mozilla/5.0')] if query_args is not None: get_params = urlencode_no_plus.urlencode_no_plus(query_args) response = opener.open(api_url + '%s' % get_params) else: response = urllib.urlopen(api_url) page = response.read() # Make sure the XML Parser doesn't return a ParsError. If it does, it's probably and API Issue, so raise an # exception, printing the response from the API call. try: xml_response = ET.fromstring(page) except ET.ParseError: raise APIException(page) return xml_response
def make_call(api_url, query_args=None): # api_url is expected to be the fully constructed URL, with any needed arguments appended. # This function will simply make the call, and return the response as an ElementTree object for parsing, # If response cannot be parsed because it is not valid XML, this function assumes an API error and raises an # APIException, passing forward the pages contents (which generally gives some indication of the error. if query_args is not None: get_params = urlencode_no_plus.urlencode_no_plus(query_args) req = urllib2.Request(api_url+'%s' % get_params) req.add_unredirected_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31') response = urllib2.urlopen(req) else: response = urllib.urlopen(api_url) page = response.read() # Make sure the XML Parser doesn't return a ParsError. If it does, it's probably and API Issue, so raise an # exception, printing the response from the API call. try: xml_response = ET.fromstring(page) except ET.ParseError: raise APIException(page) return xml_response