From 412a65de9b56f6253e70905e1e9c2c82a5dbea71 Mon Sep 17 00:00:00 2001 From: ohkinozomu Date: Mon, 16 Aug 2021 00:10:39 +0900 Subject: [PATCH] Add query to doRequest --- redash/client.go | 19 ++++++++++--------- redash/data_sources.go | 22 +++++++++++++--------- redash/groups.go | 28 +++++++++++++++++++--------- redash/users.go | 22 +++++++++++++++------- redash/users_test.go | 2 +- 5 files changed, 58 insertions(+), 35 deletions(-) diff --git a/redash/client.go b/redash/client.go index 292210c..9daa5d6 100644 --- a/redash/client.go +++ b/redash/client.go @@ -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)) @@ -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) }() @@ -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) } diff --git a/redash/data_sources.go b/redash/data_sources.go index 1c4796f..ca82646 100644 --- a/redash/data_sources.go +++ b/redash/data_sources.go @@ -17,6 +17,7 @@ import ( "encoding/json" "fmt" "io/ioutil" + "net/url" "strconv" log "github.com/sirupsen/logrus" @@ -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 @@ -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 } @@ -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 @@ -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 } @@ -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 } @@ -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 } diff --git a/redash/groups.go b/redash/groups.go index 6fda73b..5e28043 100644 --- a/redash/groups.go +++ b/redash/groups.go @@ -16,6 +16,7 @@ package redash import ( "encoding/json" "io/ioutil" + "net/url" "strconv" "time" ) @@ -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 @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } diff --git a/redash/users.go b/redash/users.go index 8482671..73bac49 100644 --- a/redash/users.go +++ b/redash/users.go @@ -17,6 +17,7 @@ import ( "encoding/json" "fmt" "io/ioutil" + "net/url" "strconv" "time" ) @@ -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 @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 diff --git a/redash/users_test.go b/redash/users_test.go index e6edcab..3de7e59 100644 --- a/redash/users_test.go +++ b/redash/users_test.go @@ -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ëst@email.com"} ]}`)) httpmock.RegisterResponder("GET", "https://com.acme/api/users/1",