1
1
package api
2
2
3
3
import (
4
+ "encoding/json"
4
5
"errors"
5
6
"fmt"
6
7
"log/slog"
@@ -35,6 +36,20 @@ query createTeam($teamId: ID!) {
35
36
query deleteTeam($teamId: ID!, $force: Boolean) {
36
37
deleteTeam(teamId: $teamId, force: $force)
37
38
}
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
+ }
38
53
`
39
54
)
40
55
@@ -43,7 +58,7 @@ type Client struct {
43
58
Endpoint string
44
59
}
45
60
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 ) {
47
62
request := graphql.Request {Query : query , Variables : variables }
48
63
slog .Info ("--> GraphQL call [" + operationName + "]" )
49
64
start := time .Now ()
@@ -53,32 +68,77 @@ func (client Client) sendRequest(operationName, query string, variables map[stri
53
68
}()
54
69
response , err := client .GraphQLClient .Execute (client .Endpoint , request )
55
70
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 )
57
72
}
58
73
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 ))
60
75
}
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
62
82
}
63
83
64
84
func (client Client ) Auth (token string ) error {
65
85
variables := map [string ]any {
66
86
"token" : token ,
67
87
}
68
- return client .sendRequest ("auth" , authQuery , variables )
88
+ return client .sendRequestDiscardingData ("auth" , authQuery , variables )
69
89
}
70
90
71
91
func (client Client ) CreateTeam (teamId string ) error {
72
92
variables := map [string ]any {
73
93
"teamId" : teamId ,
74
94
}
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 )
76
99
}
77
100
78
101
func (client Client ) DeleteTeam (teamId string , force bool ) error {
79
102
variables := map [string ]any {
80
103
"teamId" : teamId ,
81
104
"force" : force ,
82
105
}
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
84
144
}
0 commit comments