11# Copyright 2015 A Medium Corporation
2+ from os .path import basename
23try :
34 from urllib .parse import urlencode
45except ImportError :
@@ -88,6 +89,8 @@ def exchange_refresh_token(self, refresh_token):
8889 def get_current_user (self ):
8990 """Fetch the data for the currently authenticated user.
9091
92+ Requires the ``basicProfile`` scope.
93+
9194 :returns: A dictionary with the users data ::
9295
9396 {
@@ -104,6 +107,8 @@ def create_post(self, user_id, title, content, content_format, tags=None,
104107 canonical_url = None , publish_status = None , license = None ):
105108 """Create a post for the current user.
106109
110+ Requires the ``publishPost`` scope.
111+
107112 :param str user_id: The application-specific user ID as returned by
108113 ``get_current_user()``
109114 :param str title: The title of the post
@@ -156,6 +161,26 @@ def create_post(self, user_id, title, content, content_format, tags=None,
156161 path = "/v1/users/%s/posts" % user_id
157162 return self ._request ("POST" , path , json = data )
158163
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 )
183+
159184 def _request_and_set_auth_code (self , data ):
160185 """Request an access token and set it on the current client."""
161186 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):
170195 "Accept-Charset" : "utf-8" ,
171196 "Authorization" : "Bearer %s" % self .access_token ,
172197 }
173- if form_data is not None :
174- headers ["Content-Type" ] = "application/x-www-form-urlencoded"
175- else :
176- headers ["Content-Type" ] = "application/json"
198+
177199 resp = requests .request (method , url , json = json , data = form_data ,
178200 files = files , headers = headers )
179201 json = resp .json ()
0 commit comments