aboutsummaryrefslogtreecommitdiff
path: root/client.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-04-14 22:47:33 +0700
committerShulhan <ms@kilabit.info>2022-04-14 22:47:33 +0700
commit358c5ee72207c37712e44edcdb479c97f1266c8a (patch)
tree67f1603c68a289509a088083acf4c61e4886b4f2 /client.go
parent6ead9209bbc431b33ccbdebfffe98ffd23e4f9a7 (diff)
downloadrescached-358c5ee72207c37712e44edcdb479c97f1266c8a.tar.xz
cmd/resolver: implement caches command
The caches command fetch and print all caches from rescached server.
Diffstat (limited to 'client.go')
-rw-r--r--client.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/client.go b/client.go
new file mode 100644
index 0000000..0d626f1
--- /dev/null
+++ b/client.go
@@ -0,0 +1,60 @@
+// SPDX-FileCopyrightText: 2022 M. Shulhan <ms@kilabit.info>
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package rescached
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http"
+
+ "github.com/shuLhan/share/lib/dns"
+ libhttp "github.com/shuLhan/share/lib/http"
+)
+
+// Client for rescached.
+type Client struct {
+ *libhttp.Client
+}
+
+// NewClient create new HTTP client that connect to rescached HTTP server.
+func NewClient(serverUrl string, insecure bool) (cl *Client) {
+ var (
+ httpcOpts = libhttp.ClientOptions{
+ ServerUrl: serverUrl,
+ AllowInsecure: insecure,
+ }
+ )
+ cl = &Client{
+ Client: libhttp.NewClient(&httpcOpts),
+ }
+ return cl
+}
+
+// Caches fetch all of non-local caches from server.
+func (cl *Client) Caches() (answers []*dns.Answer, err error) {
+ var (
+ logp = "Caches"
+ res = libhttp.EndpointResponse{
+ Data: &answers,
+ }
+
+ resb []byte
+ )
+
+ _, resb, err = cl.Get(apiCaches, nil, nil)
+ if err != nil {
+ return nil, fmt.Errorf("%s: %w", logp, err)
+ }
+
+ err = json.Unmarshal(resb, &res)
+ if err != nil {
+ return nil, fmt.Errorf("%s: %w", logp, err)
+ }
+
+ if res.Code != http.StatusOK {
+ return nil, fmt.Errorf("%s: %s", logp, res.Code, res.Message)
+ }
+
+ return answers, nil
+}