diff options
| author | Shulhan <ms@kilabit.info> | 2022-04-21 01:39:56 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2022-04-21 01:39:56 +0700 |
| commit | 26bbadb4ca1c38f2d34a722b0c7d9509135a7eb2 (patch) | |
| tree | b0c0a3d3f7d6314f3a9a2798557ce6a2fc683d22 /client.go | |
| parent | d99356259a60ac0e2c345e60b3a6c92c3fb9d764 (diff) | |
| download | rescached-26bbadb4ca1c38f2d34a722b0c7d9509135a7eb2.tar.xz | |
all: implement HTTP API to enable or disable hosts on block.d
The URL /api/block.d/enable activate the hosts in block.d, while
The URL /api/block.d/disable deactivate the hosts in block.d.
Both of this API accept single parameter "name" in the body as
application/x-www-form-urlencoded.
Diffstat (limited to 'client.go')
| -rw-r--r-- | client.go | 60 |
1 files changed, 60 insertions, 0 deletions
@@ -32,6 +32,66 @@ func NewClient(serverUrl string, insecure bool) (cl *Client) { return cl } +// BlockdDisable disable specific hosts on block.d. +func (cl *Client) BlockdDisable(blockdName string) (an interface{}, err error) { + var ( + logp = "BlockdDisable" + res = libhttp.EndpointResponse{} + params = url.Values{} + + hb *hostsBlock + resb []byte + ) + + params.Set(paramNameName, blockdName) + + _, resb, err = cl.PostForm(apiBlockdDisable, nil, params) + if err != nil { + return nil, fmt.Errorf("%s: %w", logp, err) + } + + res.Data = &hb + 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: %d %s", logp, res.Code, res.Message) + } + + return hb, nil +} + +// BlockdEnable enable specific hosts on block.d. +func (cl *Client) BlockdEnable(blockdName string) (an interface{}, err error) { + var ( + logp = "BlockdEnable" + res = libhttp.EndpointResponse{} + params = url.Values{} + + hb *hostsBlock + resb []byte + ) + + params.Set(paramNameName, blockdName) + + _, resb, err = cl.PostForm(apiBlockdEnable, nil, params) + if err != nil { + return nil, fmt.Errorf("%s: %w", logp, err) + } + + res.Data = &hb + 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: %d %s", logp, res.Code, res.Message) + } + + return hb, nil +} + // BlockdUpdate fetch the latest hosts file from the hosts block // provider based on registered URL. func (cl *Client) BlockdUpdate(blockdName string) (an interface{}, err error) { |
