Dictionary API base for synchronous client

class freedictionaryapi.clients.sync_client.BaseDictionaryApiClient(default_language_code: freedictionaryapi.languages.LanguageCodes = <LanguageCodes.ENGLISH_US: 'en_US'>)[source]

Bases: freedictionaryapi.clients.base_client_interface.BaseDictionaryApiClientInterface

Implements base dictionary API client.

Abstract client that supposed to be inherited for sync clients.

abstract fetch_api_response(url: str)Tuple[int, Any][source]

Fetch data of the API response.

It is a abstract method that must implement:

  1. HTTP request to the API by given url (url that is generated by input params in invoked function);

  2. Get status code of the API response;

  3. Cast API response to python object with JSON decoding, apparently it is provided in all web libraries, need to do something like that:

    response: ResponseType
    json_response: Any = response.json()
    

The most important part is returning - method must return tuple of:

  1. integer code of the API response;

  2. python object loaded from API response with JSON decoding.

So:

  • integer code of the API response will be analyzed and correspond error will be raised if API returned one, so response is unsuccessful;

  • python object that expresses API response will be parsed, so all information will be available.

Finally, it should look like (pseudo code below):

# make request just with library
response: ResponseType
response = weblib.get(url)
response_status_code = response.status_code
json_response = response.json()

data_to_return = (response_status_code, json_response)

return data_to_return
# or maybe use some client/session
# that have been passed on initialization
response: ResponseType
with self._client.get(url) as response:
    response_status_code = response.status_code
    json_response = response.json()

data_to_return = (response_status_code, json_response)

return data_to_return

If it is still any questions you can also see some examples or just implementations of ready to use clients.

Parameters

url (str) – url that is generated by input params in invoked function

Returns

tuple of:

  • response status code;

  • python object loaded from API response with JSON decoding.

Return type

tuple[int, Any]

fetch_json(word: str, language_code: Optional[freedictionaryapi.languages.LanguageCodes] = None)Any[source]

Fetch API JSON response that loaded in Python object (response.json()).

Parameters
  • word (str) – searched word

  • language_code (Optional[LanguageCodes]) – language of the searched word

Returns

JSON response (supposed to be list or dict)

Return type

Any

Raise
DictionaryApiError

when unsuccessful status code got of API request

fetch_parser(word: str, language_code: Optional[freedictionaryapi.languages.LanguageCodes] = None)freedictionaryapi.parsers.response_parser.DictionaryApiParser[source]

Fetch dictionary API parser.

Parameters
  • word (str) – searched word

  • language_code (Optional[LanguageCodes]) – language of the searched word (word)

Returns

dictionary API parser

Return type

DictionaryApiParser

fetch_word(word: str, language_code: Optional[freedictionaryapi.languages.LanguageCodes] = None)freedictionaryapi.types.word.Word[source]

Fetch word (Word) - parsed object that has all word info.

Shortcut for the DictionaryApiParser.word.

Parameters
  • word (str) – searched word

  • language_code (Optional[LanguageCodes]) – language of the searched word

Returns

word (parsed object)

Return type

Word