Skip to content

Commit cbdd0dc

Browse files
committed
Merge pull request #3 from Medium/kyle-images
Add upload image method
2 parents 80be1a5 + 0f49c06 commit cbdd0dc

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

medium/__init__.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Copyright 2015 A Medium Corporation
2+
from os.path import basename
23
try:
34
from urllib.parse import urlencode
45
except ImportError:
@@ -18,7 +19,6 @@ def __init__(self, application_id=None, application_secret=None,
1819
self.application_secret = application_secret
1920
self.access_token = access_token
2021

21-
2222
def get_authorization_url(self, state, redirect_url, scopes):
2323
"""Get a URL for users to authorize the application.
2424
@@ -37,7 +37,6 @@ def get_authorization_url(self, state, redirect_url, scopes):
3737

3838
return "https://medium.com/m/oauth/authorize?" + urlencode(qs)
3939

40-
4140
def exchange_authorization_code(self, code, redirect_url):
4241
"""Exchange the authorization code for a long-lived access token, and
4342
set the token on the current Client.
@@ -64,7 +63,6 @@ def exchange_authorization_code(self, code, redirect_url):
6463
}
6564
return self._request_and_set_auth_code(data)
6665

67-
6866
def exchange_refresh_token(self, refresh_token):
6967
"""Exchange the supplied refresh token for a new access token, and
7068
set the token on the current Client.
@@ -88,10 +86,11 @@ def exchange_refresh_token(self, refresh_token):
8886
}
8987
return self._request_and_set_auth_code(data)
9088

91-
9289
def get_current_user(self):
9390
"""Fetch the data for the currently authenticated user.
9491
92+
Requires the ``basicProfile`` scope.
93+
9594
:returns: A dictionary with the users data ::
9695
9796
{
@@ -104,11 +103,12 @@ def get_current_user(self):
104103
"""
105104
return self._request("GET", "/v1/me")
106105

107-
108106
def create_post(self, user_id, title, content, content_format, tags=None,
109107
canonical_url=None, publish_status=None, license=None):
110108
"""Create a post for the current user.
111109
110+
Requires the ``publishPost`` scope.
111+
112112
:param str user_id: The application-specific user ID as returned by
113113
``get_current_user()``
114114
:param str title: The title of the post
@@ -161,14 +161,32 @@ def create_post(self, user_id, title, content, content_format, tags=None,
161161
path = "/v1/users/%s/posts" % user_id
162162
return self._request("POST", path, json=data)
163163

164+
def upload_image(self, file_path, content_type):
165+
"""Upload a local image to Medium for use in a post.
166+
167+
Requires the ``uploadImage`` scope.
168+
169+
:param str file_path: The file path of the image
170+
:param str content_type: The type of the image. Valid values are
171+
``image/jpeg``, ``image/png``, ``image/gif``, and ``image/tiff``.
172+
:returns: A dictionary with the image data ::
173+
174+
{
175+
'url': 'https://cdn-images-1.medium.com/0*dlkfjalksdjfl.jpg',
176+
'md5': 'd87e1628ca597d386e8b3e25de3a18b8'
177+
}
178+
"""
179+
with open(file_path, "rb") as f:
180+
filename = basename(file_path)
181+
files = {"image": (filename, f, content_type)}
182+
return self._request("POST", "/v1/images", files=files)
164183

165184
def _request_and_set_auth_code(self, data):
166185
"""Request an access token and set it on the current client."""
167186
result = self._request("POST", "/v1/tokens", form_data=data)
168187
self.access_token = result["access_token"]
169188
return result
170189

171-
172190
def _request(self, method, path, json=None, form_data=None, files=None):
173191
"""Make a signed request to the given route."""
174192
url = BASE_PATH + path
@@ -177,10 +195,7 @@ def _request(self, method, path, json=None, form_data=None, files=None):
177195
"Accept-Charset": "utf-8",
178196
"Authorization": "Bearer %s" % self.access_token,
179197
}
180-
if form_data is not None:
181-
headers["Content-Type"] = "application/x-www-form-urlencoded"
182-
else:
183-
headers["Content-Type"] = "application/json"
198+
184199
resp = requests.request(method, url, json=json, data=form_data,
185200
files=files, headers=headers)
186201
json = resp.json()

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
name='medium',
44
packages=['medium'],
55
install_requires=['requests'],
6-
version='0.2.0',
6+
version='0.3.0',
77
description='SDK for working with the Medium API',
88
author='Kyle Hardgrave',
99
author_email='[email protected]',
1010
url='https://github.com/Medium/medium-sdk-python',
11-
download_url='https://github.com/Medium/medium-sdk-python/tarball/v0.2.0',
11+
download_url='https://github.com/Medium/medium-sdk-python/tarball/v0.3.0',
1212
keywords=['medium', 'sdk', 'oauth', 'api'],
1313
classifiers=[],
1414
)

0 commit comments

Comments
 (0)