@@ -65,14 +65,30 @@ func (f *CodeSampleFilter) Apply() error {
6565 return nil
6666}
6767
68- func (f * CodeSampleFilter ) newDigestCurlCodeSamplesForOperation (pathName , opMethod string ) codeSample {
68+ func getFileExtension (format string ) string {
69+ switch format {
70+ case "gzip" :
71+ return "gz"
72+ default :
73+ return format
74+ }
75+ }
76+
77+ func (f * CodeSampleFilter ) newDigestCurlCodeSamplesForOperation (pathName , opMethod , format string ) codeSample {
6978 version := apiVersion (f .metadata .targetVersion )
7079 source := "curl --user \" ${PUBLIC_KEY}:${PRIVATE_KEY}\" \\ \n --digest \\ \n " +
71- "--header \" Accept: application/vnd.atlas." + version + "+json \" \\ \n "
80+ "--header \" Accept: application/vnd.atlas." + version + "+" + format + " \" \\ \n "
7281
7382 switch opMethod {
7483 case "GET" :
75- source += "-X " + opMethod + " \" https://cloud.mongodb.com" + pathName + "?pretty=true\" "
84+ source += "-X " + opMethod + " \" https://cloud.mongodb.com" + pathName
85+ if format == "gzip" {
86+ source += "\" \\ \n "
87+ source += "--output \" file_name." + getFileExtension (format ) + "\" "
88+ } else {
89+ source += "?pretty=true\" "
90+ }
91+
7692 case "DELETE" :
7793 source += "-X " + opMethod + " \" https://cloud.mongodb.com" + pathName + "\" "
7894 case "POST" , "PATCH" , "PUT" :
@@ -88,14 +104,20 @@ func (f *CodeSampleFilter) newDigestCurlCodeSamplesForOperation(pathName, opMeth
88104 }
89105}
90106
91- func (f * CodeSampleFilter ) newServiceAccountCurlCodeSamplesForOperation (pathName , opMethod string ) codeSample {
107+ func (f * CodeSampleFilter ) newServiceAccountCurlCodeSamplesForOperation (pathName , opMethod , format string ) codeSample {
92108 version := apiVersion (f .metadata .targetVersion )
93109 source := "curl --header \" Authorization: Bearer ${ACCESS_TOKEN}\" \\ \n " +
94- "--header \" Accept: application/vnd.atlas." + version + "+json \" \\ \n "
110+ "--header \" Accept: application/vnd.atlas." + version + "+" + format + " \" \\ \n "
95111
96112 switch opMethod {
97113 case "GET" :
98- source += "-X " + opMethod + " \" https://cloud.mongodb.com" + pathName + "?pretty=true\" "
114+ source += "-X " + opMethod + " \" https://cloud.mongodb.com" + pathName
115+ if format == "gzip" {
116+ source += "\" \\ \n "
117+ source += "--output \" file_name." + getFileExtension (format ) + "\" "
118+ } else {
119+ source += "?pretty=true\" "
120+ }
99121 case "DELETE" :
100122 source += "-X " + opMethod + " \" https://cloud.mongodb.com" + pathName + "\" "
101123 case "POST" , "PATCH" , "PUT" :
@@ -193,10 +215,55 @@ func (f *CodeSampleFilter) includeCodeSamplesForOperation(pathName, opMethod str
193215 codeSamples = append (codeSamples , * sdkSample )
194216 }
195217
218+ supportedFormat := getSupportedFormat (op )
196219 codeSamples = append (
197220 codeSamples ,
198- f .newServiceAccountCurlCodeSamplesForOperation (pathName , opMethod ),
199- f .newDigestCurlCodeSamplesForOperation (pathName , opMethod ))
221+ f .newServiceAccountCurlCodeSamplesForOperation (pathName , opMethod , supportedFormat ),
222+ f .newDigestCurlCodeSamplesForOperation (pathName , opMethod , supportedFormat ))
200223 op .Extensions [codeSampleExtensionName ] = codeSamples
201224 return nil
202225}
226+
227+ // getSupportedFormat inspects the response content types of a given OpenAPI operation,
228+ // looking for a content type string in the format "application/vnd.atlas.<api_version>+<supported_format>".
229+ // It splits the content type on the '+' character and returns the last part, which represents the supported format (e.g., "json").
230+ // If no such content type is found, it defaults to returning "json".
231+ func getSupportedFormat (op * openapi3.Operation ) string {
232+ responseMap := successResponseExtensions (op .Responses .Map ())
233+ format := "json"
234+ for k := range responseMap {
235+ // k is a string with the format "application/vnd.atlas.<api_version>+<supported_format>"
236+ parts := strings .Split (k , "+" )
237+ if len (parts ) == 0 {
238+ continue
239+ }
240+
241+ format = parts [len (parts )- 1 ]
242+ // If the endpoint supports "json", we return it as "json" is the best supported format in our APIs
243+ // and users should use it when available.
244+ if format == "json" {
245+ return format
246+ }
247+ }
248+
249+ return format
250+ }
251+
252+ // successResponseExtensions returns the Content object of the first successful HTTP response (status 200, 201, 202, or 204)
253+ // found in the provided responses map.
254+ func successResponseExtensions (responsesMap map [string ]* openapi3.ResponseRef ) openapi3.Content {
255+ if val , ok := responsesMap ["200" ]; ok {
256+ return val .Value .Content
257+ }
258+ if val , ok := responsesMap ["201" ]; ok {
259+ return val .Value .Content
260+ }
261+ if val , ok := responsesMap ["202" ]; ok {
262+ return val .Value .Content
263+ }
264+ if val , ok := responsesMap ["204" ]; ok {
265+ return val .Value .Content
266+ }
267+
268+ return nil
269+ }
0 commit comments