use extractor.request for API calls (#130)
... at least for OAuth1.0 based APIs (flickr, smugmug, tumblr)
This commit is contained in:
@@ -54,16 +54,17 @@ class Extractor():
|
|||||||
return config.interpolate(
|
return config.interpolate(
|
||||||
("extractor", self.category, self.subcategory, key), default)
|
("extractor", self.category, self.subcategory, key), default)
|
||||||
|
|
||||||
def request(self, url, method="GET", *,
|
def request(self, url, method="GET", *, session=None,
|
||||||
encoding=None, expect=(), retries=None, **kwargs):
|
encoding=None, expect=(), retries=None, **kwargs):
|
||||||
tries = 0
|
tries = 0
|
||||||
retries = retries or self._retries
|
retries = retries or self._retries
|
||||||
|
session = session or self.session
|
||||||
kwargs.setdefault("timeout", self._timeout)
|
kwargs.setdefault("timeout", self._timeout)
|
||||||
kwargs.setdefault("verify", self._verify)
|
kwargs.setdefault("verify", self._verify)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
response = self.session.request(method, url, **kwargs)
|
response = session.request(method, url, **kwargs)
|
||||||
except (requests.exceptions.ConnectionError,
|
except (requests.exceptions.ConnectionError,
|
||||||
requests.exceptions.Timeout,
|
requests.exceptions.Timeout,
|
||||||
requests.exceptions.ChunkedEncodingError,
|
requests.exceptions.ChunkedEncodingError,
|
||||||
|
|||||||
@@ -295,7 +295,6 @@ class FlickrAPI(oauth.OAuth1API):
|
|||||||
else:
|
else:
|
||||||
self.formats = self.FORMATS
|
self.formats = self.FORMATS
|
||||||
self.formats = self.formats[:4]
|
self.formats = self.formats[:4]
|
||||||
self.subcategory = extractor.subcategory
|
|
||||||
|
|
||||||
def favorites_getList(self, user_id):
|
def favorites_getList(self, user_id):
|
||||||
"""Returns a list of the user's favorite photos."""
|
"""Returns a list of the user's favorite photos."""
|
||||||
@@ -387,10 +386,10 @@ class FlickrAPI(oauth.OAuth1API):
|
|||||||
params["nojsoncallback"] = "1"
|
params["nojsoncallback"] = "1"
|
||||||
if self.api_key:
|
if self.api_key:
|
||||||
params["api_key"] = self.api_key
|
params["api_key"] = self.api_key
|
||||||
data = self.session.get(self.API_URL, params=params).json()
|
data = self.request(self.API_URL, params=params).json()
|
||||||
if "code" in data:
|
if "code" in data:
|
||||||
if data["code"] == 1:
|
if data["code"] == 1:
|
||||||
raise exception.NotFoundError(self.subcategory)
|
raise exception.NotFoundError(self.extractor.subcategory)
|
||||||
elif data["code"] == 98:
|
elif data["code"] == 98:
|
||||||
raise exception.AuthenticationError(data.get("message"))
|
raise exception.AuthenticationError(data.get("message"))
|
||||||
elif data["code"] == 99:
|
elif data["code"] == 99:
|
||||||
|
|||||||
@@ -238,7 +238,7 @@ class SmugmugAPI(oauth.OAuth1API):
|
|||||||
params["APIKey"] = self.api_key
|
params["APIKey"] = self.api_key
|
||||||
params["_verbosity"] = "1"
|
params["_verbosity"] = "1"
|
||||||
|
|
||||||
response = self.session.get(url, params=params, headers=self.HEADERS)
|
response = self.request(url, params=params, headers=self.HEADERS)
|
||||||
data = response.json()
|
data = response.json()
|
||||||
|
|
||||||
if 200 <= data["Code"] < 400:
|
if 200 <= data["Code"] < 400:
|
||||||
|
|||||||
@@ -303,7 +303,7 @@ class TumblrAPI(oauth.OAuth1API):
|
|||||||
url = "https://api.tumblr.com/v2/blog/{}/{}".format(
|
url = "https://api.tumblr.com/v2/blog/{}/{}".format(
|
||||||
blog, endpoint)
|
blog, endpoint)
|
||||||
|
|
||||||
response = self.session.get(url, params=params)
|
response = self.request(url, params=params)
|
||||||
data = response.json()
|
data = response.json()
|
||||||
status = data["meta"]["status"]
|
status = data["meta"]["status"]
|
||||||
|
|
||||||
|
|||||||
@@ -109,6 +109,7 @@ class OAuth1API():
|
|||||||
|
|
||||||
def __init__(self, extractor):
|
def __init__(self, extractor):
|
||||||
self.log = extractor.log
|
self.log = extractor.log
|
||||||
|
self.extractor = extractor
|
||||||
|
|
||||||
api_key = extractor.config("api-key", self.API_KEY)
|
api_key = extractor.config("api-key", self.API_KEY)
|
||||||
api_secret = extractor.config("api-secret", self.API_SECRET)
|
api_secret = extractor.config("api-secret", self.API_SECRET)
|
||||||
@@ -124,3 +125,8 @@ class OAuth1API():
|
|||||||
self.log.debug("Using api_key authentication")
|
self.log.debug("Using api_key authentication")
|
||||||
self.session = extractor.session
|
self.session = extractor.session
|
||||||
self.api_key = api_key
|
self.api_key = api_key
|
||||||
|
|
||||||
|
def request(self, url, method="GET", *, expect=range(400, 500), **kwargs):
|
||||||
|
kwargs["expect"] = expect
|
||||||
|
kwargs["session"] = self.session
|
||||||
|
return self.extractor.request(url, method, **kwargs)
|
||||||
|
|||||||
Reference in New Issue
Block a user