Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Update upload() method and more #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions imgurpython/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import base64
import requests
from .imgur.models.tag import Tag
from .imgur.models.album import Album
Expand Down Expand Up @@ -129,6 +128,9 @@ def make_request(self, method, route, data=None, force_anon=False):

if method in ('delete', 'get'):
response = method_to_call(url, headers=header, params=data, data=data)
elif method == 'post' and data.get('type', None) == 'file':
files = {'image': data.pop('image')}
response = method_to_call(url, headers=header, data=data, files=files)
else:
response = method_to_call(url, headers=header, data=data)

Expand All @@ -137,6 +139,9 @@ def make_request(self, method, route, data=None, force_anon=False):
header = self.prepare_headers()
if method in ('delete', 'get'):
response = method_to_call(url, headers=header, params=data, data=data)
elif method == 'post' and data.get('type', None) == 'file':
files = {'image': data.pop('image')}
response = method_to_call(url, headers=header, data=data, files=files)
else:
response = method_to_call(url, headers=header, data=data)

Expand Down Expand Up @@ -587,12 +592,11 @@ def upload(self, fd, config=None, anon=True):
if not config:
config = dict()

contents = fd.read()
b64 = base64.b64encode(contents)
data = {
'image': b64,
'type': 'base64',
'image': fd,
'type': 'file',
}

data.update({meta: config[meta] for meta in set(self.allowed_image_fields).intersection(config.keys())})

return self.make_request('POST', 'upload', data, anon)
Expand Down