|
| 1 | +// Copyright 2025 MongoDB Inc |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package filter |
| 16 | + |
| 17 | +import ( |
| 18 | + "time" |
| 19 | + |
| 20 | + "github.com/getkin/kin-openapi/openapi3" |
| 21 | +) |
| 22 | + |
| 23 | +const codeSampleExtensionName = "x-codeSamples" |
| 24 | + |
| 25 | +// https://redocly.com/docs-legacy/api-reference-docs/specification-extensions/x-code-samples#x-codesamples |
| 26 | +type codeSample struct { |
| 27 | + Lang string `json:"lang,omitempty" yaml:"lang,omitempty"` |
| 28 | + Label string `json:"label,omitempty" yaml:"label,omitempty"` |
| 29 | + Source string `json:"source,omitempty" yaml:"source,omitempty"` |
| 30 | +} |
| 31 | + |
| 32 | +// CodeSampleFilter modifies includes the fields "x-state" and "x-beta" to the "preview" and "upcoming" APIs Operations. |
| 33 | +// The "x-state" and "x-beta" fields are bump.sh custom fields to include budges |
| 34 | +// Bump.sh feature: https://docs.bump.sh/help/specification-support/doc-code-samples/#example-usage |
| 35 | +type CodeSampleFilter struct { |
| 36 | + oas *openapi3.T |
| 37 | + metadata *Metadata |
| 38 | +} |
| 39 | + |
| 40 | +func (f *CodeSampleFilter) ValidateMetadata() error { |
| 41 | + return validateMetadataWithVersion(f.metadata) |
| 42 | +} |
| 43 | + |
| 44 | +func (f *CodeSampleFilter) Apply() error { |
| 45 | + for pathName, p := range f.oas.Paths.Map() { |
| 46 | + for opK, op := range p.Operations() { |
| 47 | + if err := f.includeCodeSamplesForOperation(pathName, opK, op); err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return nil |
| 54 | +} |
| 55 | + |
| 56 | +func (f *CodeSampleFilter) newCurlCodeSamplesForOperation(pathName, opK string, op *openapi3.Operation) codeSample { |
| 57 | + source := "curl --user \"{PUBLIC-KEY}:{PRIVATE-KEY}\" \\\n --digest \\\n " + |
| 58 | + "--header \"Accept: application/vnd.atlas." + f.metadata.targetVersion.Date().Format(time.DateOnly) + "+json\" \\\n " |
| 59 | + |
| 60 | + switch opK { |
| 61 | + case "GET": |
| 62 | + source += "-X " + opK + " \"" + pathName + "?pretty=true\"" |
| 63 | + case "DELETE": |
| 64 | + source += "-X " + opK + " \"" + pathName + "\"" |
| 65 | + case "POST", "PATCH", "PUT": |
| 66 | + source += "-X " + opK + " \"" + pathName + "\"\n " |
| 67 | + source += "-d " + "{ <Payload> }" |
| 68 | + } |
| 69 | + |
| 70 | + return codeSample{ |
| 71 | + Lang: "cURL", |
| 72 | + Label: "curl", |
| 73 | + Source: source, |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func (f *CodeSampleFilter) newAtlasCliCodeSamplesForOperation(op *openapi3.Operation) codeSample { |
| 78 | + return codeSample{ |
| 79 | + Lang: "cURL", |
| 80 | + Label: "Atlas CLI", |
| 81 | + Source: "atlas api " + op.OperationID + " --help", |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func (f *CodeSampleFilter) includeCodeSamplesForOperation(pathName, opK string, op *openapi3.Operation) error { |
| 86 | + op.Extensions[codeSampleExtensionName] = []codeSample{ |
| 87 | + f.newCurlCodeSamplesForOperation(pathName, opK, op), |
| 88 | + f.newAtlasCliCodeSamplesForOperation(op), |
| 89 | + } |
| 90 | + return nil |
| 91 | +} |
0 commit comments