Skip to content
This repository was archived by the owner on Oct 4, 2023. It is now read-only.

Commit 19b5fb7

Browse files
committed
Progress on account endpoints
1 parent e919f86 commit 19b5fb7

3 files changed

Lines changed: 49 additions & 3 deletions

File tree

imgur-python/client.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import requests
22
from helpers.error import ImgurClientError
3+
from imgur.models.account import Account
4+
from imgur.models.gallery_album import GalleryAlbum
5+
from imgur.models.gallery_image import GalleryImage
36

47
API_URL = 'https://api.imgur.com/'
58

@@ -79,9 +82,34 @@ def make_request(self, method, route, data=None):
7982
try:
8083
response_data = response.json()
8184

82-
if 'data' in response_data and 'error' in response_data['data']:
83-
raise ImgurClientError(response_data['data'], response.status_code)
85+
if 'error' in response_data['data']:
86+
raise ImgurClientError(response_data['data']['error'], response.status_code)
8487
except:
8588
raise ImgurClientError('JSON decoding of response failed.')
8689

87-
return response_data['data']
90+
return response_data['data']
91+
92+
def validate_user_context(self, username):
93+
if username == 'me' and self.auth is None:
94+
raise ImgurClientError('\'me\' can only be used in the authenticated context.')
95+
96+
# Account-related endpoints
97+
def get_account(self, username):
98+
self.validate_user_context(username)
99+
account_data = self.make_request('GET', 'account/%s' % username)
100+
101+
return Account(
102+
account_data['id'],
103+
account_data['url'],
104+
account_data['bio'],
105+
account_data['reputation'],
106+
account_data['created'],
107+
account_data['pro_expiration'],
108+
)
109+
110+
def get_gallery_favorites(self, username):
111+
self.validate_user_context(username)
112+
favorites = self.make_request('GET', 'account/%s/gallery_favorites' % username)
113+
114+
result = [GalleryImage(favorite) for favorite in favorites]
115+
return result
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class GalleryAlbum:
2+
3+
# See documentation at https://api.imgur.com/ for available fields
4+
def __init__(self, *initial_data, **kwargs):
5+
for dictionary in initial_data:
6+
for key in dictionary:
7+
setattr(self, key, dictionary[key])
8+
for key in kwargs:
9+
setattr(self, key, kwargs[key])
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class GalleryImage:
2+
3+
# See documentation at https://api.imgur.com/ for available fields
4+
def __init__(self, *initial_data, **kwargs):
5+
for dictionary in initial_data:
6+
for key in dictionary:
7+
setattr(self, key, dictionary[key])
8+
for key in kwargs:
9+
setattr(self, key, kwargs[key])

0 commit comments

Comments
 (0)