diff options
| author | Shulhan <ms@kilabit.info> | 2024-04-18 00:33:01 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2024-04-18 00:41:07 +0700 |
| commit | 1a6d02199e0d44024648c5e805f8f10d7fc4b30a (patch) | |
| tree | 34918af7ac3bcf2777ba2d9744f801a2243edb86 | |
| parent | a28f1684d9b427149f7a8f03f865bdf57eafc08e (diff) | |
| download | kamusku-1a6d02199e0d44024648c5e805f8f10d7fc4b30a.tar.xz | |
all: remove unused Client and activeClient
Since we now only have one client, apiClient, rename it to Client so it
can be used by other module or package later.
| -rw-r--r-- | active_client.go | 14 | ||||
| -rw-r--r-- | api_client.go | 86 | ||||
| -rw-r--r-- | client.go | 100 | ||||
| -rw-r--r-- | client_test.go (renamed from api_client_test.go) | 46 | ||||
| -rw-r--r-- | telegram_bot.go | 6 |
5 files changed, 79 insertions, 173 deletions
diff --git a/active_client.go b/active_client.go deleted file mode 100644 index bd9f189..0000000 --- a/active_client.go +++ /dev/null @@ -1,14 +0,0 @@ -// SPDX-FileCopyrightText: 2020 M. Shulhan <ms@kilabit.info> -// SPDX-License-Identifier: GPL-3.0-or-later - -package kamusku - -import ( - "git.sr.ht/~shulhan/kbbi" -) - -// activeClient define an interface for an active client. -type activeClient interface { - Lookup(words []string) (res kbbi.LookupResponse, err error) - ListRootWords() (rootWords kbbi.Words, err error) -} 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 -} @@ -4,81 +4,87 @@ package kamusku import ( + "encoding/json" "fmt" + "io" + "net/http" + "net/url" + "strings" "git.sr.ht/~shulhan/kbbi" ) -// Client for dictionary API and official KBBI server. +// Client is client that connect to cached server API. type Client struct { - active activeClient - api *apiClient - kbbic *kbbi.Client + url string + conn *http.Client } -// NewClient create and initialize new client. -func NewClient() (cl *Client, err error) { - cl = &Client{} - - cl.kbbic, err = kbbi.NewClient() - if err != nil { - return nil, err +// NewClient create a new client for server API. +// If url is empty it will use default server API. +func NewClient(url string) (client *Client) { + if len(url) == 0 { + url = defServerAPI } - cl.api = newAPIClient("") - - if cl.IsAuthenticated() { - cl.active = cl.kbbic - } else { - cl.active = cl.api + client = &Client{ + url: url, + conn: &http.Client{ + Timeout: defTimeout, + }, } - return cl, nil + return client } -// Lookup lookup definition of words. -func (cl *Client) Lookup(words []string) (res kbbi.LookupResponse, err error) { - if cl.active != nil { - return cl.active.Lookup(words) +// Lookup the definition of words through server API. +func (client *Client) Lookup(words []string) (res kbbi.LookupResponse, err error) { + if len(words) == 0 { + return nil, nil } - res, err = cl.api.Lookup(words) + var ( + logp = `Lookup` + params = url.Values{} + ) + + params.Set(paramNameKata, strings.Join(words, `,`)) + + req, err := http.NewRequest(http.MethodGet, client.url+pathAPIDefinisi, nil) if err != nil { - return cl.kbbic.Lookup(words) + return nil, fmt.Errorf(`%s: %w`, logp, err) } - return res, nil -} + req.URL.RawQuery = params.Encode() -// IsAuthenticated will return true if client has logged in to KBBI official -// server. -func (cl *Client) IsAuthenticated() bool { - return cl.kbbic.IsAuthenticated() -} - -// ListRootWords list all of the root words in dictionary. -func (cl *Client) ListRootWords() (res kbbi.Words, err error) { - if cl.active != nil { - return cl.active.ListRootWords() + httpRes, err := client.conn.Do(req) + if err != nil { + return nil, fmt.Errorf(`%s: %w`, logp, err) } - res, err = cl.api.ListRootWords() + var resBody []byte + + resBody, err = io.ReadAll(httpRes.Body) if err != nil { - return cl.kbbic.ListRootWords() + return nil, fmt.Errorf(`%s: %w`, logp, err) } - return res, nil -} + defer httpRes.Body.Close() + + if len(resBody) == 0 { + return res, nil + } -// Login authenticate the client using username and password to official KBBI -// server. -func (cl *Client) Login(user, pass string) (err error) { - err = cl.kbbic.Login(user, pass) + err = json.Unmarshal(resBody, &res) if err != nil { - return fmt.Errorf("Login: %w", err) + return nil, fmt.Errorf(`%s: %w`, logp, err) } - cl.active = cl.kbbic + return res, nil +} - return nil +// ListRootWords list all of the root words in dictionary. +func (client *Client) ListRootWords() (res kbbi.Words, err error) { + //TODO: return cached list. + return res, nil } diff --git a/api_client_test.go b/client_test.go index a56175c..aaf6ed3 100644 --- a/api_client_test.go +++ b/client_test.go @@ -13,7 +13,7 @@ import ( func TestApiClient_Lookup_offline(t *testing.T) { testServer.offline = true - client := newAPIClient(testServerAPI) + client := NewClient(testServerAPI) cases := []struct { desc string @@ -21,21 +21,21 @@ func TestApiClient_Lookup_offline(t *testing.T) { exp kbbi.LookupResponse expError string }{{ - desc: "With empty input", + desc: `With empty input`, }, { - desc: "With words not found", - words: []string{"a"}, + desc: `With words not found`, + words: []string{`a`}, }, { - desc: "With valid word in cache", - words: []string{"mengeja"}, + desc: `With valid word in cache`, + words: []string{`mengeja`}, exp: kbbi.LookupResponse{ - "mengeja": testKataMengeja, + `mengeja`: testKataMengeja, }, }, { - desc: "With duplicate words", - words: []string{"mengeja", "mengeja"}, + desc: `With duplicate words`, + words: []string{`mengeja`, `mengeja`}, exp: kbbi.LookupResponse{ - "mengeja": testKataMengeja, + `mengeja`: testKataMengeja, }, }} @@ -57,7 +57,7 @@ func TestApiClient_Lookup_online(t *testing.T) { testServer.offline = false - client := newAPIClient(testServerAPI) + client := NewClient(testServerAPI) cases := []struct { desc string @@ -65,28 +65,28 @@ func TestApiClient_Lookup_online(t *testing.T) { exp kbbi.LookupResponse expError string }{{ - desc: "With empty input", + desc: `With empty input`, }, { - desc: "With valid word in cache", - words: []string{"mengeja"}, + desc: `With valid word in cache`, + words: []string{`mengeja`}, exp: kbbi.LookupResponse{ - "mengeja": testKataMengeja, + `mengeja`: testKataMengeja, }, }, { - desc: "With duplicate words", - words: []string{"mengeja", "mengeja"}, + desc: `With duplicate words`, + words: []string{`mengeja`, `mengeja`}, exp: kbbi.LookupResponse{ - "mengeja": testKataMengeja, + `mengeja`: testKataMengeja, }, }, { - desc: "With one of the word not in cache", - words: []string{"mengeja", "eja"}, + desc: `With one of the word not in cache`, + words: []string{`mengeja`, `eja`}, exp: kbbi.LookupResponse{ `mengeja`: testKataMengeja, `eja`: &kbbi.Word{ Definition: []*kbbi.WordDefinition{{ - Value: "lafal huruf satu demi satu", - Classes: []string{"Verba: kata kerja"}, + Value: `lafal huruf satu demi satu`, + Classes: []string{`Verba: kata kerja`}, }}, }, }, @@ -102,7 +102,7 @@ func TestApiClient_Lookup_online(t *testing.T) { } for k, v := range got { - t.Logf("got: %s = %+v", k, v) + t.Logf(`got: %s = %+v`, k, v) } test.Assert(t, `LookupResponse`, c.exp, got) diff --git a/telegram_bot.go b/telegram_bot.go index b01b66e..4cbae7a 100644 --- a/telegram_bot.go +++ b/telegram_bot.go @@ -24,13 +24,13 @@ const ( // TelegramBot implement Telegram Bot for KBBI. type TelegramBot struct { *bot.Bot - apiClient *apiClient + client *Client } // NewTelegramBot create and initialize new Telegram Bot. func NewTelegramBot(token, webhookURL string) (tgbot *TelegramBot, err error) { tgbot = &TelegramBot{ - apiClient: newAPIClient(""), + client: NewClient(``), } var commands = []bot.Command{{ @@ -102,7 +102,7 @@ func (tgbot *TelegramBot) handleCommandDefinisi(update bot.Update) { daftarKata := strings.Split(msgReq.CommandArgs, ",") - def, err := tgbot.apiClient.Lookup(daftarKata) + def, err := tgbot.client.Lookup(daftarKata) if err != nil { tgbot.sendError(msgReq, "", err.Error()) return |
