feat: new, more customizable client supporting API Key and user/password authentication

This commit is contained in:
Mateusz Filipowicz
2025-02-09 01:51:47 +01:00
committed by Mateusz Filipowicz
parent 8a0cf33e75
commit f79f21c4ad
87 changed files with 1286 additions and 960 deletions
+14 -14
View File
@@ -67,11 +67,11 @@ func (dst *{{ $k }}) UnmarshalJSON(b []byte) error {
{{ if not .IsSetting }}
func (c *Client) list{{ .StructName }}(ctx context.Context, site string) ([]{{ .StructName }}, error) {
var respBody struct {
Meta meta `json:"meta"`
Meta Meta `json:"meta"`
Data []{{ .StructName }} `json:"data"`
}
err := c.do(ctx, "GET", fmt.Sprintf("s/%s/{{ if eq .StructName "Device" }}stat/{{else if eq .StructName "APGroup" }}{{ else }}rest/{{ end }}{{ .ResourcePath }}", site), nil, &respBody)
err := c.Get(ctx, fmt.Sprintf("s/%s/{{ if eq .StructName "Device" }}stat/{{else if eq .StructName "APGroup" }}{{ else }}rest/{{ end }}{{ .ResourcePath }}", site), nil, &respBody)
if err != nil {
return nil, err
}
@@ -82,20 +82,20 @@ func (c *Client) list{{ .StructName }}(ctx context.Context, site string) ([]{{ .
func (c *Client) get{{ .StructName }}(ctx context.Context, site{{ if not .IsSetting }}, id{{ end }} string) (*{{ .StructName }}, error) {
var respBody struct {
Meta meta `json:"meta"`
Meta Meta `json:"meta"`
Data []{{ .StructName }} `json:"data"`
}
{{ if .IsSetting }}
err := c.do(ctx, "GET", fmt.Sprintf("s/%s/get/setting/{{ .ResourcePath }}", site), nil, &respBody)
err := c.Get(ctx, fmt.Sprintf("s/%s/get/setting/{{ .ResourcePath }}", site), nil, &respBody)
{{- else }}
err := c.do(ctx, "GET", fmt.Sprintf("s/%s/{{ if eq .StructName "Device" }}stat{{ else }}rest{{ end }}/{{ .ResourcePath }}/%s", site, id), nil, &respBody)
err := c.Get(ctx, fmt.Sprintf("s/%s/{{ if eq .StructName "Device" }}stat{{ else }}rest{{ end }}/{{ .ResourcePath }}/%s", site, id), nil, &respBody)
{{- end }}
if err != nil {
return nil, err
}
if len(respBody.Data) != 1 {
return nil, &NotFoundError{}
return nil, NotFoundError
}
d := respBody.Data[0]
@@ -104,7 +104,7 @@ func (c *Client) get{{ .StructName }}(ctx context.Context, site{{ if not .IsSett
{{ if not .IsSetting }}
func (c *Client) delete{{ .StructName }}(ctx context.Context, site, id string) error {
err := c.do(ctx, "DELETE", fmt.Sprintf("s/%s/rest/{{ .ResourcePath }}/%s", site, id), struct{}{}, nil)
err := c.Delete(ctx, fmt.Sprintf("s/%s/rest/{{ .ResourcePath }}/%s", site, id), struct{}{}, nil)
if err != nil {
return err
}
@@ -113,17 +113,17 @@ func (c *Client) delete{{ .StructName }}(ctx context.Context, site, id string) e
func (c *Client) create{{ .StructName }}(ctx context.Context, site string, d *{{ .StructName }}) (*{{ .StructName }}, error) {
var respBody struct {
Meta meta `json:"meta"`
Meta Meta `json:"meta"`
Data []{{ .StructName }} `json:"data"`
}
err := c.do(ctx, "POST", fmt.Sprintf("s/%s/rest/{{ .ResourcePath }}", site), d, &respBody)
err := c.Post(ctx, fmt.Sprintf("s/%s/rest/{{ .ResourcePath }}", site), d, &respBody)
if err != nil {
return nil, err
}
if len(respBody.Data) != 1 {
return nil, &NotFoundError{}
return nil, NotFoundError
}
new := respBody.Data[0]
@@ -134,21 +134,21 @@ func (c *Client) create{{ .StructName }}(ctx context.Context, site string, d *{{
func (c *Client) update{{ .StructName }}(ctx context.Context, site string, d *{{ .StructName }}) (*{{ .StructName }}, error) {
var respBody struct {
Meta meta `json:"meta"`
Meta Meta `json:"meta"`
Data []{{ .StructName }} `json:"data"`
}
{{ if .IsSetting }}
d.Key = "{{ .ResourcePath }}"
err := c.do(ctx, "PUT", fmt.Sprintf("s/%s/set/setting/{{ .ResourcePath }}", site), d, &respBody)
err := c.Put(ctx, fmt.Sprintf("s/%s/set/setting/{{ .ResourcePath }}", site), d, &respBody)
{{- else }}
err := c.do(ctx, "PUT", fmt.Sprintf("s/%s/rest/{{ .ResourcePath }}/%s", site, d.ID), d, &respBody)
err := c.Put(ctx, fmt.Sprintf("s/%s/rest/{{ .ResourcePath }}/%s", site, d.ID), d, &respBody)
{{- end }}
if err != nil {
return nil, err
}
if len(respBody.Data) != 1 {
return nil, &NotFoundError{}
return nil, NotFoundError
}
new := respBody.Data[0]