Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generic api request function #218

Merged
merged 11 commits into from
May 1, 2024

Conversation

k3llymariee
Copy link
Contributor

@k3llymariee k3llymariee commented Apr 29, 2024

Adds a new client and method to make generic API requests based on the openapi operation structs we'll be creating for each endpoint.

@k3llymariee k3llymariee changed the title Kelly/sc 241152/generic api request function feat: generic api request function Apr 29, 2024
@@ -1,42 +0,0 @@
package cmd_test
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

file was changed & moved not deleted

@@ -1,19 +1,18 @@
// this file WILL be generated (sc-241153)

package cmd
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changes here are just due to moving the files

"ldcli/internal/errors"
)

type Client interface {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made a new interface for testing but had an issue with passing in data to the mock client, will try to tackle in follow up PR

Comment on lines -114 to -121
var val interface{}
switch p.Type {
case "string":
val = viper.GetString(p.Name)
case "boolean":
val = viper.GetBool(p.Name)
case "int":
val = viper.GetInt(p.Name)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think it's actually ok to just grab all of these as string since we're just passing them into the query param or url anyway

@k3llymariee k3llymariee marked this pull request as ready for review April 29, 2024 23:21
@k3llymariee k3llymariee requested a review from dbolson April 29, 2024 23:21
require.NoError(t, err)
assert.Contains(t, string(output), "Create a team.")
})
// TODO: add back test when mock client is added
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use t.Skip("reason") instead of commenting-out the code so we'll see the skipped test when running the rest of the tests.

if val != "" {
switch p.In {
case "path":
urlParms = append(urlParms, fmt.Sprintf("%v", val))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since val is a string, I don't think we need the Sprintf.

@@ -97,41 +112,81 @@ func (op *OperationCmd) initFlags() error {
return nil
}

func (op *OperationCmd) makeRequest(cmd *cobra.Command, args []string) error {
paramVals := map[string]interface{}{}
func formatURL(baseURI, path string, urlParams []string) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make this more descriptive about how it's interpolating the placeholders in the URL with the urlParams?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to buildURLWithParams

jsonData,
)
if err != nil {
out, err := output.CmdOutputSingular(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use a simpler function now.

path := formatURL(viper.GetString(cliflags.BaseURIFlag), op.Path, urlParms)

contentType := "application/json"
if op.SupportsSemanticPatch && viper.GetBool("semantic-patch") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to check op.SupportsSemanticPatch here since viper will ignore the flag if it hasn't been set above.

@k3llymariee k3llymariee merged commit 0141d07 into main May 1, 2024
2 checks passed
@k3llymariee k3llymariee deleted the kelly/sc-241152/generic-api-request-function branch May 1, 2024 16:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants