Skip to content

Commit 280d05d

Browse files
committed
➕📝 Added validation of tokens and updated readme.md
1 parent 5eff52a commit 280d05d

File tree

3 files changed

+69
-21
lines changed

3 files changed

+69
-21
lines changed

README.md

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ while also maintaining a readable codebase.
1010

1111
You can install jokeapi through [pip](https://pypi.org/project/pip/) by using `pip install jokeapi`
1212

13-
So far there are no build from source instructions.
14-
1513
---
1614

1715
# get_joke
@@ -29,7 +27,8 @@ interact with one function. This function is `get_joke()`
2927
from jokeapi import Jokes # Import the Jokes class
3028

3129
j = Jokes() # Initialise the class
32-
j.get_joke() # Retrieve a random joke
30+
joke = j.get_joke(type="single", response_format="txt") # Retrieve a random joke
31+
print(joke)
3332
```
3433

3534
### Parameters
@@ -129,15 +128,66 @@ If left blank it will default to `None`
129128

130129
The range in which the selected joke should fall. ID's are decided by the order in which jokes are submitted.
131130
The argument passes should be in form of list or tuple, and should not exceed length of 2 items. First item
132-
should be minimum 0.
131+
should be minimum 0. Maximum value can be determined [here](https://sv443.net/jokeapi/v2/info)
133132

134133
If left blank it will default to the maximum range.
135134

136135

137136
#### Example
138137

139138
```python
140-
joke = get_joke(id_range=[10,100]) # Will return a joke with the ID between 10 and 100
139+
joke = get_joke(id_range=[10,100]) # Will return a joke with the ID between 10 and 100.
140+
```
141+
142+
---
143+
144+
### auth_token
145+
146+
A string token provided by the api owner. Using it will mean you are whitelisted by the api and can make
147+
more requests than normal users. Defaults to None
148+
149+
150+
#### Example
151+
152+
```python
153+
joke = get_joke(auth_token="aaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbb") # Will send the token to the api in a header.
154+
```
155+
156+
---
157+
158+
### user_agent
159+
160+
A string sent the the api that tells the api what browser you are (pretending to be). The default user agent
161+
is Mozilla Firefox from Windows 10 and should work fine, but the functionality is provided in case you wish
162+
to change it
163+
164+
165+
#### Example
166+
167+
```python
168+
joke = get_joke(user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0")
169+
# This is in fact the default user agent, and tells the API that we are visitng the page from a Firefox 77.0
170+
# browser using Windows 10 64bit.
171+
```
172+
173+
---
174+
175+
### return_headers
176+
177+
A boolean value (True or False) that tells the wrapper if you wish to receive headers in the return from the function.
178+
Defaults to False.
179+
180+
181+
#### Example
182+
183+
```python
184+
response = get_joke(return_headers=True)
185+
joke = response[0]
186+
headers = response[1]
187+
# The function returns the joke and then the headers using the "return x, y" syntax, so you can index it like a list or tuple.
188+
189+
print(f"Joke: {joke}")
190+
print(f"Headers: {headers}")
141191
```
142192

143193
---
@@ -237,7 +287,7 @@ If not, feel free to ask me through one of the channels provided below.
237287

238288
Developer contact:
239289

240-
[Discord](https://discord.gg/mB989eP)
290+
![Discord](https://discord.com/assets/07dca80a102d4149e9736d4b162cff6f.ico)[**Discord**](https://discord.gg/mB989eP)
241291

242292
[Issue Tracker](https://github.com/thenamesweretakenalready/Sv443s-JokeAPI-Python-Wrapper/issues)
243293

jokeapi/main.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,26 +131,24 @@ def build_request(
131131
return r
132132

133133
def send_request(self, request, response_format, return_headers, auth_token, user_agent):
134+
returns = []
135+
134136
if auth_token:
135137
r = self.http.request('GET', request, headers={'Authorization': str(
136138
auth_token), 'user-agent': str(user_agent)})
137139
else:
138140
r = self.http.request('GET', request, headers={'user-agent': str(user_agent)})
139141

142+
headers = r.headers
143+
returns.append(r.data)
144+
140145
if return_headers:
141-
if response_format == "json":
142-
return r.data, r.headers
143-
elif response_format == "xml":
144-
return r.data, r.headers
145-
else:
146-
return r.data, r.headers
147-
else:
148-
if response_format == "json":
149-
return r.data
150-
elif response_format == "xml":
151-
return r.data
152-
else:
153-
return r.data
146+
returns.append(headers)
147+
148+
if not headers["Token-Valid"]:
149+
returns.append({"Token-Valid": False})
150+
151+
return returns
154152

155153
def get_joke(
156154
self,

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
setuptools.setup(
77
name="jokeapi",
88
packages=["jokeapi"],
9-
version="0.2.1",
9+
version="0.2.2",
1010
license="GNU General Public License v3 (GPLv3)",
1111
description="An API Wrapper for Sv443's JokeAPI",
1212
long_description=long_description,
1313
long_description_content_type="text/markdown",
1414
author="thenamesweretakenalready",
1515
author_email="[email protected]",
1616
url="""https://github.com/thenamesweretakenalready/Sv443s-JokeAPI-Python-Wrapper""",
17-
download_url="https://github.com/user/Sv443s-JokeAPI-Python-Wrapper/archive/v0.1.5.tar.gz",
17+
download_url="https://github.com/user/Sv443s-JokeAPI-Python-Wrapper/archive/v0.2.2.tar.gz",
1818
keywords=["api wrapper", "wrapper", "api", "jokes", "python", "joke api"],
1919
install_requires=setuptools.find_packages(),
2020
classifiers=[

0 commit comments

Comments
 (0)