Skip to content

Commit

Permalink
Add query to doRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
ohkinozomu committed Aug 18, 2021
1 parent f913ccc commit 412a65d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 35 deletions.
19 changes: 10 additions & 9 deletions redash/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (c *Client) IsStrict() bool {
return c.Config.StrictMode
}

func (c *Client) doRequest(method, path, body string) (*http.Response, error) {
func (c *Client) doRequest(method, path, body string, query url.Values) (*http.Response, error) {
requestURI := strings.TrimSuffix(c.Config.RedashURI, "/") + path

log.Debug(fmt.Sprintf("[DEBUG] %s request to %s", method, path))
Expand All @@ -74,6 +74,7 @@ func (c *Client) doRequest(method, path, body string) (*http.Response, error) {

request.Header.Add("Content-Type", "application/json")
request.Header.Set("Authorization", "Key "+c.Config.APIKey)
request.URL.RawQuery = query.Encode()

return http.DefaultClient.Do(request)
}()
Expand All @@ -88,18 +89,18 @@ func (c *Client) doRequest(method, path, body string) (*http.Response, error) {
return response, nil
}

func (c *Client) get(path string) (*http.Response, error) {
return c.doRequest(http.MethodGet, path, "")
func (c *Client) get(path string, query url.Values) (*http.Response, error) {
return c.doRequest(http.MethodGet, path, "", query)
}

func (c *Client) post(path string, payload string) (*http.Response, error) {
return c.doRequest(http.MethodPost, path, payload)
func (c *Client) post(path string, payload string, query url.Values) (*http.Response, error) {
return c.doRequest(http.MethodPost, path, payload, query)
}

func (c *Client) put(path string, payload string) (*http.Response, error) {
return c.doRequest(http.MethodPut, path, payload)
func (c *Client) put(path string, payload string, query url.Values) (*http.Response, error) {
return c.doRequest(http.MethodPut, path, payload, query)
}

func (c *Client) delete(path string) (*http.Response, error) {
return c.doRequest(http.MethodDelete, path, "")
func (c *Client) delete(path string, query url.Values) (*http.Response, error) {
return c.doRequest(http.MethodDelete, path, "", query)
}
22 changes: 13 additions & 9 deletions redash/data_sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"strconv"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -59,8 +60,8 @@ type DataSourceTypePropertyField struct {
//GetDataSources gets an array of all DataSources available
func (c *Client) GetDataSources() (*[]DataSource, error) {
path := "/api/data_sources"

response, err := c.get(path)
query := url.Values{}
response, err := c.get(path, query)

if err != nil {
return nil, err
Expand All @@ -80,8 +81,8 @@ func (c *Client) GetDataSources() (*[]DataSource, error) {
//GetDataSource gets a specific DataSource
func (c *Client) GetDataSource(id int) (*DataSource, error) {
path := "/api/data_sources/" + strconv.Itoa(id)

response, err := c.get(path)
query := url.Values{}
response, err := c.get(path, query)
if err != nil {
return nil, err
}
Expand All @@ -105,8 +106,8 @@ func (c *Client) GetDataSource(id int) (*DataSource, error) {
//GetDataSourceTypes gets all available types with configuration details
func (c *Client) GetDataSourceTypes() ([]DataSourceType, error) {
path := "/api/data_sources/types"

response, err := c.get(path)
query := url.Values{}
response, err := c.get(path, query)

if err != nil {
return nil, err
Expand Down Expand Up @@ -193,7 +194,8 @@ func (c *Client) CreateDataSource(dataSourcePayload *DataSource) (*DataSource, e
return nil, err
}

response, err := c.post(path, string(payload))
query := url.Values{}
response, err := c.post(path, string(payload), query)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -228,7 +230,8 @@ func (c *Client) UpdateDataSource(id int, dataSourcePayload *DataSource) (*DataS
return nil, err
}

response, err := c.post(path, string(payload))
query := url.Values{}
response, err := c.post(path, string(payload), query)
if err != nil {
return nil, err
}
Expand All @@ -253,7 +256,8 @@ func (c *Client) UpdateDataSource(id int, dataSourcePayload *DataSource) (*DataS
func (c *Client) DeleteDataSource(id int) error {
path := "/api/data_sources/" + strconv.Itoa(id)

_, err := c.delete(path)
query := url.Values{}
_, err := c.delete(path, query)
if err != nil {
return err
}
Expand Down
28 changes: 19 additions & 9 deletions redash/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package redash
import (
"encoding/json"
"io/ioutil"
"net/url"
"strconv"
"time"
)
Expand Down Expand Up @@ -48,7 +49,8 @@ type GroupCreatePayload struct {
func (c *Client) GetGroups() (*[]Group, error) {
path := "/api/groups"

response, err := c.get(path)
query := url.Values{}
response, err := c.get(path, query)

if err != nil {
return nil, err
Expand All @@ -69,7 +71,8 @@ func (c *Client) GetGroups() (*[]Group, error) {
func (c *Client) GetGroup(id int) (*Group, error) {
path := "/api/groups/" + strconv.Itoa(id)

response, err := c.get(path)
query := url.Values{}
response, err := c.get(path, query)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -99,7 +102,8 @@ func (c *Client) CreateGroup(groupPayload *GroupCreatePayload) (*Group, error) {
return nil, err
}

response, err := c.post(path, string(payload))
query := url.Values{}
response, err := c.post(path, string(payload), query)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -129,7 +133,8 @@ func (c *Client) UpdateGroup(id int, group *Group) (*Group, error) {
return nil, err
}

response, err := c.post(path, string(payload))
query := url.Values{}
response, err := c.post(path, string(payload), query)
if err != nil {
return nil, err
}
Expand All @@ -152,7 +157,8 @@ func (c *Client) UpdateGroup(id int, group *Group) (*Group, error) {
func (c *Client) DeleteGroup(id int) error {
path := "/api/groups/" + strconv.Itoa(id)

_, err := c.delete(path)
query := url.Values{}
_, err := c.delete(path, query)
if err != nil {
return err
}
Expand All @@ -170,7 +176,8 @@ func (c *Client) GroupAddUser(groupID int, userID int) error {
return err
}

response, err := c.post(path, string(payload))
query := url.Values{}
response, err := c.post(path, string(payload), query)
if err != nil {
return err
}
Expand All @@ -183,7 +190,8 @@ func (c *Client) GroupAddUser(groupID int, userID int) error {
func (c *Client) GroupRemoveUser(groupID int, userID int) error {
path := "/api/groups/" + strconv.Itoa(groupID) + "/members/" + strconv.Itoa(userID)

response, err := c.delete(path)
query := url.Values{}
response, err := c.delete(path, query)
if err != nil {
return err
}
Expand All @@ -202,7 +210,8 @@ func (c *Client) GroupAddDataSource(groupID int, dataSourceID int) error {
return err
}

response, err := c.post(path, string(payload))
query := url.Values{}
response, err := c.post(path, string(payload), query)
if err != nil {
return err
}
Expand All @@ -215,7 +224,8 @@ func (c *Client) GroupAddDataSource(groupID int, dataSourceID int) error {
func (c *Client) GroupRemoveDataSource(groupID int, dataSourceID int) error {
path := "/api/groups/" + strconv.Itoa(groupID) + "/data_sources/" + strconv.Itoa(dataSourceID)

response, err := c.delete(path)
query := url.Values{}
response, err := c.delete(path, query)
if err != nil {
return err
}
Expand Down
22 changes: 15 additions & 7 deletions redash/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"strconv"
"time"
)
Expand Down Expand Up @@ -80,7 +81,8 @@ type UserUpdatePayload struct {
func (c *Client) GetUsers() (*UserList, error) {
path := "/api/users"

response, err := c.get(path)
query := url.Values{}
response, err := c.get(path, query)

if err != nil {
return nil, err
Expand All @@ -102,7 +104,8 @@ func (c *Client) GetUsers() (*UserList, error) {
func (c *Client) GetUser(id int) (*User, error) {
path := "/api/users/" + strconv.Itoa(id)

response, err := c.get(path)
query := url.Values{}
response, err := c.get(path, query)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -132,7 +135,8 @@ func (c *Client) CreateUser(userCreatePayload *UserCreatePayload) (*User, error)
return nil, err
}

response, err := c.post(path, string(payload))
query := url.Values{}
response, err := c.post(path, string(payload), query)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -162,7 +166,8 @@ func (c *Client) UpdateUser(id int, userUpdatePayload *UserUpdatePayload) (*User
return nil, err
}

response, err := c.post(path, string(payload))
query := url.Values{}
response, err := c.post(path, string(payload), query)
if err != nil {
return nil, err
}
Expand All @@ -187,7 +192,8 @@ func (c *Client) UpdateUser(id int, userUpdatePayload *UserUpdatePayload) (*User
func (c *Client) DisableUser(id int) error {
path := "/api/users/" + strconv.Itoa(id) + "/disable"

response, err := c.post(path, "")
query := url.Values{}
response, err := c.post(path, "", query)
if err != nil {
return err
}
Expand All @@ -203,9 +209,11 @@ func (c *Client) DisableUser(id int) error {

//SearchUsers finds a list of users matching a string (searches `name` and `email` fields)
func (c *Client) SearchUsers(term string) (*UserList, error) {
path := "/api/users?q=" + term
path := "/api/users"

response, err := c.get(path)
query := url.Values{}
query.Add("q", term)
response, err := c.get(path, query)

if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion redash/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TestGetUserByEmail(t *testing.T) {

c, _ := NewClient(&Config{RedashURI: "https://com.acme/", APIKey: "ApIkEyApIkEyApIkEyApIkEyApIkEy"})

httpmock.RegisterResponder("GET", "https://com.acme/api/users?q=tëst@email.com",
httpmock.RegisterResponder("GET", "https://com.acme/api/users?q=t%C3%ABst%40email.com",
httpmock.NewStringResponder(200, `{"count": 1, "page": 1, "page_size": 25, "results": [ {"id": 1, "name": "Existing User", "email": "të[email protected]"} ]}`))

httpmock.RegisterResponder("GET", "https://com.acme/api/users/1",
Expand Down

0 comments on commit 412a65d

Please sign in to comment.