Skip to content

Commit

Permalink
Merge pull request #70 from CircleCI-Public/gql-address-pure-fn
Browse files Browse the repository at this point in the history
Refactor GraphQLServerAddress into a pure function
  • Loading branch information
Zachary Scott authored Aug 21, 2018
2 parents de70cdc + 2effa5f commit 9788d75
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
33 changes: 19 additions & 14 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,23 @@ func (response GQLResponseErrors) ToError() error {
return errors.New(strings.Join(messages, ": "))
}

// EnvEndpointHost pulls the endpoint and host values from viper
func EnvEndpointHost() (string, string) {
return viper.GetString("endpoint"), viper.GetString("host")
}

// GraphQLServerAddress returns the full address to CircleCI GraphQL API server
func GraphQLServerAddress() (string, error) {
func GraphQLServerAddress(endpoint string, host string) (string, error) {
// 1. Parse the endpoint
endpoint, err := url.Parse(viper.GetString("endpoint"))
e, err := url.Parse(endpoint)
if err != nil {
return endpoint.String(), errors.Wrapf(err, "Parsing endpoint '%s'", viper.GetString("endpoint"))
return e.String(), errors.Wrapf(err, "Parsing endpoint '%s'", endpoint)
}

// 2. Parse the host
host, err := url.Parse(viper.GetString("host"))
h, err := url.Parse(host)
if err != nil {
return host.String(), errors.Wrapf(err, "Parsing host '%s'", viper.GetString("host"))
return h.String(), errors.Wrapf(err, "Parsing host '%s'", host)
}

// 3. Resolve the two URLs using host as the base
Expand All @@ -100,7 +105,7 @@ func GraphQLServerAddress() (string, error) {
//
// Specifically this function always returns the reference (endpoint) if provided an absolute URL.
// This way we can safely introduce --host and merge the two.
return host.ResolveReference(endpoint).String(), err
return h.ResolveReference(e).String(), err
}

// nolint: gosec
Expand Down Expand Up @@ -128,7 +133,7 @@ func buildAndOrbQuery(ctx context.Context, logger *logger.Logger, configPath str

request := client.NewAuthorizedRequest(viper.GetString("token"), query)
request.Var("config", config)
address, err := GraphQLServerAddress()
address, err := GraphQLServerAddress(EnvEndpointHost())
if err != nil {
return err
}
Expand Down Expand Up @@ -220,7 +225,7 @@ func OrbPublish(ctx context.Context, logger *logger.Logger,
request.Var("orbId", orbID)
request.Var("version", orbVersion)

address, err := GraphQLServerAddress()
address, err := GraphQLServerAddress(EnvEndpointHost())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -250,7 +255,7 @@ func getOrbID(ctx context.Context, logger *logger.Logger, name string) (string,
request := client.NewAuthorizedRequest(viper.GetString("token"), query)
request.Var("name", name)

address, err := GraphQLServerAddress()
address, err := GraphQLServerAddress(EnvEndpointHost())
if err != nil {
return "", err
}
Expand Down Expand Up @@ -296,7 +301,7 @@ func createNamespaceWithOwnerID(ctx context.Context, logger *logger.Logger, name
request.Var("name", name)
request.Var("organizationId", ownerID)

address, err := GraphQLServerAddress()
address, err := GraphQLServerAddress(EnvEndpointHost())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -332,7 +337,7 @@ func getOrganization(ctx context.Context, logger *logger.Logger, organizationNam
request.Var("organizationName", organizationName)
request.Var("organizationVcs", organizationVcs)

address, err := GraphQLServerAddress()
address, err := GraphQLServerAddress(EnvEndpointHost())
if err != nil {
return "", err
}
Expand Down Expand Up @@ -383,7 +388,7 @@ func getNamespace(ctx context.Context, logger *logger.Logger, name string) (stri
request := client.NewAuthorizedRequest(viper.GetString("token"), query)
request.Var("name", name)

address, err := GraphQLServerAddress()
address, err := GraphQLServerAddress(EnvEndpointHost())
if err != nil {
return "", err
}
Expand Down Expand Up @@ -426,7 +431,7 @@ func createOrbWithNsID(ctx context.Context, logger *logger.Logger, name string,
request.Var("name", name)
request.Var("registryNamespaceId", namespaceID)

address, err := GraphQLServerAddress()
address, err := GraphQLServerAddress(EnvEndpointHost())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -476,7 +481,7 @@ func OrbSource(ctx context.Context, logger *logger.Logger, namespace string, orb
request := client.NewAuthorizedRequest(viper.GetString("token"), query)
request.Var("name", name)

address, err := GraphQLServerAddress()
address, err := GraphQLServerAddress(EnvEndpointHost())
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/diagnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func newDiagnosticCommand() *cobra.Command {
}

func diagnostic(cmd *cobra.Command, args []string) error {
address, err := api.GraphQLServerAddress()
address, err := api.GraphQLServerAddress(api.EnvEndpointHost())
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/orb.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ query ListOrbs ($after: String!) {
}
`)

address, err := api.GraphQLServerAddress()
address, err := api.GraphQLServerAddress(api.EnvEndpointHost())
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func query(cmd *cobra.Command, args []string) error {
var err error
// This local is named "q" to avoid confusion with the function name.
var q []byte
address, err := api.GraphQLServerAddress()
address, err := api.GraphQLServerAddress(api.EnvEndpointHost())
if err != nil {
return err
}
Expand Down

0 comments on commit 9788d75

Please sign in to comment.