diff options
| author | Shulhan <m.shulhan@gmail.com> | 2020-04-12 04:35:58 +0700 |
|---|---|---|
| committer | Shulhan <m.shulhan@gmail.com> | 2020-04-12 04:35:58 +0700 |
| commit | 9cf57fc952e4161c8e7fc5c9de8869d3137acb20 (patch) | |
| tree | 1fe91b6316ea34ff6600878f7d12b474da290361 /client.go | |
| parent | c6f1ebdc315eda5f41fd7a3f30c693b243882be8 (diff) | |
| download | kamusku-9cf57fc952e4161c8e7fc5c9de8869d3137acb20.tar.xz | |
all: refactoring client to load cookies automatically
When creating new client, the directClient will autoload cookies in
predefined location.
If Login is called, the cookies will autosave directly to predefined
location.
This will allow the server to use authenticated directClient to mitigate
the limit on KBBI official server.
Diffstat (limited to 'client.go')
| -rw-r--r-- | client.go | 40 |
1 files changed, 17 insertions, 23 deletions
@@ -5,7 +5,7 @@ package kbbi import ( - "net/http" + "fmt" ) // @@ -19,19 +19,18 @@ type Client struct { // // NewClient create and initialize new client. -// If cookies is not empty, the direct client will be initialized and actived. // -func NewClient(cookies []*http.Cookie) (cl *Client, err error) { +func NewClient() (cl *Client, err error) { cl = &Client{} - cl.direct, err = newDirectClient(cookies) + cl.direct, err = newDirectClient() if err != nil { return nil, err } cl.api = newAPIClient("") - if cookies != nil { + if cl.IsAuthenticated() { cl.active = cl.direct } else { cl.active = cl.api @@ -59,6 +58,14 @@ func (cl *Client) CariDefinisi(words []string) ( } // +// IsAuthenticated will return true if client has logged in to KBBI official +// server. +// +func (cl *Client) IsAuthenticated() bool { + return cl.direct.isAuthenticated() +} + +// // ListKataDasar list all of the root words in dictionary. // func (cl *Client) ListKataDasar() (res DaftarKata, err error) { @@ -78,26 +85,13 @@ func (cl *Client) ListKataDasar() (res DaftarKata, err error) { // Login authenticate the client using username and password to official KBBI // server. // -func (cl *Client) Login(user, pass string) (cookies []*http.Cookie, err error) { - if cl.direct == nil { - cl.direct, err = newDirectClient(nil) - if err != nil { - return nil, err - } - } - cookies, err = cl.direct.login(user, pass) +func (cl *Client) Login(user, pass string) (err error) { + err = cl.direct.login(user, pass) if err != nil { - return nil, err + return fmt.Errorf("Login: %w", err) } + cl.active = cl.direct - return cookies, nil -} -// -// SetCookies for HTTP request in direct client. -// -func (cl *Client) SetCookies(cookies []*http.Cookie) { - if cl.direct != nil { - cl.direct.setCookies(cookies) - } + return nil } |
