From d3e29c3b31ee9bfd5b8fd6d994ae1000c1f9a163 Mon Sep 17 00:00:00 2001 From: kairi003 Date: Sat, 13 Feb 2021 12:48:51 +0900 Subject: [PATCH] Update upload() method and more This patch updates the upload() method to upload raw binary file. In line with this, the make_request() method is used to migrate the file-like object of data['image'] to the files dict when necessary. --- imgurpython/client.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/imgurpython/client.py b/imgurpython/client.py index 9c41ea6..6cfa28c 100644 --- a/imgurpython/client.py +++ b/imgurpython/client.py @@ -1,4 +1,3 @@ -import base64 import requests from .imgur.models.tag import Tag from .imgur.models.album import Album @@ -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) @@ -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) @@ -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)