1
1
# Copyright 2015 A Medium Corporation
2
+ from os .path import basename
2
3
try :
3
4
from urllib .parse import urlencode
4
5
except ImportError :
@@ -18,7 +19,6 @@ def __init__(self, application_id=None, application_secret=None,
18
19
self .application_secret = application_secret
19
20
self .access_token = access_token
20
21
21
-
22
22
def get_authorization_url (self , state , redirect_url , scopes ):
23
23
"""Get a URL for users to authorize the application.
24
24
@@ -37,7 +37,6 @@ def get_authorization_url(self, state, redirect_url, scopes):
37
37
38
38
return "https://medium.com/m/oauth/authorize?" + urlencode (qs )
39
39
40
-
41
40
def exchange_authorization_code (self , code , redirect_url ):
42
41
"""Exchange the authorization code for a long-lived access token, and
43
42
set the token on the current Client.
@@ -64,7 +63,6 @@ def exchange_authorization_code(self, code, redirect_url):
64
63
}
65
64
return self ._request_and_set_auth_code (data )
66
65
67
-
68
66
def exchange_refresh_token (self , refresh_token ):
69
67
"""Exchange the supplied refresh token for a new access token, and
70
68
set the token on the current Client.
@@ -88,10 +86,11 @@ def exchange_refresh_token(self, refresh_token):
88
86
}
89
87
return self ._request_and_set_auth_code (data )
90
88
91
-
92
89
def get_current_user (self ):
93
90
"""Fetch the data for the currently authenticated user.
94
91
92
+ Requires the ``basicProfile`` scope.
93
+
95
94
:returns: A dictionary with the users data ::
96
95
97
96
{
@@ -104,11 +103,12 @@ def get_current_user(self):
104
103
"""
105
104
return self ._request ("GET" , "/v1/me" )
106
105
107
-
108
106
def create_post (self , user_id , title , content , content_format , tags = None ,
109
107
canonical_url = None , publish_status = None , license = None ):
110
108
"""Create a post for the current user.
111
109
110
+ Requires the ``publishPost`` scope.
111
+
112
112
:param str user_id: The application-specific user ID as returned by
113
113
``get_current_user()``
114
114
:param str title: The title of the post
@@ -161,14 +161,32 @@ def create_post(self, user_id, title, content, content_format, tags=None,
161
161
path = "/v1/users/%s/posts" % user_id
162
162
return self ._request ("POST" , path , json = data )
163
163
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 )
164
183
165
184
def _request_and_set_auth_code (self , data ):
166
185
"""Request an access token and set it on the current client."""
167
186
result = self ._request ("POST" , "/v1/tokens" , form_data = data )
168
187
self .access_token = result ["access_token" ]
169
188
return result
170
189
171
-
172
190
def _request (self , method , path , json = None , form_data = None , files = None ):
173
191
"""Make a signed request to the given route."""
174
192
url = BASE_PATH + path
@@ -177,10 +195,7 @@ def _request(self, method, path, json=None, form_data=None, files=None):
177
195
"Accept-Charset" : "utf-8" ,
178
196
"Authorization" : "Bearer %s" % self .access_token ,
179
197
}
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
+
184
199
resp = requests .request (method , url , json = json , data = form_data ,
185
200
files = files , headers = headers )
186
201
json = resp .json ()
0 commit comments