Skip to content

Commit

Permalink
chore: simplify client and remove setting headers
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasZeissner committed Sep 13, 2024
1 parent 11cd222 commit 01e059a
Show file tree
Hide file tree
Showing 16 changed files with 175 additions and 161 deletions.
25 changes: 21 additions & 4 deletions internal/client/grpc.go → internal/api/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package api

import (
"fmt"
Expand All @@ -25,15 +25,32 @@ func NewClient(apiKey, apiUrl string) (*PlatformClient, error) {
cosmoApiUrl = "https://cosmo-cp.wundergraph.com"
}

httpClient := http.Client{}
httpClient.Transport = &http.Transport{
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
}

client := platformv1connect.NewPlatformServiceClient(&httpClient, cosmoApiUrl)
httpClient := &http.Client{
Transport: &transportWithAuth{
Transport: transport,
ApiKey: cosmoApiKey,
},
}

client := platformv1connect.NewPlatformServiceClient(httpClient, cosmoApiUrl)

return &PlatformClient{
Client: client,
CosmoApiKey: cosmoApiKey,
}, nil
}

type transportWithAuth struct {
Transport http.RoundTripper
ApiKey string
}

func (t *transportWithAuth) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("Authorization", "Bearer "+t.ApiKey)
return t.Transport.RoundTrip(req)
}

21 changes: 8 additions & 13 deletions internal/api/federated_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
"connectrpc.com/connect"
"github.com/wundergraph/cosmo/connect-go/wg/cosmo/common"
platformv1 "github.com/wundergraph/cosmo/connect-go/wg/cosmo/platform/v1"
"github.com/wundergraph/cosmo/connect-go/wg/cosmo/platform/v1/platformv1connect"
)

func CreateFederatedGraph(ctx context.Context, client platformv1connect.PlatformServiceClient, apiKey string, admissionWebhookSecret *string, graph *platformv1.FederatedGraph) (*platformv1.CreateFederatedGraphResponse, error) {
func (p *PlatformClient) CreateFederatedGraph(ctx context.Context, admissionWebhookSecret *string, graph *platformv1.FederatedGraph) (*platformv1.CreateFederatedGraphResponse, error) {
var admissionWebhookURL string
if graph.AdmissionWebhookUrl != nil {
admissionWebhookURL = *graph.AdmissionWebhookUrl
Expand All @@ -28,8 +27,7 @@ func CreateFederatedGraph(ctx context.Context, client platformv1connect.Platform
LabelMatchers: graph.LabelMatchers,
})

request.Header().Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
response, err := client.CreateFederatedGraph(ctx, request)
response, err := p.Client.CreateFederatedGraph(ctx, request)
if err != nil {
return nil, err
}
Expand All @@ -41,7 +39,7 @@ func CreateFederatedGraph(ctx context.Context, client platformv1connect.Platform
return response.Msg, nil
}

func UpdateFederatedGraph(ctx context.Context, client platformv1connect.PlatformServiceClient, apiKey string, admissionWebhookSecret *string, graph *platformv1.FederatedGraph) (*platformv1.UpdateFederatedGraphResponse, error) {
func (p *PlatformClient) UpdateFederatedGraph(ctx context.Context, admissionWebhookSecret *string, graph *platformv1.FederatedGraph) (*platformv1.UpdateFederatedGraphResponse, error) {
var admissionWebhookURL *string
if graph.AdmissionWebhookUrl != nil {
admissionWebhookURL = graph.AdmissionWebhookUrl
Expand All @@ -56,8 +54,7 @@ func UpdateFederatedGraph(ctx context.Context, client platformv1connect.Platform
LabelMatchers: graph.LabelMatchers,
})

request.Header().Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
response, err := client.UpdateFederatedGraph(ctx, request)
response, err := p.Client.UpdateFederatedGraph(ctx, request)
if err != nil {
return nil, err
}
Expand All @@ -69,14 +66,13 @@ func UpdateFederatedGraph(ctx context.Context, client platformv1connect.Platform
return response.Msg, nil
}

func DeleteFederatedGraph(ctx context.Context, client platformv1connect.PlatformServiceClient, apiKey, name, namespace string) error {
func (p *PlatformClient) DeleteFederatedGraph(ctx context.Context, name, namespace string) error {
request := connect.NewRequest(&platformv1.DeleteFederatedGraphRequest{
Name: name,
Namespace: namespace,
})

request.Header().Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
response, err := client.DeleteFederatedGraph(ctx, request)
response, err := p.Client.DeleteFederatedGraph(ctx, request)
if err != nil {
return err
}
Expand All @@ -88,14 +84,13 @@ func DeleteFederatedGraph(ctx context.Context, client platformv1connect.Platform
return nil
}

func GetFederatedGraph(ctx context.Context, client platformv1connect.PlatformServiceClient, apiKey, name, namespace string) (*platformv1.GetFederatedGraphByNameResponse, error) {
func (p *PlatformClient) GetFederatedGraph(ctx context.Context, name, namespace string) (*platformv1.GetFederatedGraphByNameResponse, error) {
request := connect.NewRequest(&platformv1.GetFederatedGraphByNameRequest{
Name: name,
Namespace: namespace,
})

request.Header().Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
response, err := client.GetFederatedGraphByName(ctx, request)
response, err := p.Client.GetFederatedGraphByName(ctx, request)
if err != nil {
return nil, err
}
Expand Down
51 changes: 35 additions & 16 deletions internal/api/monograph.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
"connectrpc.com/connect"
"github.com/wundergraph/cosmo/connect-go/wg/cosmo/common"
platformv1 "github.com/wundergraph/cosmo/connect-go/wg/cosmo/platform/v1"
"github.com/wundergraph/cosmo/connect-go/wg/cosmo/platform/v1/platformv1connect"
)

func CreateMonograph(ctx context.Context, client platformv1connect.PlatformServiceClient, apiKey string, name string, namespace string, routingUrl string, graphUrl string, subscriptionUrl *string, readme *string, websocketSubprotocol string, subscriptionProtocol string, admissionWebhookUrl string, admissionWebhookSecret string) error {
func (p PlatformClient) CreateMonograph(ctx context.Context, name string, namespace string, routingUrl string, graphUrl string, subscriptionUrl *string, readme *string, websocketSubprotocol string, subscriptionProtocol string, admissionWebhookUrl string, admissionWebhookSecret string) error {
request := connect.NewRequest(&platformv1.CreateMonographRequest{
Name: name,
Namespace: namespace,
Expand All @@ -23,12 +22,19 @@ func CreateMonograph(ctx context.Context, client platformv1connect.PlatformServi
AdmissionWebhookURL: admissionWebhookUrl,
AdmissionWebhookSecret: &admissionWebhookSecret,
})
request.Header().Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
_, err := client.CreateMonograph(ctx, request)
return err
response, err := p.Client.CreateMonograph(ctx, request)
if err != nil {
return err
}

if response.Msg.GetResponse().Code != common.EnumStatusCode_OK {
return fmt.Errorf("failed to create monograph: %s", response.Msg)
}

return nil
}

func UpdateMonograph(ctx context.Context, client platformv1connect.PlatformServiceClient, apiKey string, name string, namespace string, routingUrl string, graphUrl string, subscriptionUrl *string, readme *string, websocketSubprotocol string, subscriptionProtocol string, admissionWebhookUrl string, admissionWebhookSecret string) error {
func (p PlatformClient) UpdateMonograph(ctx context.Context, name string, namespace string, routingUrl string, graphUrl string, subscriptionUrl *string, readme *string, websocketSubprotocol string, subscriptionProtocol string, admissionWebhookUrl string, admissionWebhookSecret string) error {
request := connect.NewRequest(&platformv1.UpdateMonographRequest{
Name: name,
Namespace: namespace,
Expand All @@ -41,28 +47,41 @@ func UpdateMonograph(ctx context.Context, client platformv1connect.PlatformServi
AdmissionWebhookURL: &admissionWebhookUrl,
AdmissionWebhookSecret: &admissionWebhookSecret,
})
request.Header().Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
_, err := client.UpdateMonograph(ctx, request)
return err
response, err := p.Client.UpdateMonograph(ctx, request)
if err != nil {
return err
}

if response.Msg.GetResponse().Code != common.EnumStatusCode_OK {
return fmt.Errorf("failed to update monograph: %s", response.Msg)
}

return nil
}

func DeleteMonograph(ctx context.Context, client platformv1connect.PlatformServiceClient, apiKey string, name string, namespace string) error {
func (p PlatformClient) DeleteMonograph(ctx context.Context, name string, namespace string) error {
request := connect.NewRequest(&platformv1.DeleteMonographRequest{
Name: name,
Namespace: namespace,
})
request.Header().Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
_, err := client.DeleteMonograph(ctx, request)
return err
response, err := p.Client.DeleteMonograph(ctx, request)
if err != nil {
return err
}

if response.Msg.GetResponse().Code != common.EnumStatusCode_OK {
return fmt.Errorf("failed to delete monograph: %s", response.Msg)
}

return nil
}

func GetMonograph(ctx context.Context, client platformv1connect.PlatformServiceClient, apiKey string, name string, namespace string) (*platformv1.FederatedGraph, error) {
func (p PlatformClient) GetMonograph(ctx context.Context, name string, namespace string) (*platformv1.FederatedGraph, error) {
request := connect.NewRequest(&platformv1.GetFederatedGraphByNameRequest{
Name: name,
Namespace: namespace,
})
request.Header().Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
response, err := client.GetFederatedGraphByName(ctx, request)
response, err := p.Client.GetFederatedGraphByName(ctx, request)
if err != nil {
return nil, err
}
Expand Down
21 changes: 8 additions & 13 deletions internal/api/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ import (

"github.com/wundergraph/cosmo/connect-go/wg/cosmo/common"
platformv1 "github.com/wundergraph/cosmo/connect-go/wg/cosmo/platform/v1"
"github.com/wundergraph/cosmo/connect-go/wg/cosmo/platform/v1/platformv1connect"
)

func CreateNamespace(ctx context.Context, client platformv1connect.PlatformServiceClient, apiKey, name string) error {
func (p PlatformClient) CreateNamespace(ctx context.Context, name string) error {
request := connect.NewRequest(&platformv1.CreateNamespaceRequest{Name: name})
request.Header().Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
response, err := client.CreateNamespace(ctx, request)
response, err := p.Client.CreateNamespace(ctx, request)
if err != nil {
return err
}
Expand All @@ -26,13 +24,12 @@ func CreateNamespace(ctx context.Context, client platformv1connect.PlatformServi
return err
}

func RenameNamespace(ctx context.Context, client platformv1connect.PlatformServiceClient, apiKey, oldName, newName string) error {
func (p PlatformClient) RenameNamespace(ctx context.Context, oldName, newName string) error {
request := connect.NewRequest(&platformv1.RenameNamespaceRequest{
Name: oldName,
NewName: newName,
})
request.Header().Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
response, err := client.RenameNamespace(ctx, request)
response, err := p.Client.RenameNamespace(ctx, request)
if err != nil {
return err
}
Expand All @@ -43,10 +40,9 @@ func RenameNamespace(ctx context.Context, client platformv1connect.PlatformServi
return err
}

func DeleteNamespace(ctx context.Context, client platformv1connect.PlatformServiceClient, apiKey, name string) error {
func (p PlatformClient) DeleteNamespace(ctx context.Context, name string) error {
request := connect.NewRequest(&platformv1.DeleteNamespaceRequest{Name: name})
request.Header().Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
response, err := client.DeleteNamespace(ctx, request)
response, err := p.Client.DeleteNamespace(ctx, request)
if err != nil {
return err
}
Expand All @@ -57,10 +53,9 @@ func DeleteNamespace(ctx context.Context, client platformv1connect.PlatformServi
return err
}

func ListNamespaces(ctx context.Context, client platformv1connect.PlatformServiceClient, apiKey string) ([]*platformv1.Namespace, error) {
func (p PlatformClient) ListNamespaces(ctx context.Context) ([]*platformv1.Namespace, error) {
request := connect.NewRequest(&platformv1.GetNamespacesRequest{})
request.Header().Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
response, err := client.GetNamespaces(ctx, request)
response, err := p.Client.GetNamespaces(ctx, request)
if err != nil {
return nil, err
}
Expand Down
53 changes: 32 additions & 21 deletions internal/api/subgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"fmt"

"connectrpc.com/connect"
"github.com/wundergraph/cosmo/connect-go/wg/cosmo/common"
platformv1 "github.com/wundergraph/cosmo/connect-go/wg/cosmo/platform/v1"
"github.com/wundergraph/cosmo/connect-go/wg/cosmo/platform/v1/platformv1connect"
)

func CreateSubgraph(ctx context.Context, client platformv1connect.PlatformServiceClient, apiKey string, name string, namespace string, routingUrl string, baseSubgraphName *string, labels []*platformv1.Label, subscriptionUrl *string, readme *string, isEventDrivenGraph *bool, isFeatureSubgraph *bool, subscriptionProtocol string, websocketSubprotocol string) error {
func (p PlatformClient) CreateSubgraph(ctx context.Context, name string, namespace string, routingUrl string, baseSubgraphName *string, labels []*platformv1.Label, subscriptionUrl *string, readme *string, isEventDrivenGraph *bool, isFeatureSubgraph *bool, subscriptionProtocol string, websocketSubprotocol string) error {
request := connect.NewRequest(&platformv1.CreateFederatedSubgraphRequest{
Name: name,
Namespace: namespace,
Expand All @@ -23,12 +23,19 @@ func CreateSubgraph(ctx context.Context, client platformv1connect.PlatformServic
BaseSubgraphName: baseSubgraphName,
IsFeatureSubgraph: isFeatureSubgraph,
})
request.Header().Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
_, err := client.CreateFederatedSubgraph(ctx, request)
return err
response, err := p.Client.CreateFederatedSubgraph(ctx, request)
if err != nil {
return err
}

if response.Msg.GetResponse().Code != common.EnumStatusCode_OK {
return fmt.Errorf("failed to create subgraph: %s", response.Msg)
}

return nil
}

func UpdateSubgraph(ctx context.Context, client platformv1connect.PlatformServiceClient, apiKey, name, namespace, routingUrl string, labels []*platformv1.Label, headers []string, subscriptionUrl, readme *string, unsetLabels *bool, websocketSubprotocol string, subscriptionProtocol string) error {
func (p PlatformClient) UpdateSubgraph(ctx context.Context, name, namespace, routingUrl string, labels []*platformv1.Label, headers []string, subscriptionUrl, readme *string, unsetLabels *bool, websocketSubprotocol string, subscriptionProtocol string) error {
request := connect.NewRequest(&platformv1.UpdateSubgraphRequest{
Name: name,
RoutingUrl: &routingUrl,
Expand All @@ -42,28 +49,37 @@ func UpdateSubgraph(ctx context.Context, client platformv1connect.PlatformServic
SubscriptionProtocol: resolveSubscriptionProtocol(subscriptionProtocol),
})

request.Header().Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
_, err := client.UpdateSubgraph(ctx, request)
return err
_, err := p.Client.UpdateSubgraph(ctx, request)
if err != nil {
return err
}

return nil
}

func DeleteSubgraph(ctx context.Context, client platformv1connect.PlatformServiceClient, apiKey, name, namespace string) error {
func (p PlatformClient) DeleteSubgraph(ctx context.Context, name, namespace string) error {
request := connect.NewRequest(&platformv1.DeleteFederatedSubgraphRequest{
SubgraphName: name,
Namespace: namespace,
})
request.Header().Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
_, err := client.DeleteFederatedSubgraph(ctx, request)
return err
response, err := p.Client.DeleteFederatedSubgraph(ctx, request)
if err != nil {
return err
}

if response.Msg.GetResponse().Code != common.EnumStatusCode_OK {
return fmt.Errorf("failed to delete subgraph: %s", response.Msg)
}

return nil
}

func GetSubgraph(ctx context.Context, client platformv1connect.PlatformServiceClient, apiKey, name, namespace string) (*platformv1.Subgraph, error) {
func (p PlatformClient) GetSubgraph(ctx context.Context, name, namespace string) (*platformv1.Subgraph, error) {
request := connect.NewRequest(&platformv1.GetSubgraphByNameRequest{
Name: name,
Namespace: namespace,
})
request.Header().Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
response, err := client.GetSubgraphByName(ctx, request)
response, err := p.Client.GetSubgraphByName(ctx, request)
if err != nil {
return nil, err
}
Expand All @@ -75,10 +91,5 @@ func GetSubgraph(ctx context.Context, client platformv1connect.PlatformServiceCl
RoutingURL: response.Msg.Graph.RoutingURL,
}

if response.Msg.Graph.BaseSubgraphName != nil && *response.Msg.Graph.BaseSubgraphName != "" {
baseSubgraphName := *response.Msg.Graph.BaseSubgraphName
subgraph.BaseSubgraphName = &baseSubgraphName
}

return subgraph, nil
}
11 changes: 4 additions & 7 deletions internal/api/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ import (
"connectrpc.com/connect"
"github.com/wundergraph/cosmo/connect-go/wg/cosmo/common"
platformv1 "github.com/wundergraph/cosmo/connect-go/wg/cosmo/platform/v1"
"github.com/wundergraph/cosmo/connect-go/wg/cosmo/platform/v1/platformv1connect"
)

func CreateToken(ctx context.Context, client platformv1connect.PlatformServiceClient, apiKey, name, graphName, namespace string) (string, error) {
func (p PlatformClient) CreateToken(ctx context.Context, name, graphName, namespace string) (string, error) {
request := connect.NewRequest(&platformv1.CreateFederatedGraphTokenRequest{
GraphName: graphName,
Namespace: namespace,
})

request.Header().Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
response, err := client.CreateFederatedGraphToken(ctx, request)
response, err := p.Client.CreateFederatedGraphToken(ctx, request)
if err != nil {
return "", err
}
Expand All @@ -29,14 +27,13 @@ func CreateToken(ctx context.Context, client platformv1connect.PlatformServiceCl
return fmt.Sprintf("Token created successfully: %s", response.Msg.Token), nil
}

func DeleteToken(ctx context.Context, client platformv1connect.PlatformServiceClient, apiKey, tokenName, graphName, namespace string) error {
func (p PlatformClient) DeleteToken(ctx context.Context, tokenName, graphName, namespace string) error {
request := connect.NewRequest(&platformv1.DeleteRouterTokenRequest{
TokenName: tokenName,
Namespace: namespace,
})

request.Header().Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
response, err := client.DeleteRouterToken(ctx, request)
response, err := p.Client.DeleteRouterToken(ctx, request)
if err != nil {
return fmt.Errorf("failed to delete token: %w", err)
}
Expand Down
Loading

0 comments on commit 01e059a

Please sign in to comment.