diff --git a/medium/__init__.py b/medium/__init__.py index 0dba1bc..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: @@ -18,7 +19,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 +37,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 +63,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,10 +86,11 @@ 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. + Requires the ``basicProfile`` scope. + :returns: A dictionary with the users data :: { @@ -104,11 +103,12 @@ 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. + 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 @@ -161,6 +161,25 @@ 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.""" @@ -168,7 +187,6 @@ def _request_and_set_auth_code(self, 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 @@ -177,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() 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=[], )