Skip to content

feat: add dochub link to SDK code sample #775

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

Merged
merged 5 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 3 additions & 27 deletions tools/cli/internal/openapi/filter/code_sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package filter

import (
"bytes"
_ "embed"
"fmt"
goFormat "go/format"
"strings"
Expand All @@ -28,33 +29,8 @@ import (
"golang.org/x/text/language"
)

const goSDKTemplate = `import (
"os"
"context"
"log"
sdk "go.mongodb.org/atlas-sdk/v{{ .Version }}/admin"
)

func main() {
ctx := context.Background()
clientID := os.Getenv("MONGODB_ATLAS_CLIENT_ID")
clientSecret := os.Getenv("MONGODB_ATLAS_CLIENT_SECRET")

client, err := sdk.NewClient(
sdk.UseOAuthAuth(clientID, clientSecret),
sdk.UseBaseURL(url))

if err != nil {
log.Fatalf("Error: %v", err)
}

params = &sdk.{{ .OperationID }}ApiParams{}
{{ if eq .Method "DELETE" }} httpResp, err := client.{{ .Tag }}Api.
{{ .OperationID }}WithParams(ctx, params).
Execute(){{ else }} sdkResp, httpResp, err := client.{{ .Tag }}Api.
{{ .OperationID }}WithParams(ctx, params).
Execute(){{ end}}
}`
//go:embed template/go_sdk_code_sample.txt
var goSDKTemplate string

const codeSampleExtensionName = "x-codeSamples"

Expand Down
3 changes: 2 additions & 1 deletion tools/cli/internal/openapi/filter/code_sample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,15 @@ func TestCodeSampleFilter(t *testing.T) {
"\tctx := context.Background()\n" +
"\tclientID := os.Getenv(\"MONGODB_ATLAS_CLIENT_ID\")\n" +
"\tclientSecret := os.Getenv(\"MONGODB_ATLAS_CLIENT_SECRET\")\n\n" +
"\t// See https://dochub.mongodb.org/core/atlas-go-sdk-oauth\n" +
"\tclient, err := sdk.NewClient(\n" +
"\t\tsdk.UseOAuthAuth(clientID, clientSecret),\n" +
"\t\tsdk.UseBaseURL(url))\n\n" +
"\tif err != nil {\n" + "\t\tlog.Fatalf(\"Error: %v\", err)\n\t}\n\n" +
"\tparams = &sdk.TestOperationIDApiParams{}\n" +
"\tsdkResp, httpResp, err := client.TestTagApi.\n" +
"\t\tTestOperationIDWithParams(ctx, params).\n" +
"\t\tExecute()" + "\n}",
"\t\tExecute()" + "\n}\n",
},
{
Lang: "cURL",
Expand Down
28 changes: 28 additions & 0 deletions tools/cli/internal/openapi/filter/template/go_sdk_code_sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import (
"os"
"context"
"log"
sdk "go.mongodb.org/atlas-sdk/v{{ .Version }}/admin"
)

func main() {
ctx := context.Background()
clientID := os.Getenv("MONGODB_ATLAS_CLIENT_ID")
clientSecret := os.Getenv("MONGODB_ATLAS_CLIENT_SECRET")

// See https://dochub.mongodb.org/core/atlas-go-sdk-oauth
client, err := sdk.NewClient(
sdk.UseOAuthAuth(clientID, clientSecret),
sdk.UseBaseURL(url))

if err != nil {
log.Fatalf("Error: %v", err)
}

params = &sdk.{{ .OperationID }}ApiParams{}
{{ if eq .Method "DELETE" }} httpResp, err := client.{{ .Tag }}Api.
{{ .OperationID }}WithParams(ctx, params).
Execute(){{ else }} sdkResp, httpResp, err := client.{{ .Tag }}Api.
{{ .OperationID }}WithParams(ctx, params).
Execute(){{ end}}
}
112,414 changes: 56,207 additions & 56,207 deletions tools/cli/test/data/split/dev/openapi-v2-2023-01-01.json

Large diffs are not rendered by default.

Loading
Loading