Skip to content

Commit 8bde80a

Browse files
committed
#2 add project management API
1 parent 7133c67 commit 8bde80a

File tree

3 files changed

+110
-17
lines changed

3 files changed

+110
-17
lines changed

go/api/client.go

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package api
22

33
import (
4+
"encoding/json"
45
"errors"
56
"fmt"
67
"log/slog"
@@ -35,6 +36,20 @@ query createTeam($teamId: ID!) {
3536
query deleteTeam($teamId: ID!, $force: Boolean) {
3637
deleteTeam(teamId: $teamId, force: $force)
3738
}
39+
`
40+
41+
createProjectMutation = `
42+
mutation RmCreateProject($projectName: String!) {
43+
rmCreateProject(projectName: $projectName) {
44+
id
45+
}
46+
}
47+
`
48+
49+
deleteProjectMutation = `
50+
mutation RmDeleteProject($projectId: ID!) {
51+
rmDeleteProject(projectId: $projectId)
52+
}
3853
`
3954
)
4055

@@ -43,7 +58,7 @@ type Client struct {
4358
Endpoint string
4459
}
4560

46-
func (client Client) sendRequest(operationName, query string, variables map[string]any) error {
61+
func (client Client) sendRequest(operationName, query string, variables graphql.Object) (json.RawMessage, error) {
4762
request := graphql.Request{Query: query, Variables: variables}
4863
slog.Info("--> GraphQL call [" + operationName + "]")
4964
start := time.Now()
@@ -53,32 +68,77 @@ func (client Client) sendRequest(operationName, query string, variables map[stri
5368
}()
5469
response, err := client.GraphQLClient.Execute(client.Endpoint, request)
5570
if err != nil {
56-
return lib.WrapError("error while sending an api request", err)
71+
return json.RawMessage{}, lib.WrapError("error while sending an api request", err)
5772
}
5873
if response.Errors != nil {
59-
return errors.New("error recieved when executing an API call: \n" + string(response.Errors))
74+
err = errors.New("error recieved when executing an API call: \n" + string(response.Errors))
6075
}
61-
return nil
76+
return response.Data, err
77+
}
78+
79+
func (client Client) sendRequestDiscardingData(operationName, query string, variables graphql.Object) error {
80+
_, err := client.sendRequest(operationName, query, variables)
81+
return err
6282
}
6383

6484
func (client Client) Auth(token string) error {
6585
variables := map[string]any{
6686
"token": token,
6787
}
68-
return client.sendRequest("auth", authQuery, variables)
88+
return client.sendRequestDiscardingData("auth", authQuery, variables)
6989
}
7090

7191
func (client Client) CreateTeam(teamId string) error {
7292
variables := map[string]any{
7393
"teamId": teamId,
7494
}
75-
return client.sendRequest(fmt.Sprintf("create team '%s'", teamId), createTeamQuery, variables)
95+
return client.sendRequestDiscardingData(
96+
fmt.Sprintf("create team '%s'", teamId),
97+
createTeamQuery,
98+
variables)
7699
}
77100

78101
func (client Client) DeleteTeam(teamId string, force bool) error {
79102
variables := map[string]any{
80103
"teamId": teamId,
81104
"force": force,
82105
}
83-
return client.sendRequest(fmt.Sprintf("delete team '%s'", teamId), deleteTeamQuery, variables)
106+
return client.sendRequestDiscardingData(
107+
fmt.Sprintf("delete team '%s'", teamId),
108+
deleteTeamQuery,
109+
variables)
110+
}
111+
112+
func (client Client) CreateProject(projectName string) (id string, err error) {
113+
variables := map[string]any{
114+
"projectName": projectName,
115+
}
116+
rawData, err := client.sendRequest(
117+
fmt.Sprintf("create project with name '%s'", projectName),
118+
createProjectMutation,
119+
variables)
120+
if err != nil {
121+
return id, err
122+
}
123+
var rmCreateProjectResponse RmCreateProjectResponse
124+
err = json.Unmarshal(rawData, &rmCreateProjectResponse)
125+
if err != nil {
126+
return id, lib.WrapError("unable to unmarshal rmCreateProjectResponse", err)
127+
}
128+
return rmCreateProjectResponse.RmCreateProject.Id, err
129+
}
130+
131+
func (client Client) DeleteProject(projectId string) error {
132+
variables := map[string]any{
133+
"projectId": projectId,
134+
}
135+
rawData, err := client.sendRequest(
136+
fmt.Sprintf("delete project with id '%s'", projectId),
137+
deleteProjectMutation,
138+
variables)
139+
if err != nil {
140+
return err
141+
}
142+
slog.Debug(string(rawData))
143+
return nil
84144
}

go/api/types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package api
2+
3+
type RmCreateProjectResponse struct {
4+
RmCreateProject RmCreateProject
5+
}
6+
7+
type RmCreateProject struct {
8+
Id string
9+
}

go/main.go

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ import (
1212
"github.com/dbeaver/cloudbeaver-graphql-examples/go/lib"
1313
)
1414

15+
const (
16+
objectPrefix = "cloudbeaver-graqhql-examples-go-"
17+
teamId = objectPrefix + "team"
18+
projectName = objectPrefix + "project"
19+
)
20+
1521
func main() {
1622
if err := main0(); err != nil {
1723
slog.Error(err.Error())
@@ -25,12 +31,7 @@ func main0() error {
2531
if err != nil {
2632
return lib.WrapError("error while reading variables", err)
2733
}
28-
cookieJar, err := cookiejar.New(nil)
29-
if err != nil {
30-
return lib.WrapError("unable to create a cookie jar", err)
31-
}
32-
graphQLClient := graphql.Client{HttpClient: &http.Client{Jar: cookieJar}}
33-
apiClient := api.Client{GraphQLClient: graphQLClient, Endpoint: env.GraphqlEndpoint()}
34+
apiClient := initClient(env.GraphqlEndpoint())
3435

3536
// Auth
3637
err = apiClient.Auth(env.Token)
@@ -39,18 +40,41 @@ func main0() error {
3940
return err
4041
}
4142

42-
// Create a team
43-
teamId := "exampleTeamId"
43+
// Creation / deletion of a team
4444
err = apiClient.CreateTeam(teamId)
4545
if err != nil {
4646
return err
4747
}
48+
defer cleanup("delete team "+teamId, func() error {
49+
return apiClient.DeleteTeam(teamId, true)
50+
})
4851

49-
// Delete a team
50-
err = apiClient.DeleteTeam(teamId, true)
52+
// Creation of a project
53+
projectId, err := apiClient.CreateProject(projectName)
5154
if err != nil {
5255
return err
5356
}
57+
defer cleanup("delete project "+projectId, func() error {
58+
return apiClient.DeleteProject(projectId)
59+
})
5460

5561
return nil
5662
}
63+
64+
func initClient(endpoint string) api.Client {
65+
cookieJar, err := cookiejar.New(nil)
66+
if err != nil {
67+
// Invariant: the method that creates cookie jar with no options never returns non-nil err
68+
panic("encountered error while creating a cookie jar! " + err.Error())
69+
}
70+
graphQLClient := graphql.Client{HttpClient: &http.Client{Jar: cookieJar}}
71+
return api.Client{GraphQLClient: graphQLClient, Endpoint: endpoint}
72+
}
73+
74+
func cleanup(callDescription string, apiCall func() error) {
75+
err := apiCall()
76+
if err != nil {
77+
slog.Warn("unable to " + callDescription)
78+
slog.Warn(err.Error())
79+
}
80+
}

0 commit comments

Comments
 (0)