Skip to content

Commit 7f2b855

Browse files
authored
Release v1.1 (#2)
1 parent 81e0e5d commit 7f2b855

File tree

11 files changed

+1099
-20
lines changed

11 files changed

+1099
-20
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ docs/_build/
3434

3535
# PyCharm files
3636
.idea
37+
test.py

byte_api/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from .byte_api import ByteApi
1+
from .client import Client
2+
from .types import *
23

34

45
__author__ = 'bixnel'
5-
__version__ = '1.0'
6+
__version__ = '1.1'

byte_api/api.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import json
2+
import requests
3+
4+
5+
class Api(object):
6+
def __init__(self, token, headers=None):
7+
self.token = token
8+
self.API_URL = 'https://api.byte.co/'
9+
if headers:
10+
self.headers = headers + {'Authorization': token}
11+
else:
12+
self.headers = {
13+
'User-Agent': 'byte/0.2 (co.byte.video; build:145; '
14+
'iOS 13.3.0) Alamofire/4.9.1',
15+
'Authorization': token
16+
}
17+
18+
@staticmethod
19+
def check_response(response):
20+
if not response:
21+
raise ValueError('empty response from API, check your token')
22+
23+
def get(self, url, params=None, check_response=True):
24+
response = requests.get(self.API_URL + url,
25+
params,
26+
headers=self.headers).text
27+
if check_response:
28+
self.check_response(response)
29+
return json.loads(response)
30+
31+
def post(self, url, data=None, json_data=None, check_response=True):
32+
response = requests.post(self.API_URL + url,
33+
data,
34+
json_data,
35+
headers=self.headers).text
36+
if check_response:
37+
self.check_response(response)
38+
return json.loads(response)
39+
40+
def put(self, url, data=None, check_response=True):
41+
response = requests.put(self.API_URL + url,
42+
data,
43+
headers=self.headers).text
44+
if check_response:
45+
self.check_response(response)
46+
return json.loads(response)
47+
48+
def delete(self, url, check_response=True):
49+
response = requests.delete(self.API_URL + url,
50+
headers=self.headers).text
51+
if check_response:
52+
self.check_response(response)
53+
return json.loads(response)

byte_api/byte_api.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

byte_api/client.py

Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
from .api import Api
2+
from .types import *
3+
4+
5+
class Client(object):
6+
"""
7+
Initializes the API for the client
8+
9+
:param token: Authorization token
10+
:type token: str
11+
12+
:param headers: Additional headers **except Authorization**
13+
:type headers: dict
14+
"""
15+
16+
def __init__(self, token: str, headers=None):
17+
"""
18+
Initializes the API for the client
19+
20+
:param token: Authorization token
21+
:type token: str
22+
"""
23+
self.api = Api(token, headers)
24+
25+
def follow(self, id: str) -> Response:
26+
"""
27+
Subscribes to a user
28+
29+
:param id: User id
30+
:type id: str
31+
32+
33+
:rtype: :class:`Response`
34+
"""
35+
response = self.api.put('account/id/{}/follow'.format(id))
36+
return Response.de_json(response)
37+
38+
def unfollow(self, id: str) -> Response:
39+
"""
40+
Unsubscribes to a user
41+
42+
:param id: User id
43+
:type id: str
44+
45+
46+
:rtype: :class:`Response`
47+
"""
48+
response = self.api.delete('account/id/{}/follow'.format(id))
49+
return Response.de_json(response)
50+
51+
def get_user(self, id: str) -> Response:
52+
"""
53+
Gets a user profile
54+
55+
:param id: User id
56+
:type id: str
57+
58+
59+
:rtype: :class:`Response`, :class:`Account`
60+
"""
61+
response = self.api.get('account/id/{}'.format(id))
62+
response = Response.de_json(response)
63+
data = None
64+
error = None
65+
if hasattr(response, 'data'):
66+
data = Account.de_json(response.data)
67+
if hasattr(response, 'error'):
68+
error = Error.de_json(response.error)
69+
return Response(response.success, data=data, error=error)
70+
71+
def like(self, id: str) -> Response:
72+
"""
73+
Likes a byte
74+
75+
:param id: Byte (post) id
76+
:type id: str
77+
78+
79+
:rtype: :class:`Response`
80+
"""
81+
response = self.api.put('post/id/{}/feedback/like'.format(id))
82+
return Response.de_json(response)
83+
84+
def dislike(self, id: str) -> Response:
85+
"""
86+
Removes like from a byte
87+
88+
:param id: Byte (post) id
89+
:type id: str
90+
91+
92+
:rtype: :class:`Response`
93+
"""
94+
response = self.api.delete('post/id/{}/feedback/like'.format(id))
95+
return Response.de_json(response)
96+
97+
def comment(self, id: str, text: str) -> Response:
98+
"""
99+
Comments a byte
100+
101+
:param id: Byte (post) id
102+
:type id: str
103+
104+
:param text: Comment text
105+
:type id: str
106+
107+
108+
:rtype: :class:`Response`, :class:`Comment`
109+
"""
110+
response = self.api.post('post/id/{}/feedback/comment'.format(id),
111+
json_data={
112+
'postID': id,
113+
'body': text
114+
})
115+
response = Response.de_json(response)
116+
data = None
117+
error = None
118+
if hasattr(response, 'data'):
119+
data = Comment.de_json(response.data)
120+
if hasattr(response, 'error'):
121+
error = Error.de_json(response.error)
122+
return Response(response.success, data=data, error=error)
123+
124+
def delete_comment(self, id: str) -> Response:
125+
"""
126+
Deletes a comment
127+
128+
:param id: Comment id patterned by **{post id}-{comment id}**
129+
:type id: str
130+
131+
132+
:rtype: :class:`Response`
133+
"""
134+
response = self.api.post('feedback/comment/id/{}'.format(id),
135+
json_data={
136+
'commentID': id
137+
})
138+
response = Response.de_json(response)
139+
return Response.de_json(response)
140+
141+
def loop(self, id: str) -> Response:
142+
"""
143+
Increments loop counter
144+
145+
:param id: Byte (post) id
146+
:type id: str
147+
148+
149+
:rtype: :class:`Response`
150+
"""
151+
response = self.api.post('post/id/{}/loop'.format(id))
152+
response = Response.de_json(response)
153+
data = None
154+
error = None
155+
if hasattr(response, 'data'):
156+
data = LoopCounter.de_json(response.data)
157+
if hasattr(response, 'error'):
158+
error = Error.de_json(response.error)
159+
return Response(response.success, data=data, error=error)
160+
161+
def rebyte(self, id: str) -> Response:
162+
"""
163+
Increments loop counter
164+
165+
:param id: Byte (post) id
166+
:type id: str
167+
168+
169+
:rtype: :class:`Response`
170+
"""
171+
response = self.api.post('rebyte',
172+
json_data={
173+
'postID': id
174+
})
175+
response = Response.de_json(response)
176+
data = None
177+
error = None
178+
if hasattr(response, 'data'):
179+
data = Rebyte.de_json(response.data)
180+
if hasattr(response, 'error'):
181+
error = response.error
182+
return Response(response.success, data=data, error=error)
183+
184+
def get_colors(self) -> Response:
185+
"""
186+
Gets available color schemes
187+
188+
189+
:rtype: :class:`Response`
190+
"""
191+
response = self.api.get('account/me/colors')
192+
response = Response.de_json(response)
193+
data = None
194+
error = None
195+
if hasattr(response, 'data'):
196+
data = Colors.de_json(response.data)
197+
if hasattr(response, 'error'):
198+
error = response.error
199+
return Response(response.success, data=data, error=error)
200+
201+
def set_info(self, bio: str = None, display_name: str = None,
202+
username: str = None, color_scheme: int = None) -> Response:
203+
"""
204+
Sets profile info
205+
206+
:param bio: New bio
207+
:type bio: str
208+
209+
:param display_name: New name to display
210+
:type display_name: str
211+
212+
:param username: New username
213+
:type username: str
214+
215+
:param color_scheme: Id of new color scheme
216+
:type color_scheme: int
217+
218+
219+
:rtype: :class:`Response`
220+
"""
221+
data = {}
222+
if bio:
223+
data['bio'] = bio
224+
if display_name:
225+
data['displayName'] = display_name
226+
if username:
227+
data['username'] = username
228+
if color_scheme:
229+
data['colorScheme'] = color_scheme
230+
response = self.api.put('account/me',
231+
data=data)
232+
return Response.de_json(response)
233+
234+
def set_username(self, username: str) -> Response:
235+
"""
236+
Sets username
237+
238+
:param username: New username
239+
:type username: str
240+
241+
242+
:rtype: :class:`Response`
243+
"""
244+
return self.set_info(username=username)
245+
246+
def set_bio(self, bio: str) -> Response:
247+
"""
248+
Sets bio
249+
250+
:param bio: New bio
251+
:type bio: str
252+
253+
254+
:rtype: :class:`Response`
255+
"""
256+
return self.set_info(bio=bio)
257+
258+
def set_display_name(self, display_name: str) -> Response:
259+
"""
260+
Sets name to display
261+
262+
:param display_name: New name to display
263+
:type display_name: str
264+
265+
266+
:rtype: :class:`Response`
267+
"""
268+
return self.set_info(display_name=display_name)
269+
270+
def set_color_scheme(self, color_scheme: int) -> Response:
271+
"""
272+
Sets color scheme
273+
274+
:param color_scheme: Id of new color scheme
275+
:type color_scheme: str
276+
277+
278+
:rtype: :class:`Response`
279+
"""
280+
return self.set_info(color_scheme=color_scheme)

0 commit comments

Comments
 (0)