@@ -86,6 +86,17 @@ def set_user_auth(self, access_token, refresh_token):
8686 def get_client_id (self ):
8787 return self .client_id
8888
89+ def get_auth_url (self , response_type = 'pin' ):
90+ return '%soauth2/authorize?client_id=%s&response_type=%s' % (API_URL , self .client_id , response_type )
91+
92+ def authorize (self , response , grant_type = 'pin' ):
93+ return self .make_request ('POST' , 'oauth2/token' , {
94+ 'client_id' : self .client_id ,
95+ 'client_secret' : self .client_secret ,
96+ 'grant_type' : grant_type ,
97+ grant_type : response
98+ }, True )
99+
89100 def prepare_headers (self , force_anon = False ):
90101 if force_anon or self .auth is None :
91102 if self .client_id is None :
@@ -100,7 +111,7 @@ def make_request(self, method, route, data=None, force_anon=False):
100111 method_to_call = getattr (requests , method )
101112
102113 header = self .prepare_headers (force_anon )
103- url = API_URL + '3/%s' % route
114+ url = API_URL + ( '3/%s' % route if 'oauth2' not in route else route )
104115
105116 if method in ('delete' , 'get' ):
106117 response = method_to_call (url , headers = header , params = data , data = data )
@@ -124,10 +135,10 @@ def make_request(self, method, route, data=None, force_anon=False):
124135 except :
125136 raise ImgurClientError ('JSON decoding of response failed.' )
126137
127- if isinstance (response_data ['data' ], dict ) and 'error' in response_data ['data' ]:
138+ if 'data' in response_data and isinstance (response_data ['data' ], dict ) and 'error' in response_data ['data' ]:
128139 raise ImgurClientError (response_data ['data' ]['error' ], response .status_code )
129140
130- return response_data ['data' ]
141+ return response_data ['data' ] if 'data' in response_data else response_data
131142
132143 def validate_user_context (self , username ):
133144 if username == 'me' and self .auth is None :
@@ -237,9 +248,9 @@ def get_account_image_ids(self, username, page=0):
237248 self .validate_user_context (username )
238249 return self .make_request ('GET' , 'account/%s/images/ids/%d' % (username , page ))
239250
240- def get_account_images_count (self , username , page = 0 ):
251+ def get_account_images_count (self , username ):
241252 self .validate_user_context (username )
242- return self .make_request ('GET' , 'account/%s/images/ids/%d ' % ( username , page ) )
253+ return self .make_request ('GET' , 'account/%s/images/count ' % username )
243254
244255 # Album-related endpoints
245256 def get_album (self , album_id ):
@@ -313,11 +324,9 @@ def post_comment_reply(self, comment_id, image_id, comment):
313324
314325 return self .make_request ('POST' , 'comment/%d' % comment_id , data )
315326
316- def comment_vote (self , comment_id , vote , toggle = True ):
327+ def comment_vote (self , comment_id , vote = 'up' ):
317328 self .logged_in ()
318- toggle_behavior = 1 if toggle else 0
319-
320- return self .make_request ('POST' , 'comment/%d/vote/%s?toggle=%d' % (comment_id , vote , toggle_behavior ))
329+ return self .make_request ('POST' , 'comment/%d/vote/%s' % (comment_id , vote ))
321330
322331 def comment_report (self , comment_id ):
323332 self .logged_in ()
@@ -642,6 +651,7 @@ def mark_notifications_as_read(self, notification_ids):
642651 self .logged_in ()
643652 return self .make_request ('POST' , 'notification' , ',' .join (notification_ids ))
644653
654+ # Memegen-related endpoints
645655 def default_memes (self ):
646656 response = self .make_request ('GET' , 'memegen/defaults' )
647657 return [Image (meme ) for meme in response ]
0 commit comments