diff options
Diffstat (limited to 'api_client.go')
| -rw-r--r-- | api_client.go | 86 |
1 files changed, 0 insertions, 86 deletions
diff --git a/api_client.go b/api_client.go deleted file mode 100644 index 77fce29..0000000 --- a/api_client.go +++ /dev/null @@ -1,86 +0,0 @@ -// SPDX-FileCopyrightText: 2020 M. Shulhan <ms@kilabit.info> -// SPDX-License-Identifier: GPL-3.0-or-later - -package kamusku - -import ( - "encoding/json" - "fmt" - "io" - "net/http" - "net/url" - "strings" - - "git.sr.ht/~shulhan/kbbi" -) - -// apiClient is client that connect to cached server API. -type apiClient struct { - url string - conn *http.Client -} - -// newAPIClient create a new client for server API. -// If url is empty it will use default server API. -func newAPIClient(url string) (client *apiClient) { - if len(url) == 0 { - url = defServerAPI - } - - client = &apiClient{ - url: url, - conn: &http.Client{ - Timeout: defTimeout, - }, - } - - return client -} - -// Lookup the definition of words through server API. -func (client *apiClient) Lookup(words []string) (res kbbi.LookupResponse, err error) { - if len(words) == 0 { - return nil, nil - } - - params := url.Values{} - params.Set(paramNameKata, strings.Join(words, ",")) - - req, err := http.NewRequest(http.MethodGet, client.url+pathAPIDefinisi, nil) - if err != nil { - return nil, fmt.Errorf("Lookup: %w", err) - } - - req.URL.RawQuery = params.Encode() - - httpRes, err := client.conn.Do(req) - if err != nil { - return nil, fmt.Errorf("Lookup: %w", err) - } - - var resBody []byte - - resBody, err = io.ReadAll(httpRes.Body) - if err != nil { - return nil, fmt.Errorf("Lookup: %w", err) - } - - defer httpRes.Body.Close() - - if len(resBody) == 0 { - return res, nil - } - - err = json.Unmarshal(resBody, &res) - if err != nil { - return nil, fmt.Errorf("Lookup: %w", err) - } - - return res, nil -} - -// ListRootWords list all of the root words in dictionary. -func (client *apiClient) ListRootWords() (res kbbi.Words, err error) { - //TODO: return cached list. - return res, nil -} |
