aboutsummaryrefslogtreecommitdiff
path: root/client.go
diff options
context:
space:
mode:
Diffstat (limited to 'client.go')
-rw-r--r--client.go100
1 files changed, 53 insertions, 47 deletions
diff --git a/client.go b/client.go
index 1646295..f411e5c 100644
--- a/client.go
+++ b/client.go
@@ -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
}