@@ -4,12 +4,9 @@ Utility types definitions
44package utils
55
66import (
7- "bytes"
8- "context"
97 "encoding/json"
108 "errors"
119 "fmt"
12- "net/http"
1310 "os"
1411 "path/filepath"
1512 "strings"
@@ -237,58 +234,3 @@ func checkVisibility(visibility string) bool {
237234 }
238235 return valid
239236}
240-
241- // GraphQLClient is a client for sending GraphQL requests to GitLab
242- type GraphQLClient struct {
243- token string
244- URL string
245- }
246-
247- // GraphQLRequest is a struct that represents a GraphQL request
248- // It contains the query and the variables
249- type GraphQLRequest struct {
250- Query string `json:"query"`
251- Variables string `json:"variables,omitempty"`
252- }
253-
254- // NewGitlabGraphQLClient creates a new GraphQL client for GitLab
255- // It takes the token and the GitLab URL as arguments
256- // It returns a pointer to the GraphQLClient struct
257- func NewGitlabGraphQLClient (token , gitlabUrl string ) * GraphQLClient {
258- return & GraphQLClient {
259- token : token ,
260- URL : strings .TrimSuffix (gitlabUrl , "/" ) + "/api/graphql" ,
261- }
262- }
263-
264- // SendRequest sends a GraphQL request to GitLab
265- // It takes a GraphQLRequest struct and the HTTP method as arguments
266- // It returns the response body as a string and an error if any
267- func (g * GraphQLClient ) SendRequest (request * GraphQLRequest , method string ) (string , error ) {
268- requestBody , err := json .Marshal (request )
269- if err != nil {
270- return "" , err
271- }
272- req , err := http .NewRequestWithContext (context .Background (), method , g .URL , bytes .NewBuffer (requestBody ))
273- if err != nil {
274- return "" , err
275- }
276-
277- req .Header .Set ("Content-Type" , "application/json" )
278- req .Header .Set ("Authorization" , "Bearer " + g .token )
279-
280- client := & http.Client {}
281- resp , err := client .Do (req )
282- if err != nil {
283- return "" , err
284- }
285- defer resp .Body .Close ()
286- if resp .StatusCode != http .StatusOK {
287- return "" , fmt .Errorf ("GraphQL request failed with status: %s" , resp .Status )
288- }
289- var responseBody bytes.Buffer
290- if _ , err := responseBody .ReadFrom (resp .Body ); err != nil {
291- return "" , err
292- }
293- return responseBody .String (), nil
294- }
0 commit comments