def resolve_adapter(uri):
# type: (AdapterSpec) -> BaseAdapter
"""
Given a URI, returns a properly-configured adapter instance.
"""
if isinstance(uri, BaseAdapter):
return uri
parsed = compat.urllib_parse.urlsplit(uri) # type: SplitResult
if not parsed.scheme:
raise with_context(
exc = InvalidUri(
'URI must begin with "<protocol>://" (e.g., "udp://").',
),
context = {
'parsed': parsed,
'uri': uri,
},
)
try:
adapter_type = adapter_registry[parsed.scheme]
except KeyError:
raise with_context(
exc = InvalidUri('Unrecognized protocol {protocol!r}.'.format(
protocol = parsed.scheme,
)),
context = {
'parsed': parsed,
'uri': uri,
},
)
return adapter_type.configure(parsed)
评论列表
文章目录