Skip to content

Commit f27b295

Browse files
committed
➕📝Add submit endpoint
2 parents f1cb1b4 + 92362ac commit f27b295

File tree

2 files changed

+78
-37
lines changed

2 files changed

+78
-37
lines changed

README.md

+45-14
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ A list of categories that the returned joke should fit in.
6363
Options are:
6464
`programming`,
6565
`miscellaneous`,
66+
<<<<<<< HEAD
6667
`dark`,
68+
=======
69+
`dark`
70+
>>>>>>> 92362ac33602ee9d5baf7e11f93507b473ebe207
6771
`pun`
6872

6973
If left blank it will default to use `Any`.
@@ -107,15 +111,15 @@ Options are:
107111

108112
If left blank it will default to `json`.
109113

110-
#### Example
114+
##### Example
111115

112116
```python
113117
joke = j.get_joke(response_format="xml") # Will return a joke in xml format.
114118
```
115119

116120
---
117121

118-
### type
122+
#### type
119123

120124
The type of joke returned.
121125
Options are:
@@ -124,21 +128,21 @@ Options are:
124128

125129
If left blank it will default to `Any`
126130

127-
#### Example
131+
##### Example
128132

129133
```python
130134
joke = j.get_joke(type="twopart") # Will return a twopart joke; both a setup and a delivery.
131135
```
132136

133137
---
134138

135-
### search_string
139+
#### search_string
136140

137141
A string to search for in jokes.
138142

139143
If left blank it will default to `None`
140144

141-
#### Example
145+
##### Example
142146

143147
```python
144148
joke = j.get_joke(search_string="the") # Will return a joke with the word "the" in it.
@@ -147,7 +151,7 @@ If left blank it will default to `None`
147151

148152
---
149153

150-
### id_range
154+
#### id_range
151155

152156
The range in which the selected joke should fall. ID's are decided by the order in which jokes are submitted.
153157
The argument passes should be in form of list or tuple, and should not exceed length of 2 items. First item
@@ -156,36 +160,63 @@ should be minimum 0. Maximum value can be determined [here](https://sv443.net/jo
156160
If left blank it will default to the maximum range.
157161

158162

159-
#### Example
163+
##### Example
160164

161165
```python
162166
joke = j.get_joke(id_range=[10,100]) # Will return a joke with the ID between 10 and 100.
163167
```
164168

165169
---
166170

167-
### auth_token
171+
#### amount
172+
173+
The amount of jokes you want the api to return. Will return them in a list. Maximum number is 10 jokes, and the
174+
api defaults to 1 if you use a number larger than the maximum. Defaults to 1.
175+
176+
177+
##### Example
178+
179+
```python
180+
joke = j.get_joke(amount=2) # Will return 2 jokes.
181+
```
182+
183+
---
184+
185+
#### lang
186+
187+
The language that the joke and response should be in. Currently supported languages are in the official api
188+
documentation. Defaults to en.
189+
190+
##### Example
191+
192+
```python
193+
joke = j.get_joke(lang="de")
194+
```
195+
196+
---
197+
198+
#### auth_token
168199

169200
A string token provided by the api owner. Using it will mean you are whitelisted by the api and can make
170201
more requests than normal users. Defaults to None
171202

172203

173-
#### Example
204+
##### Example
174205

175206
```python
176207
joke = j.get_joke(auth_token="aaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbb") # Will send the token to the api in a header.
177208
```
178209

179210
---
180211

181-
### user_agent
212+
#### user_agent
182213

183214
A string sent the the api that tells the api what browser you are (pretending to be). The default user agent
184215
is Mozilla Firefox from Windows 10 and should work fine, but the functionality is provided in case you wish
185216
to change it
186217

187218

188-
#### Example
219+
##### Example
189220

190221
```python
191222
joke = j.get_joke(user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0")
@@ -195,19 +226,19 @@ to change it
195226

196227
---
197228

198-
### return_headers
229+
#### return_headers
199230

200231
A boolean value (True or False) that tells the wrapper if you wish to receive headers in the return from the function.
232+
Will return a list instead of a single value
201233
Defaults to False.
202234

203235

204-
#### Example
236+
##### Example
205237

206238
```python
207239
response = j.get_joke(return_headers=True)
208240
joke = response[0]
209241
headers = response[1]
210-
# 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.
211242

212243
print(f"Joke: {joke}")
213244
print(f"Headers: {headers}")

jokeapi/main.py

+33-23
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ def build_request(
3333
category=[],
3434
blacklist=[],
3535
response_format="json",
36-
type=None,
37-
search_string=None,
38-
id_range=None,
36+
type="Any",
37+
search_string="",
38+
id_range=[],
3939
amount=1,
4040
lang="en"
4141
):
@@ -98,19 +98,19 @@ def build_request(
9898
return
9999
else:
100100
search_string = urllib.parse.quote(search_string)
101-
if id_range:
102-
range_limit = self.info["totalCount"]
101+
range_limit = self.info["totalCount"]
103102

104-
if len(id_range) > 2:
105-
raise ValueError("id_range must be no longer than 2 items.")
106-
elif id_range[0] < 0:
107-
raise ValueError(
108-
"id_range[0] must be greater than or equal to 0."
109-
)
110-
elif id_range[1] > range_limit:
111-
raise ValueError(
112-
f"id_range[1] must be less than or equal to {range_limit-1}."
113-
)
103+
if len(id_range) and (id_range[1] - id_range[0] > range_limit):
104+
raise ValueError(
105+
"id_range must be no longer than 2 items, \
106+
id_range[0] must be greater than or equal to 0 and \
107+
id_range[1] must be less than or equal to {range_limit-1}.")
108+
109+
if amount > 10:
110+
raise ValueError(
111+
f"amount parameter must be no greater than 10. \
112+
you passed {amount}."
113+
)
114114

115115
r += cats
116116

@@ -166,9 +166,11 @@ def send_request(self,
166166
print(data)
167167
raise
168168
else:
169-
if len(' '.join(re.split("error", data.lower().replace("\n", "NEWLINECHAR"))[0:][1:]).replace(
170-
'<', '').replace('/', '').replace(' ', '').replace(':', '').replace('>', '').replace('NEWLINECHAR', '\n')) == 4:
171-
return [Exception(f"API returned an error. Full response: \n\n {data}")]
169+
if len(' '.join(re.split("error", data.lower())[0:][1:]).replace(
170+
'<', '').replace('/', '').replace(' ', '').replace(':', '')
171+
.replace('>', '')) == 4:
172+
raise Exception(f"API returned an error. \
173+
Full response: \n\n {data}")
172174

173175
headers = str(r.headers).replace(r'\n', '').replace(
174176
'\n', '').replace(r'\\', '').replace(r"\'", '')[15:-1]
@@ -190,17 +192,25 @@ def get_joke(
190192
category=[],
191193
blacklist=[],
192194
response_format="json",
193-
type=None,
194-
search_string=None,
195-
id_range=None,
195+
type="Any",
196+
search_string="",
197+
id_range=[],
196198
amount=1,
197199
lang=None,
198200
auth_token=None,
199-
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0",
201+
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) \
202+
Gecko/20100101 Firefox/77.0",
200203
return_headers=False
201204
):
202205
r = self.build_request(
203-
category, blacklist, response_format, type, search_string, id_range, amount, lang
206+
category,
207+
blacklist,
208+
response_format,
209+
type,
210+
search_string,
211+
id_range,
212+
amount,
213+
lang
204214
)
205215

206216
response = self.send_request(

0 commit comments

Comments
 (0)