diff options
Diffstat (limited to 'client.go')
| -rw-r--r-- | client.go | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/client.go b/client.go new file mode 100644 index 0000000..840b0e2 --- /dev/null +++ b/client.go @@ -0,0 +1,73 @@ +// SPDX-FileCopyrightText: 2025 M. Shulhan <ms@kilabit.info> +// SPDX-License-Identifier: GPL-3.0-only + +package lilin + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + + libhttp "git.sr.ht/~shulhan/pakakeh.go/lib/http" +) + +// Client of Lilin server. +type Client struct { + httpc *http.Client + Options ClientOptions +} + +// NewClient create new client using the provided options. +func NewClient(opts ClientOptions) (client *Client, err error) { + var logp = `NewClient` + + client = &Client{ + Options: opts, + } + + err = client.Options.init() + if err != nil { + return nil, fmt.Errorf(`%s: %w`, logp, err) + } + + client.httpc = &http.Client{ + Timeout: client.Options.Timeout, + } + return client, nil +} + +// ServicesSummary get the last report of all services from server. +func (client *Client) ServicesSummary() (summary []Service, err error) { + var logp = `ServicesSummary` + + var reqURL = *client.Options.serverURL + reqURL.Path = pathAPIServicesSummary + + var httpReq = http.Request{ + Method: `GET`, + URL: &reqURL, + } + var resp *http.Response + var respBody []byte + + resp, err = client.httpc.Do(&httpReq) + if err != nil { + return nil, fmt.Errorf(`%s: %w`, logp, err) + } + defer resp.Body.Close() + + respBody, err = io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf(`%s: %w`, logp, err) + } + + var httpResp = libhttp.EndpointResponse{ + Data: &summary, + } + err = json.Unmarshal(respBody, &httpResp) + if err != nil { + return nil, fmt.Errorf(`%s: %w`, logp, err) + } + return summary, nil +} |
