Skip to content

Commit 70dea61

Browse files
Merge pull request #88 from alexis-mignon/net_requests
Net requests
2 parents 7e2d28a + 790e7a7 commit 70dea61

File tree

6 files changed

+28
-108
lines changed

6 files changed

+28
-108
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Requires:
5151
* python >= 2.7
5252
* [python-oauth2](https://github.com/joestump/python-oauth2)
5353
* [six](https://github.com/benjaminp/six)
54+
* requests
5455

5556
Please note that `flickrapi` on Pypi is a different distribution by a different author.
5657

flickr_api/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
Allows to define the version number uniquely.
44
"""
55

6-
__version__ = "0.5"
6+
__version__ = "0.6"

flickr_api/method_call.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"""
1111
from six.moves import urllib
1212
from six import iteritems
13+
import requests
1314
import hashlib
1415
import json
1516
import logging
@@ -104,36 +105,36 @@ def call_api(api_key=None, api_secret=None, auth_handler=None,
104105

105106
if auth_handler is None:
106107
if needssigning:
107-
query_elements = args.items()
108+
query_elements = list(args.items())
108109
query_elements.sort()
109110
sig = keys.API_SECRET + \
110111
["".join(["".join(e) for e in query_elements])]
111112
m = hashlib.md5()
112113
m.update(sig)
113114
api_sig = m.digest()
114115
args["api_sig"] = api_sig
116+
# Used to hash the arguments for cache
115117
data = urllib.parse.urlencode(args)
116118
else:
117-
data = auth_handler.complete_parameters(
119+
args = auth_handler.complete_parameters(
118120
url=request_url, params=args
119-
).to_postdata()
121+
)
122+
# Used to hash the arguments for cache
123+
data = args.to_postdata()
120124

121125
if CACHE is None:
122-
resp = send_request(request_url, data)
126+
resp = requests.post(request_url, args)
123127
else:
124-
resp = CACHE.get(data) or send_request(request_url, data)
128+
resp = CACHE.get(data) or requests(request_url, args)
125129
if data not in CACHE:
126130
CACHE.set(data, resp)
127131

132+
resp = resp.content
128133
if raw:
129134
return resp
130135

131136
try:
132-
try:
133-
resp = json.loads(resp.decode())
134-
except AttributeError:
135-
# exception for python 3 where `resp` is a string (doesn't need decoding)
136-
resp = json.loads(resp)
137+
resp = json.loads(resp)
137138
except ValueError as e:
138139
logger.error("Could not parse response: %s", str(resp))
139140

flickr_api/multipart.py

Lines changed: 0 additions & 87 deletions
This file was deleted.

flickr_api/reflection.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ def static_call(*args, **kwargs):
290290
token, kwargs = _get_token(None, **kwargs)
291291
method_args, format_result = method(*args, **kwargs)
292292
method_args["auth_handler"] = token
293+
logger.debug("Calling method '%s' with arguments: %s", flickr_method, str(method_args))
293294
r = method_call.call_api(method=flickr_method, **method_args)
294295
try:
295296
return format_result(r, token)

flickr_api/upload.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
from .flickrerrors import FlickrError, FlickrAPIError
1919
from .objects import Photo, UploadTicket
2020
from . import auth
21-
from . import multipart
2221
import os
2322
from xml.etree import ElementTree as ET
2423
from six import text_type, binary_type, iteritems
24+
import requests
2525

2626
UPLOAD_URL = "https://api.flickr.com/services/upload/"
2727
REPLACE_URL = "https://api.flickr.com/services/replace/"
@@ -47,16 +47,19 @@ def post(url, auth_handler, args, photo_file, photo_file_data=None):
4747

4848
params = auth_handler.complete_parameters(url, args)
4949

50-
fields = params.items()
5150
if photo_file_data is None:
52-
files = [("photo", os.path.basename(photo_file), open(photo_file, "rb").read())]
53-
else:
54-
files = [("photo", os.path.basename(photo_file), photo_file_data.read())]
51+
photo_file_data = open(photo_file, "rb")
52+
53+
files = {
54+
"photo": (os.path.basename(photo_file), photo_file_data.read())
55+
}
5556

56-
r, data = multipart.posturl(url, fields, files)
57-
if r.status != 200:
57+
resp = requests.post(url, params, files=files)
58+
data = resp.content
59+
60+
if resp.status_code != 200:
5861
raise FlickrError("HTTP Error %i: %s" % (r.status, data))
59-
62+
6063
r = ET.fromstring(data)
6164
if r.get("stat") != 'ok':
6265
err = r[0]
@@ -141,7 +144,7 @@ def replace(**args):
141144
142145
"""
143146
if "async" not in args:
144-
args["async"] = True
147+
args["async"] = False
145148
if "photo" in args:
146149
args["photo_id"] = args.pop("photo").id
147150

@@ -155,9 +158,10 @@ def replace(**args):
155158
r = post(REPLACE_URL, auth.AUTH_HANDLER, args, photo_file, photo_file_data)
156159

157160
t = r[0]
161+
158162
if t.tag == 'photoid':
159163
return Photo(id=t.text)
160164
elif t.tag == 'ticketid':
161-
return UploadTicket(id=t.text, secret=t.secret)
165+
return UploadTicket(id=t.text)
162166
else:
163167
raise FlickrError("Unexpected tag: %s" % t.tag)

0 commit comments

Comments
 (0)