Usage of the synchronous client¶
Requires httpx package to be installed.
1"""
2`httpx` package is required.
3Since synchronous client is powered by `httpx.Client`.
4"""
5
6import logging
7
8# module is requiring external dependency:
9# so it can not be set in __init__ files
10# for easier importing
11from freedictionaryapi.clients.sync_client import DictionaryApiClient
12from freedictionaryapi.errors import DictionaryApiError
13# or `from freedictionaryapi import DictionaryApiError`
14from freedictionaryapi.languages import LanguageCodes
15# or `from freedictionaryapi import LanguageCodes`
16
17
18def main():
19 logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
20 logger = logging.getLogger(__name__)
21
22 # on initializing might be passed
23 # # - one of supported languages (listed in LanguageCodes enum that imported above)
24 # # - manually created httpx.Client
25 client = DictionaryApiClient()
26
27 # client have to be closed after all work
28 # like in the same way we close our
29 # web libraries` clients/sessions
30
31 # # it might be done with try-finally block
32 try:
33 # before watching of client methods
34 # important note:
35 # # each method that under hood
36 # # uses HTTP request to API
37 # # might raise correspond API error
38 # # if one has been occurred
39 # # so, you have to handle exceptions
40 # # that might be raised during processing.
41 # # for this we`ve imported errors above
42 # # for now, we`ll catch API errors with one
43 # # the big-scaled (common for all errors) exception
44 # # DictionaryApiError
45
46 # so, go next...
47
48 # # in client`s methods we have to pass word
49 # that we actually are searching info about
50 # and also
51 # if word`s language not the client default one
52 # we can pass it manually
53
54 # 1. default language
55 word = 'hello'
56
57 # # do not forger about errors handling
58 try:
59 parser = client.fetch_parser(word)
60 except DictionaryApiError:
61 logger.error('Oops! Here I might do some stuff for errors handling!')
62 else:
63 # parser has a lot of different data
64 # to explore see parser example and docs
65 word_transcription = parser.get_transcription()
66 print(f'Transcription of the word <{word}> is: {word_transcription}.')
67
68 # 2. set language for the particular word
69 word = 'привет'
70 language = LanguageCodes.RUSSIAN
71
72 try:
73 parser = client.fetch_parser(word, language)
74 except DictionaryApiError:
75 logger.error('Oops! Here I might do some stuff for errors handling!')
76 else:
77 word_definitions = parser.get_all_definitions()
78 word_definitions_formatted_string = '; '.join(word_definitions)
79 print(f'Definition[s] of the word <{word}> is/are: {word_definitions_formatted_string}')
80
81 finally:
82 client.close()
83
84 # # or with implemented context manager
85 # # Note:
86 # # on each usage of this context manager
87 # # you init new instance -> init new web client instance
88 # # so, client supposed to be created once for all program
89 with DictionaryApiClient() as client:
90 word = 'queue'
91 try:
92 # here instead of the parser
93 # we fetch word object
94 # that implements clear API JSON response
95 word = client.fetch_word(word)
96 except DictionaryApiError:
97 logger.error('Oops! Here I might do some stuff for errors handling!')
98 else:
99 print(f'Audio links of the word <{word}>:')
100 for phonetic in word.phonetics:
101 print(phonetic.audio)
102
103 # # # ---------------------------
104 # so, the most important methods are:
105 # - .fetch_parser() - that returns parser object
106 # - .fetch_word() - that returns word object
107 # also, it is some more methods
108 # but there are the most often needed
109 # since they implement high-level API
110 # that you want to use to
111
112
113if __name__ == '__main__':
114 main()