55xkcd-wrapper Client
66"""
77
8- import json
98import random
109import requests
10+ from .base_client import BaseClient
1111from .comic import Comic
1212from . import exceptions
1313
1414
15- class Client :
15+ class Client ( BaseClient ) :
1616 """
1717 Client communicates with the xkcd API, parses its response and generates Comic objects
1818
@@ -39,52 +39,6 @@ class Client:
3939 When calling get(id), get_latest() and random() if an http error or a timeout occurs
4040 """
4141
42- def __init__ (self ):
43- """
44- Client init
45- """
46- self ._base_url = 'https://xkcd.com/{}{}'
47- self ._api = 'info.0.json'
48- self ._explanation_wiki_url = 'https://www.explainxkcd.com/wiki/index.php/'
49-
50- def base_url (self ):
51- """
52- xkcd base API url
53-
54- Returns
55- -------
56- str
57- xkcd base API url
58- """
59- return self ._base_url .format ('' , '' )
60-
61- def latest_comic_url (self ):
62- """
63- xkcd API url for latest comic
64-
65- Returns
66- -------
67- str
68- xkcd API url for latest comic
69- """
70- return self ._base_url .format (self ._api , '' )
71-
72- def comic_id_url (self , comic_id ):
73- """
74- xkcd API url for comic with id = comic_id
75-
76- Parameters
77- ----------
78- comic_id : int
79- xkcd comic id
80-
81- Returns
82- -------
83- str
84- xkcd API url for comic with id = comic_id
85- """
86- return self ._base_url .format (str (comic_id ) + '/' , self ._api )
87-
8842 def get (self , comic_id ):
8943 """
9044 Retrieve an xkcd comic by id
@@ -203,62 +157,5 @@ def _request_comic(self, comic_id):
203157 raise exceptions .HttpError (xkcd_response .status_code , xkcd_response .reason )
204158 return xkcd_response .text
205159
206- def _parse_response (self , response ):
207- """
208- Parses xkcd API response
209-
210- Parameters
211- ----------
212- response : str
213- xkcd API json response as str
214-
215- Returns
216- -------
217- dict
218- relevant fields, extracted from xkcd API response, plus some extra ones
219-
220- Raises
221- ------
222- xkcd_wrapper.exceptions.BadResponseField
223- If response contained a field that could not be converted to int (after json decode)
224- """
225- # relation between xkcd-wrapper fields and xkcd API fields
226- fields_relationship = {
227- 'id' : 'num' ,
228- 'day' : 'day' ,
229- 'month' : 'month' ,
230- 'year' : 'year' ,
231- 'title' : 'safe_title' ,
232- 'description' : 'alt' ,
233- 'transcript' : 'transcript' ,
234- 'image' : 'img' ,
235- }
236-
237- json_response = json .loads (response )
238-
239- # all values default to None to avoid errors when the API returns missing data
240- parsed = dict ()
241-
242- for wrapper_value , api_value in fields_relationship .items ():
243- # if int conversion raises ValueError, something came wrong from the API
244- if wrapper_value in ('id' , 'day' , 'month' , 'year' ): # int
245- try :
246- parsed [wrapper_value ] = int (
247- json_response [api_value ]) if api_value in json_response else None
248- except ValueError as err :
249- raise exceptions .BadResponseField (wrapper_value , api_value , err )
250-
251- # everything converts to str, so no error here
252- else : # str
253- parsed [wrapper_value ] = str (
254- json_response [api_value ]) if api_value in json_response else None
255-
256- parsed ['link' ] = self ._base_url .format (
257- parsed ['id' ], '' ) if parsed ['id' ] is not None else None
258- parsed ['explanation' ] = '{}{}' .format (self ._explanation_wiki_url ,
259- parsed ['id' ]) if parsed ['id' ] is not None else None
260-
261- return parsed
262-
263160 def __repr__ (self ):
264161 return 'xkcd_wrapper.Client()'
0 commit comments