Skip to content

Commit

Permalink
feat: Add query parameters support for GET method
Browse files Browse the repository at this point in the history
  • Loading branch information
alperencelik committed Nov 4, 2024
1 parent 5617b30 commit f4f2c05
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions proxmox.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -146,6 +147,42 @@ func (c *Client) Get(ctx context.Context, p string, v interface{}) error {
return c.Req(ctx, http.MethodGet, p, nil, v)
}

// GetWithParams is a helper function to append query parameters to the URL
func (c *Client) GetWithParams(ctx context.Context, p string, d interface{}, v interface{}) error {
// Parse data and append to URL
if d != nil {
queryString, err := dataParserForUrl(d)
if err != nil {
return err
}
p = p + "?" + queryString
}
return c.Req(ctx, http.MethodGet, p, nil, v)
}

// dataParserForUrl parses the data and appends it to the URL as a query string
func dataParserForUrl(d interface{}) (string, error) {

Check failure on line 164 in proxmox.go

View workflow job for this annotation

GitHub Actions / ci

var-naming: func dataParserForUrl should be dataParserForURL (revive)
jsonBytes, err := json.Marshal(d)
if err != nil {
return "", err
}

var m map[string]interface{}
err = json.Unmarshal(jsonBytes, &m)
if err != nil {
return "", err
}

values := url.Values{}
for key, value := range m {
strValue := fmt.Sprintf("%v", value)
values.Set(key, strValue)
}

return values.Encode(), nil

}

func (c *Client) Post(ctx context.Context, p string, d interface{}, v interface{}) error {
var data []byte
if d != nil {
Expand Down

0 comments on commit f4f2c05

Please sign in to comment.