kopia lustrzana https://github.com/jcarbaugh/python-webfinger
move host from init to finger method
rodzic
0cfbc04cf9
commit
9ec76c807a
21
webfinger.py
21
webfinger.py
|
@ -55,8 +55,7 @@ class WebFingerResponse(object):
|
||||||
|
|
||||||
class WebFingerClient(object):
|
class WebFingerClient(object):
|
||||||
|
|
||||||
def __init__(self, host, timeout=None, official=False):
|
def __init__(self, timeout=None, official=False):
|
||||||
self._host = host
|
|
||||||
self._official = official
|
self._official = official
|
||||||
self._session = requests.session(
|
self._session = requests.session(
|
||||||
timeout=timeout,
|
timeout=timeout,
|
||||||
|
@ -70,7 +69,7 @@ class WebFingerClient(object):
|
||||||
content = resp.content
|
content = resp.content
|
||||||
return content if raw else rd.loads(content, resp.headers.get('Content-Type'))
|
return content if raw else rd.loads(content, resp.headers.get('Content-Type'))
|
||||||
|
|
||||||
def hostmeta(self, resource=None, rel=None, secure=True, raw=False):
|
def hostmeta(self, host, resource=None, rel=None, secure=True, raw=False):
|
||||||
""" Load host-meta resource from WebFinger provider.
|
""" Load host-meta resource from WebFinger provider.
|
||||||
Defaults to a secure (SSL) connection unless secure=False.
|
Defaults to a secure (SSL) connection unless secure=False.
|
||||||
"""
|
"""
|
||||||
|
@ -85,10 +84,8 @@ class WebFingerClient(object):
|
||||||
params['rel'] = rel
|
params['rel'] = rel
|
||||||
|
|
||||||
# use unofficial endpoint, if enabled
|
# use unofficial endpoint, if enabled
|
||||||
if not self._official and self._host in UNOFFICIAL_ENDPOINTS:
|
if not self._official and host in UNOFFICIAL_ENDPOINTS:
|
||||||
host = UNOFFICIAL_ENDPOINTS[self._host]
|
host = UNOFFICIAL_ENDPOINTS[host]
|
||||||
else:
|
|
||||||
host = self._host
|
|
||||||
|
|
||||||
# create full host-meta URL
|
# create full host-meta URL
|
||||||
url = "%s://%s/.well-known/host-meta" % (protocol, host)
|
url = "%s://%s/.well-known/host-meta" % (protocol, host)
|
||||||
|
@ -114,13 +111,15 @@ class WebFingerClient(object):
|
||||||
rel filter.
|
rel filter.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
host = subject.split("@")[-1]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# attempt SSL host-meta retrieval
|
# attempt SSL host-meta retrieval
|
||||||
hm = self.hostmeta(resource=resource, rel=rel)
|
hm = self.hostmeta(host, resource=resource, rel=rel)
|
||||||
secure = True
|
secure = True
|
||||||
except (requests.RequestException, requests.HTTPError):
|
except (requests.RequestException, requests.HTTPError):
|
||||||
# on failure, attempt non-SSL
|
# on failure, attempt non-SSL
|
||||||
hm = self.hostmeta(resource=resource, rel=rel, secure=False)
|
hm = self.hostmeta(host, resource=resource, rel=rel, secure=False)
|
||||||
secure = False
|
secure = False
|
||||||
|
|
||||||
if hm is None:
|
if hm is None:
|
||||||
|
@ -150,9 +149,7 @@ def finger(subject, resource=None, rel=None, timeout=None, official=False):
|
||||||
if ":" not in subject:
|
if ":" not in subject:
|
||||||
raise WebFingerException("scheme is required in subject URI")
|
raise WebFingerException("scheme is required in subject URI")
|
||||||
|
|
||||||
(identifier, host) = subject.split('@')
|
client = WebFingerClient(timeout=timeout, official=official)
|
||||||
|
|
||||||
client = WebFingerClient(host, timeout=timeout, official=official)
|
|
||||||
return client.finger(subject, resource=resource, rel=rel)
|
return client.finger(subject, resource=resource, rel=rel)
|
||||||
|
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue