ustroetz / python-osrm

A Python wrapper around the OSRM API
MIT License
133 stars 55 forks source link

TypeError: expected string or bytes-like object #48

Open neilfloow opened 4 years ago

neilfloow commented 4 years ago

Hi,

I'm trying to use the match function with the following...

import osrm
OSRM_AUTH=('user', 'pass')
OSRM_VERSION = 'v1'
OSRM_PROFILE = 'driving'
OSRM_HOST = 'http://router.project-osrm.org'
osrm.RequestConfig.host = OSRM_HOST
osrm.RequestConfig.version = OSRM_VERSION
osrm.RequestConfig.profile = OSRM_PROFILE
# osrm.RequestConfig.auth = OSRM_AUTH
points = [(-33.45017046193167,-70.65281867980957),
          (-33.45239047269638,-70.65300107002258),
          (-33.453867464504555,-70.65277576446533)]
result = osrm.match(points, steps=False, overview='simplified', url_config=osrm.RequestConfig)

This fails with when calling request.py with HTTPError: HTTP Error 400: Bad Request...

---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
<ipython-input-21-94dd1c9583c0> in <module>
----> 1 result = osrm.match(points, steps=False, overview='simplified', url_config=osrm.RequestConfig)

~/work/mapping/python-osrm/osrm/core.py in match(points, steps, overview, geometry, timestamps, radius, annotations, gaps, tidy, waypoints, url_config)
    102     print(f'type(REQUEST)   : {type(req.get_full_url())}')
    103 
--> 104     r = urlopen(req)
    105     r_json = json.loads(r.read().decode('utf-8'))
    106     if "code" not in r_json or "Ok" not in r_json["code"]:

~/.miniconda3/lib/python3.8/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    220     else:
    221         opener = _opener
--> 222     return opener.open(url, data, timeout)
    223 
    224 def install_opener(opener):

~/.miniconda3/lib/python3.8/urllib/request.py in open(self, fullurl, data, timeout)
    529         for processor in self.process_response.get(protocol, []):
    530             meth = getattr(processor, meth_name)
--> 531             response = meth(req, response)
    532 
    533         return response

~/.miniconda3/lib/python3.8/urllib/request.py in http_response(self, request, response)
    638         # request was successfully received, understood, and accepted.
    639         if not (200 <= code < 300):
--> 640             response = self.parent.error(
    641                 'http', request, response, code, msg, hdrs)
    642 

~/.miniconda3/lib/python3.8/urllib/request.py in error(self, proto, *args)
    567         if http_err:
    568             args = (dict, 'default', 'http_error_default') + orig_args
--> 569             return self._call_chain(*args)
    570 
    571 # XXX probably also want an abstract factory that knows when it makes

~/.miniconda3/lib/python3.8/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
    500         for handler in handlers:
    501             func = getattr(handler, meth_name)
--> 502             result = func(*args)
    503             if result is not None:
    504                 return result

~/.miniconda3/lib/python3.8/urllib/request.py in http_error_default(self, req, fp, code, msg, hdrs)
    647 class HTTPDefaultErrorHandler(BaseHandler):
    648     def http_error_default(self, req, fp, code, msg, hdrs):
--> 649         raise HTTPError(req.full_url, code, msg, hdrs, fp)
    650 
    651 class HTTPRedirectHandler(BaseHandler):

HTTPError: HTTP Error 400: Bad Request

If I enable authorisation it gets round this but still fails, this time with a TypeError: expected string or bytes-like object...

OSRM_AUTH=('user', 'pass')
OSRM_VERSION = 'v1'
OSRM_PROFILE = 'driving'
OSRM_HOST = 'http://router.project-osrm.org'
osrm.RequestConfig.host = OSRM_HOST
osrm.RequestConfig.version = OSRM_VERSION
osrm.RequestConfig.profile = OSRM_PROFILE
osrm.RequestConfig.auth = OSRM_AUTH
points = [(-33.45017046193167,-70.65281867980957),
          (-33.45239047269638,-70.65300107002258),
          (-33.453867464504555,-70.65277576446533)]
result = osrm.match(points, steps=False, overview='simplified', url_config=osrm.RequestConfig)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-94dd1c9583c0> in <module>
----> 1 result = osrm.match(points, steps=False, overview='simplified', url_config=osrm.RequestConfig)

~/work/mapping/python-osrm/osrm/core.py in match(points, steps, overview, geometry, timestamps, radius, annotations, gaps, tidy, waypoints, url_config)
    102     print(f'type(REQUEST)   : {type(req.get_full_url())}')
    103 
--> 104     r = urlopen(req)
    105     r_json = json.loads(r.read().decode('utf-8'))
    106     if "code" not in r_json or "Ok" not in r_json["code"]:

~/.miniconda3/lib/python3.8/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    220     else:
    221         opener = _opener
--> 222     return opener.open(url, data, timeout)
    223 
    224 def install_opener(opener):

~/.miniconda3/lib/python3.8/urllib/request.py in open(self, fullurl, data, timeout)
    523 
    524         sys.audit('urllib.Request', req.full_url, req.data, req.headers, req.get_method())
--> 525         response = self._open(req, data)
    526 
    527         # post-process response

~/.miniconda3/lib/python3.8/urllib/request.py in _open(self, req, data)
    540 
    541         protocol = req.type
--> 542         result = self._call_chain(self.handle_open, protocol, protocol +
    543                                   '_open', req)
    544         if result:

~/.miniconda3/lib/python3.8/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
    500         for handler in handlers:
    501             func = getattr(handler, meth_name)
--> 502             result = func(*args)
    503             if result is not None:
    504                 return result

~/.miniconda3/lib/python3.8/urllib/request.py in http_open(self, req)
   1377 
   1378     def http_open(self, req):
-> 1379         return self.do_open(http.client.HTTPConnection, req)
   1380 
   1381     http_request = AbstractHTTPHandler.do_request_

~/.miniconda3/lib/python3.8/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
   1348         try:
   1349             try:
-> 1350                 h.request(req.get_method(), req.selector, req.data, headers,
   1351                           encode_chunked=req.has_header('Transfer-encoding'))
   1352             except OSError as err: # timeout error

~/.miniconda3/lib/python3.8/http/client.py in request(self, method, url, body, headers, encode_chunked)
   1253                 encode_chunked=False):
   1254         """Send a complete request to the server."""
-> 1255         self._send_request(method, url, body, headers, encode_chunked)
   1256 
   1257     def _send_request(self, method, url, body, headers, encode_chunked):

~/.miniconda3/lib/python3.8/http/client.py in _send_request(self, method, url, body, headers, encode_chunked)
   1294 
   1295         for hdr, value in headers.items():
-> 1296             self.putheader(hdr, value)
   1297         if isinstance(body, str):
   1298             # RFC 2616 Section 3.7.1 says that text default has a

~/.miniconda3/lib/python3.8/http/client.py in putheader(self, header, *values)
   1230                 values[i] = str(one_value).encode('ascii')
   1231 
-> 1232             if _is_illegal_header_value(values[i]):
   1233                 raise ValueError('Invalid header value %r' % (values[i],))
   1234 

TypeError: expected string or bytes-like object

I've checked type(req) that is passed to urlopen(req) and its reported as a string so I'm stumped as to whats going wrong here.

If I try defining a custom RequestConfig() I get an HTTPError: HTTP Erro 400: Bad Request again...

OSRM_AUTH=('user', 'pass')
OSRM_VERSION = 'v1'
OSRM_PROFILE = 'driving'
OSRM_HOST = 'http://router.project-osrm.org'
custom_conf = osrm.RequestConfig(OSRM_HOST, basic_auth=OSRM_AUTH)
custom_conf.profile = OSRM_PROFILE
custom_conf.version = OSRM_VERSION
points = [(-33.45017046193167,-70.65281867980957),
          (-33.45239047269638,-70.65300107002258),
          (-33.453867464504555,-70.65277576446533)]
result = osrm.match(points, steps=False, overview='simplified', url_config=custom_conf)
---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
<ipython-input-66-216cc65d42b9> in <module>
----> 1 result = osrm.match(points, steps=False, overview='simplified', url_config=custom_conf)

~/work/mapping/python-osrm/osrm/core.py in match(points, steps, overview, geometry, timestamps, radius, annotations, gaps, tidy, waypoints, url_config)
    104 #    print(f'REQUEST.headers  : {req.get_headers()}')
    105 
--> 106     r = urlopen(req)
    107     r_json = json.loads(r.read().decode('utf-8'))
    108     if "code" not in r_json or "Ok" not in r_json["code"]:

~/.miniconda3/lib/python3.8/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    220     else:
    221         opener = _opener
--> 222     return opener.open(url, data, timeout)
    223 
    224 def install_opener(opener):

~/.miniconda3/lib/python3.8/urllib/request.py in open(self, fullurl, data, timeout)
    529         for processor in self.process_response.get(protocol, []):
    530             meth = getattr(processor, meth_name)
--> 531             response = meth(req, response)
    532 
    533         return response

~/.miniconda3/lib/python3.8/urllib/request.py in http_response(self, request, response)
    638         # request was successfully received, understood, and accepted.
    639         if not (200 <= code < 300):
--> 640             response = self.parent.error(
    641                 'http', request, response, code, msg, hdrs)
    642 

~/.miniconda3/lib/python3.8/urllib/request.py in error(self, proto, *args)
    567         if http_err:
    568             args = (dict, 'default', 'http_error_default') + orig_args
--> 569             return self._call_chain(*args)
    570 
    571 # XXX probably also want an abstract factory that knows when it makes

~/.miniconda3/lib/python3.8/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
    500         for handler in handlers:
    501             func = getattr(handler, meth_name)
--> 502             result = func(*args)
    503             if result is not None:
    504                 return result

~/.miniconda3/lib/python3.8/urllib/request.py in http_error_default(self, req, fp, code, msg, hdrs)
    647 class HTTPDefaultErrorHandler(BaseHandler):
    648     def http_error_default(self, req, fp, code, msg, hdrs):
--> 649         raise HTTPError(req.full_url, code, msg, hdrs, fp)
    650 
    651 class HTTPRedirectHandler(BaseHandler):

HTTPError: HTTP Error 400: Bad Request

I'm doing this all under a Conda Virtual Environment that is based on Python 3.8.

Any advice/insight would be very much appreciated as I'm stumped as to how to work out where things are going wrong.

Thanks in advance.