From b2654cc8babd9d8b90d1b04190f44a4a2ee4116c Mon Sep 17 00:00:00 2001 From: Kyle Hardgrave Date: Fri, 9 Oct 2015 14:59:09 -0700 Subject: [PATCH 1/3] Pass PEP8 --- medium/__init__.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/medium/__init__.py b/medium/__init__.py index 0dba1bc..1449ac8 100644 --- a/medium/__init__.py +++ b/medium/__init__.py @@ -18,7 +18,6 @@ def __init__(self, application_id=None, application_secret=None, self.application_secret = application_secret self.access_token = access_token - def get_authorization_url(self, state, redirect_url, scopes): """Get a URL for users to authorize the application. @@ -37,7 +36,6 @@ def get_authorization_url(self, state, redirect_url, scopes): return "https://medium.com/m/oauth/authorize?" + urlencode(qs) - def exchange_authorization_code(self, code, redirect_url): """Exchange the authorization code for a long-lived access token, and set the token on the current Client. @@ -64,7 +62,6 @@ def exchange_authorization_code(self, code, redirect_url): } return self._request_and_set_auth_code(data) - def exchange_refresh_token(self, refresh_token): """Exchange the supplied refresh token for a new access token, and set the token on the current Client. @@ -88,7 +85,6 @@ def exchange_refresh_token(self, refresh_token): } return self._request_and_set_auth_code(data) - def get_current_user(self): """Fetch the data for the currently authenticated user. @@ -104,7 +100,6 @@ def get_current_user(self): """ return self._request("GET", "/v1/me") - def create_post(self, user_id, title, content, content_format, tags=None, canonical_url=None, publish_status=None, license=None): """Create a post for the current user. @@ -161,14 +156,12 @@ def create_post(self, user_id, title, content, content_format, tags=None, path = "/v1/users/%s/posts" % user_id return self._request("POST", path, json=data) - def _request_and_set_auth_code(self, data): """Request an access token and set it on the current client.""" result = self._request("POST", "/v1/tokens", form_data=data) self.access_token = result["access_token"] return result - def _request(self, method, path, json=None, form_data=None, files=None): """Make a signed request to the given route.""" url = BASE_PATH + path From 66b656086c1a259098f61804be024d845c4ed6e2 Mon Sep 17 00:00:00 2001 From: Kyle Hardgrave Date: Fri, 9 Oct 2015 16:20:12 -0700 Subject: [PATCH 2/3] Add upload image method --- medium/__init__.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/medium/__init__.py b/medium/__init__.py index 1449ac8..0cdc79b 100644 --- a/medium/__init__.py +++ b/medium/__init__.py @@ -1,4 +1,5 @@ # Copyright 2015 A Medium Corporation +from os.path import basename try: from urllib.parse import urlencode except ImportError: @@ -88,6 +89,8 @@ def exchange_refresh_token(self, refresh_token): def get_current_user(self): """Fetch the data for the currently authenticated user. + Requires the ``basicProfile`` scope. + :returns: A dictionary with the users data :: { @@ -104,6 +107,8 @@ def create_post(self, user_id, title, content, content_format, tags=None, canonical_url=None, publish_status=None, license=None): """Create a post for the current user. + Requires the ``publishPost`` scope. + :param str user_id: The application-specific user ID as returned by ``get_current_user()`` :param str title: The title of the post @@ -156,6 +161,26 @@ def create_post(self, user_id, title, content, content_format, tags=None, path = "/v1/users/%s/posts" % user_id return self._request("POST", path, json=data) + def upload_image(self, file_path, content_type): + """Upload a local image to Medium for use in a post. + + Requires the ``uploadImage`` scope. + + :param str file_path: The file path of the image + :param str content_type: The type of the image. Valid values are + ``image/jpeg``, ``image/png``, ``image/gif``, and ``image/tiff``. + :returns: A dictionary with the image data :: + + { + 'url': 'https://cdn-images-1.medium.com/0*dlkfjalksdjfl.jpg', + 'md5': 'd87e1628ca597d386e8b3e25de3a18b8' + } + """ + with open(file_path, "rb") as f: + filename = basename(file_path) + files = {"image": (filename, f, content_type)} + return self._request("POST", "/v1/images", files=files) + def _request_and_set_auth_code(self, data): """Request an access token and set it on the current client.""" result = self._request("POST", "/v1/tokens", form_data=data) @@ -170,10 +195,7 @@ def _request(self, method, path, json=None, form_data=None, files=None): "Accept-Charset": "utf-8", "Authorization": "Bearer %s" % self.access_token, } - if form_data is not None: - headers["Content-Type"] = "application/x-www-form-urlencoded" - else: - headers["Content-Type"] = "application/json" + resp = requests.request(method, url, json=json, data=form_data, files=files, headers=headers) json = resp.json() From 0f49c064443b90b71c22837ee02b1c8c3acdbd23 Mon Sep 17 00:00:00 2001 From: Kyle Hardgrave Date: Mon, 12 Oct 2015 13:45:57 -0700 Subject: [PATCH 3/3] Bump version --- setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 7205240..33a60e6 100644 --- a/setup.py +++ b/setup.py @@ -3,12 +3,12 @@ name='medium', packages=['medium'], install_requires=['requests'], - version='0.2.0', + version='0.3.0', description='SDK for working with the Medium API', author='Kyle Hardgrave', author_email='kyle@medium.com', url='https://github.com/Medium/medium-sdk-python', - download_url='https://github.com/Medium/medium-sdk-python/tarball/v0.2.0', + download_url='https://github.com/Medium/medium-sdk-python/tarball/v0.3.0', keywords=['medium', 'sdk', 'oauth', 'api'], classifiers=[], )