This repository was archived by the owner on Mar 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Add a bunch of new APIs #16
Open
supercharlesliu
wants to merge
10
commits into
nytm:master
Choose a base branch
from
supercharlesliu:api-update
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e88eb55
Add Team CRUD API
supercharlesliu d3e68b4
Add User Get & Update API
supercharlesliu b8113e3
Add tests for User Get & Update APIs
supercharlesliu 573ec74
Add foler permission GET & Update api
supercharlesliu dbed9b6
Add Datasources API
supercharlesliu 9c8633c
Add OrgPreferences Get & Update APIs
supercharlesliu 2a14e14
Add tests for GetOrgPreferences & UpdateOrgPreferences
supercharlesliu 826fe88
Add DashboardsByFolder & DashboardByUid
supercharlesliu b1bdedf
Add GetDashboardVersions API
supercharlesliu 3f9b9f5
Fix permission json Marshal and Unmarshal
supercharlesliu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
package gapi | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
) | ||
|
||
type Team struct { | ||
Id int64 `json:"id,omitempty"` | ||
OrgId int64 `json:"orgId,omitempty"` | ||
Name string `json:"name,omitempty"` | ||
Email string `json:"email,omitempty"` | ||
AvatarUrl string `json:"avatarUrl,omitempty"` | ||
MemberCount int64 `json:"memberCount,omitempty"` | ||
} | ||
|
||
type TeamMember struct { | ||
UserId int64 `json:"userId,omitempty"` | ||
TeamId int64 `json:"teamId,omitempty"` | ||
OrgId int64 `json:"orgId,omitempty"` | ||
Login string `json:"login,omitempty"` | ||
Email string `json:"email,omitempty"` | ||
AvatarUrl string `json:"avatarUrl,omitempty"` | ||
} | ||
|
||
type TeamSearchResponse struct { | ||
TotalCount int64 `json:"totalCount,omitempty"` | ||
Teams []*Team `json:"teams,omitempty"` | ||
Page int64 `json:"page,omitempty"` | ||
PerPage int64 `json:"perPage,omitempty"` | ||
} | ||
|
||
type CreateTeamResponse struct { | ||
Id int64 `json:"teamId"` | ||
} | ||
|
||
func (c *Client) Team(id int64) (Team, error) { | ||
team := Team{} | ||
req, err := c.newRequest("GET", fmt.Sprintf("/api/teams/%d", id), nil, nil) | ||
if err != nil { | ||
return team, err | ||
} | ||
resp, err := c.Do(req) | ||
if err != nil { | ||
return team, err | ||
} | ||
if resp.StatusCode != 200 { | ||
return team, errors.New(resp.Status) | ||
} | ||
data, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return team, err | ||
} | ||
err = json.Unmarshal(data, &team) | ||
return team, err | ||
} | ||
|
||
func (c *Client) Teams() ([]*Team, error) { | ||
var list []*Team | ||
req, err := c.newRequest("GET", "/api/teams/search", nil, nil) | ||
if err != nil { | ||
return list, err | ||
} | ||
resp, err := c.Do(req) | ||
if err != nil { | ||
return list, err | ||
} | ||
if resp.StatusCode != 200 { | ||
return list, errors.New(resp.Status) | ||
} | ||
|
||
data, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return list, err | ||
} | ||
var r TeamSearchResponse | ||
err = json.Unmarshal(data, &r) | ||
return r.Teams, err | ||
} | ||
|
||
func (c *Client) NewTeam(name string) (Team, error) { | ||
team := Team{ | ||
Name: name, | ||
} | ||
data, err := json.Marshal(team) | ||
req, err := c.newRequest("POST", "/api/teams", nil, bytes.NewBuffer(data)) | ||
if err != nil { | ||
return team, err | ||
} | ||
resp, err := c.Do(req) | ||
if err != nil { | ||
return team, err | ||
} | ||
if resp.StatusCode != 200 { | ||
data, _ = ioutil.ReadAll(resp.Body) | ||
return team, fmt.Errorf("status: %s body: %s", resp.Status, data) | ||
} | ||
data, err = ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return team, err | ||
} | ||
var r CreateTeamResponse | ||
err = json.Unmarshal(data, &r) | ||
if err != nil { | ||
return team, err | ||
} | ||
team.Id = r.Id | ||
return team, err | ||
} | ||
|
||
func (c *Client) UpdateTeam(id string, name string) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
dataMap := map[string]string{ | ||
"name": name, | ||
} | ||
data, err := json.Marshal(dataMap) | ||
req, err := c.newRequest("PUT", fmt.Sprintf("/api/teams/%s", id), nil, bytes.NewBuffer(data)) | ||
if err != nil { | ||
return err | ||
} | ||
resp, err := c.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
if resp.StatusCode != 200 { | ||
return errors.New(resp.Status) | ||
} | ||
return err | ||
} | ||
|
||
func (c *Client) DeleteTeam(id string) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
req, err := c.newRequest("DELETE", fmt.Sprintf("/api/teams/%s", id), nil, nil) | ||
if err != nil { | ||
return err | ||
} | ||
resp, err := c.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
if resp.StatusCode != 200 { | ||
return errors.New(resp.Status) | ||
} | ||
return err | ||
} | ||
|
||
func (c *Client) AddTeamMember(id string, userID int64) error { | ||
dataMap := map[string]interface{}{ | ||
"userId": userID, | ||
} | ||
data, err := json.Marshal(dataMap) | ||
if err != nil { | ||
return err | ||
} | ||
req, err := c.newRequest("POST", fmt.Sprintf("/api/teams/%s/members", id), nil, bytes.NewBuffer(data)) | ||
if err != nil { | ||
return err | ||
} | ||
resp, err := c.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if resp.StatusCode != 200 { | ||
return errors.New(resp.Status) | ||
} | ||
return err | ||
} | ||
|
||
func (c *Client) RemoveTeamMember(id string, userID int64) error { | ||
req, err := c.newRequest("DELETE", fmt.Sprintf("/api/teams/%s/members/%d", id, userID), nil, nil) | ||
if err != nil { | ||
return err | ||
} | ||
resp, err := c.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if resp.StatusCode != 200 { | ||
return errors.New(resp.Status) | ||
} | ||
return err | ||
} | ||
|
||
func (c *Client) TeamMembers(id string) ([]*TeamMember, error) { | ||
req, err := c.newRequest("GET", fmt.Sprintf("/api/teams/%s/members", id), nil, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resp, err := c.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if resp.StatusCode != 200 { | ||
return nil, errors.New(resp.Status) | ||
} | ||
|
||
data, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var list []*TeamMember | ||
err = json.Unmarshal(data, &list) | ||
return list, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package gapi | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gobs/pretty" | ||
) | ||
|
||
const ( | ||
creatTeamJSON = `{"message":"Team created","teamId":2}` | ||
getTeamJSON = `{ | ||
"id": 1, | ||
"orgId": 1, | ||
"name": "MyTestTeam", | ||
"email": "", | ||
"created": "2017-12-15T10:40:45+01:00", | ||
"updated": "2017-12-15T10:40:45+01:00" | ||
}` | ||
updateTeamJSON = `{"message":"Team updated"}` | ||
deleteTeamJSON = `{"message":"Team deleted"}` | ||
addTeamMemberJSON = `{"message":"Member added to Team"}` | ||
) | ||
|
||
func TestNewTeam(t *testing.T) { | ||
server, client := gapiTestTools(200, creatTeamJSON) | ||
defer server.Close() | ||
|
||
team, err := client.NewTeam("test") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
t.Log(pretty.PrettyFormat(team)) | ||
|
||
if team.Id != 2 { | ||
t.Error("Create Team Error.") | ||
} | ||
} | ||
|
||
func TestGetTeam(t *testing.T) { | ||
server, client := gapiTestTools(200, getTeamJSON) | ||
defer server.Close() | ||
|
||
resp, err := client.Team(1) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
t.Log(pretty.PrettyFormat(resp)) | ||
|
||
team := Team{ | ||
Id: 1, | ||
OrgId: 1, | ||
Email: "", | ||
Name: "MyTestTeam", | ||
} | ||
if resp != team { | ||
t.Error("Not correctly parsing returned team.") | ||
} | ||
} | ||
|
||
func TestUpdateTeam(t *testing.T) { | ||
server, client := gapiTestTools(200, updateTeamJSON) | ||
defer server.Close() | ||
|
||
err := client.UpdateTeam("1", "test team 2") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
func TestDeleteTeam(t *testing.T) { | ||
server, client := gapiTestTools(200, deleteTeamJSON) | ||
defer server.Close() | ||
|
||
err := client.DeleteTeam("1") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
func TestAddTeamMember(t *testing.T) { | ||
server, client := gapiTestTools(200, addTeamMemberJSON) | ||
defer server.Close() | ||
|
||
err := client.AddTeamMember("1", 1) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this not be?