@kattebak/openapi-generator-ts 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (290) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +189 -0
  3. package/dist/cli/commands/generate.d.ts +23 -0
  4. package/dist/cli/commands/generate.d.ts.map +1 -0
  5. package/dist/cli/commands/generate.js +116 -0
  6. package/dist/cli/commands/generate.js.map +1 -0
  7. package/dist/cli/commands/list.d.ts +2 -0
  8. package/dist/cli/commands/list.d.ts.map +1 -0
  9. package/dist/cli/commands/list.js +26 -0
  10. package/dist/cli/commands/list.js.map +1 -0
  11. package/dist/cli/commands/validate.d.ts +6 -0
  12. package/dist/cli/commands/validate.d.ts.map +1 -0
  13. package/dist/cli/commands/validate.js +80 -0
  14. package/dist/cli/commands/validate.js.map +1 -0
  15. package/dist/cli/convert-template.d.ts +31 -0
  16. package/dist/cli/convert-template.d.ts.map +1 -0
  17. package/dist/cli/convert-template.js +204 -0
  18. package/dist/cli/convert-template.js.map +1 -0
  19. package/dist/cli/convert-template.test.d.ts +2 -0
  20. package/dist/cli/convert-template.test.d.ts.map +1 -0
  21. package/dist/cli/convert-template.test.js +74 -0
  22. package/dist/cli/convert-template.test.js.map +1 -0
  23. package/dist/cli/index.d.ts +3 -0
  24. package/dist/cli/index.d.ts.map +1 -0
  25. package/dist/cli/index.js +246 -0
  26. package/dist/cli/index.js.map +1 -0
  27. package/dist/core/config.d.ts +252 -0
  28. package/dist/core/config.d.ts.map +1 -0
  29. package/dist/core/config.js +31 -0
  30. package/dist/core/config.js.map +1 -0
  31. package/dist/core/generator.d.ts +99 -0
  32. package/dist/core/generator.d.ts.map +1 -0
  33. package/dist/core/generator.js +492 -0
  34. package/dist/core/generator.js.map +1 -0
  35. package/dist/core/index.d.ts +4 -0
  36. package/dist/core/index.d.ts.map +1 -0
  37. package/dist/core/index.js +4 -0
  38. package/dist/core/index.js.map +1 -0
  39. package/dist/core/types.d.ts +47 -0
  40. package/dist/core/types.d.ts.map +1 -0
  41. package/dist/core/types.js +5 -0
  42. package/dist/core/types.js.map +1 -0
  43. package/dist/generators/generators.test.d.ts +6 -0
  44. package/dist/generators/generators.test.d.ts.map +1 -0
  45. package/dist/generators/generators.test.js +247 -0
  46. package/dist/generators/generators.test.js.map +1 -0
  47. package/dist/generators/go.d.ts +10 -0
  48. package/dist/generators/go.d.ts.map +1 -0
  49. package/dist/generators/go.js +327 -0
  50. package/dist/generators/go.js.map +1 -0
  51. package/dist/generators/index.d.ts +29 -0
  52. package/dist/generators/index.d.ts.map +1 -0
  53. package/dist/generators/index.js +58 -0
  54. package/dist/generators/index.js.map +1 -0
  55. package/dist/generators/php.d.ts +14 -0
  56. package/dist/generators/php.d.ts.map +1 -0
  57. package/dist/generators/php.js +241 -0
  58. package/dist/generators/php.js.map +1 -0
  59. package/dist/generators/python.d.ts +10 -0
  60. package/dist/generators/python.d.ts.map +1 -0
  61. package/dist/generators/python.js +273 -0
  62. package/dist/generators/python.js.map +1 -0
  63. package/dist/generators/typescript-fetch.d.ts +14 -0
  64. package/dist/generators/typescript-fetch.d.ts.map +1 -0
  65. package/dist/generators/typescript-fetch.js +217 -0
  66. package/dist/generators/typescript-fetch.js.map +1 -0
  67. package/dist/index.d.ts +14 -0
  68. package/dist/index.d.ts.map +1 -0
  69. package/dist/index.js +16 -0
  70. package/dist/index.js.map +1 -0
  71. package/dist/models/codegen-model.d.ts +123 -0
  72. package/dist/models/codegen-model.d.ts.map +1 -0
  73. package/dist/models/codegen-model.js +64 -0
  74. package/dist/models/codegen-model.js.map +1 -0
  75. package/dist/models/codegen-model.test.d.ts +6 -0
  76. package/dist/models/codegen-model.test.d.ts.map +1 -0
  77. package/dist/models/codegen-model.test.js +143 -0
  78. package/dist/models/codegen-model.test.js.map +1 -0
  79. package/dist/models/codegen-operation.d.ts +95 -0
  80. package/dist/models/codegen-operation.d.ts.map +1 -0
  81. package/dist/models/codegen-operation.js +54 -0
  82. package/dist/models/codegen-operation.js.map +1 -0
  83. package/dist/models/codegen-parameter.d.ts +86 -0
  84. package/dist/models/codegen-parameter.d.ts.map +1 -0
  85. package/dist/models/codegen-parameter.js +47 -0
  86. package/dist/models/codegen-parameter.js.map +1 -0
  87. package/dist/models/codegen-property.d.ts +95 -0
  88. package/dist/models/codegen-property.d.ts.map +1 -0
  89. package/dist/models/codegen-property.js +47 -0
  90. package/dist/models/codegen-property.js.map +1 -0
  91. package/dist/models/codegen-response.d.ts +64 -0
  92. package/dist/models/codegen-response.d.ts.map +1 -0
  93. package/dist/models/codegen-response.js +42 -0
  94. package/dist/models/codegen-response.js.map +1 -0
  95. package/dist/models/codegen-security.d.ts +34 -0
  96. package/dist/models/codegen-security.d.ts.map +1 -0
  97. package/dist/models/codegen-security.js +18 -0
  98. package/dist/models/codegen-security.js.map +1 -0
  99. package/dist/models/index.d.ts +8 -0
  100. package/dist/models/index.d.ts.map +1 -0
  101. package/dist/models/index.js +7 -0
  102. package/dist/models/index.js.map +1 -0
  103. package/dist/parser/index.d.ts +4 -0
  104. package/dist/parser/index.d.ts.map +1 -0
  105. package/dist/parser/index.js +4 -0
  106. package/dist/parser/index.js.map +1 -0
  107. package/dist/parser/openapi-parser.d.ts +71 -0
  108. package/dist/parser/openapi-parser.d.ts.map +1 -0
  109. package/dist/parser/openapi-parser.js +195 -0
  110. package/dist/parser/openapi-parser.js.map +1 -0
  111. package/dist/parser/openapi-parser.test.d.ts +6 -0
  112. package/dist/parser/openapi-parser.test.d.ts.map +1 -0
  113. package/dist/parser/openapi-parser.test.js +238 -0
  114. package/dist/parser/openapi-parser.test.js.map +1 -0
  115. package/dist/parser/operation-transformer.d.ts +95 -0
  116. package/dist/parser/operation-transformer.d.ts.map +1 -0
  117. package/dist/parser/operation-transformer.js +634 -0
  118. package/dist/parser/operation-transformer.js.map +1 -0
  119. package/dist/parser/schema-transformer.d.ts +134 -0
  120. package/dist/parser/schema-transformer.d.ts.map +1 -0
  121. package/dist/parser/schema-transformer.js +557 -0
  122. package/dist/parser/schema-transformer.js.map +1 -0
  123. package/dist/template/engine-adapter.d.ts +55 -0
  124. package/dist/template/engine-adapter.d.ts.map +1 -0
  125. package/dist/template/engine-adapter.js +178 -0
  126. package/dist/template/engine-adapter.js.map +1 -0
  127. package/dist/template/engine-adapter.test.d.ts +6 -0
  128. package/dist/template/engine-adapter.test.d.ts.map +1 -0
  129. package/dist/template/engine-adapter.test.js +186 -0
  130. package/dist/template/engine-adapter.test.js.map +1 -0
  131. package/dist/template/index.d.ts +5 -0
  132. package/dist/template/index.d.ts.map +1 -0
  133. package/dist/template/index.js +5 -0
  134. package/dist/template/index.js.map +1 -0
  135. package/dist/template/lambdas/indent-lambdas.d.ts +40 -0
  136. package/dist/template/lambdas/indent-lambdas.d.ts.map +1 -0
  137. package/dist/template/lambdas/indent-lambdas.js +98 -0
  138. package/dist/template/lambdas/indent-lambdas.js.map +1 -0
  139. package/dist/template/lambdas/index.d.ts +44 -0
  140. package/dist/template/lambdas/index.d.ts.map +1 -0
  141. package/dist/template/lambdas/index.js +79 -0
  142. package/dist/template/lambdas/index.js.map +1 -0
  143. package/dist/template/lambdas/string-lambdas.d.ts +78 -0
  144. package/dist/template/lambdas/string-lambdas.d.ts.map +1 -0
  145. package/dist/template/lambdas/string-lambdas.js +148 -0
  146. package/dist/template/lambdas/string-lambdas.js.map +1 -0
  147. package/dist/template/lambdas/string-lambdas.test.d.ts +6 -0
  148. package/dist/template/lambdas/string-lambdas.test.d.ts.map +1 -0
  149. package/dist/template/lambdas/string-lambdas.test.js +158 -0
  150. package/dist/template/lambdas/string-lambdas.test.js.map +1 -0
  151. package/dist/template/template-locator.d.ts +72 -0
  152. package/dist/template/template-locator.d.ts.map +1 -0
  153. package/dist/template/template-locator.js +173 -0
  154. package/dist/template/template-locator.js.map +1 -0
  155. package/dist/template/template-manager.d.ts +65 -0
  156. package/dist/template/template-manager.d.ts.map +1 -0
  157. package/dist/template/template-manager.js +185 -0
  158. package/dist/template/template-manager.js.map +1 -0
  159. package/package.json +67 -0
  160. package/templates/go/.travis.yml +8 -0
  161. package/templates/go/README.mustache +236 -0
  162. package/templates/go/api.mustache +452 -0
  163. package/templates/go/api_doc.mustache +92 -0
  164. package/templates/go/api_test.mustache +59 -0
  165. package/templates/go/client.mustache +761 -0
  166. package/templates/go/configuration.mustache +332 -0
  167. package/templates/go/git_push.sh.mustache +57 -0
  168. package/templates/go/gitignore.mustache +24 -0
  169. package/templates/go/go.mod.mustache +16 -0
  170. package/templates/go/go.sum.mustache +19 -0
  171. package/templates/go/model.mustache +21 -0
  172. package/templates/go/model_anyof.mustache +93 -0
  173. package/templates/go/model_doc.mustache +97 -0
  174. package/templates/go/model_enum.mustache +101 -0
  175. package/templates/go/model_oneof.mustache +160 -0
  176. package/templates/go/model_simple.mustache +572 -0
  177. package/templates/go/nullable_model.mustache +35 -0
  178. package/templates/go/openapi.mustache +1 -0
  179. package/templates/go/partial_header.mustache +18 -0
  180. package/templates/go/response.mustache +38 -0
  181. package/templates/go/signing.mustache +453 -0
  182. package/templates/go/utils.mustache +352 -0
  183. package/templates/php/.php-cs-fixer.dist.php +29 -0
  184. package/templates/php/.travis.yml +8 -0
  185. package/templates/php/ApiException.mustache +111 -0
  186. package/templates/php/Configuration.mustache +606 -0
  187. package/templates/php/FormDataProcessor.mustache +238 -0
  188. package/templates/php/HeaderSelector.mustache +265 -0
  189. package/templates/php/ModelInterface.mustache +103 -0
  190. package/templates/php/ObjectSerializer.mustache +591 -0
  191. package/templates/php/README.mustache +151 -0
  192. package/templates/php/api.mustache +891 -0
  193. package/templates/php/api_doc.mustache +105 -0
  194. package/templates/php/api_test.mustache +80 -0
  195. package/templates/php/composer.mustache +44 -0
  196. package/templates/php/git_push.sh.mustache +57 -0
  197. package/templates/php/gitignore +15 -0
  198. package/templates/php/libraries/psr-18/ApiException.mustache +114 -0
  199. package/templates/php/libraries/psr-18/DebugPlugin.mustache +93 -0
  200. package/templates/php/libraries/psr-18/README.mustache +161 -0
  201. package/templates/php/libraries/psr-18/api.mustache +833 -0
  202. package/templates/php/libraries/psr-18/api_doc.mustache +79 -0
  203. package/templates/php/libraries/psr-18/composer.mustache +56 -0
  204. package/templates/php/model.mustache +47 -0
  205. package/templates/php/model_doc.mustache +10 -0
  206. package/templates/php/model_enum.mustache +33 -0
  207. package/templates/php/model_generic.mustache +565 -0
  208. package/templates/php/model_test.mustache +88 -0
  209. package/templates/php/partial_header.mustache +18 -0
  210. package/templates/php/php_doc_auth_partial.mustache +23 -0
  211. package/templates/php/phpunit.xml.mustache +18 -0
  212. package/templates/python/README.mustache +60 -0
  213. package/templates/python/README_onlypackage.mustache +50 -0
  214. package/templates/python/__init__.mustache +1 -0
  215. package/templates/python/__init__api.mustache +19 -0
  216. package/templates/python/__init__model.mustache +22 -0
  217. package/templates/python/__init__package.mustache +49 -0
  218. package/templates/python/api.mustache +244 -0
  219. package/templates/python/api_client.mustache +822 -0
  220. package/templates/python/api_doc.mustache +81 -0
  221. package/templates/python/api_doc_example.mustache +38 -0
  222. package/templates/python/api_response.mustache +21 -0
  223. package/templates/python/api_test.mustache +48 -0
  224. package/templates/python/asyncio/rest.mustache +209 -0
  225. package/templates/python/common_README.mustache +85 -0
  226. package/templates/python/configuration.mustache +806 -0
  227. package/templates/python/exceptions.mustache +210 -0
  228. package/templates/python/exports_api.mustache +3 -0
  229. package/templates/python/exports_model.mustache +3 -0
  230. package/templates/python/exports_package.mustache +20 -0
  231. package/templates/python/git_push.sh.mustache +57 -0
  232. package/templates/python/github-workflow.mustache +35 -0
  233. package/templates/python/gitignore.mustache +66 -0
  234. package/templates/python/gitlab-ci.mustache +31 -0
  235. package/templates/python/httpx/rest.mustache +190 -0
  236. package/templates/python/model.mustache +16 -0
  237. package/templates/python/model_anyof.mustache +182 -0
  238. package/templates/python/model_doc.mustache +40 -0
  239. package/templates/python/model_enum.mustache +36 -0
  240. package/templates/python/model_generic.mustache +403 -0
  241. package/templates/python/model_oneof.mustache +209 -0
  242. package/templates/python/model_test.mustache +60 -0
  243. package/templates/python/partial_api.mustache +52 -0
  244. package/templates/python/partial_api_args.mustache +18 -0
  245. package/templates/python/partial_header.mustache +19 -0
  246. package/templates/python/py.typed.mustache +1 -0
  247. package/templates/python/pyproject.mustache +172 -0
  248. package/templates/python/python_doc_auth_partial.mustache +108 -0
  249. package/templates/python/requirements.mustache +23 -0
  250. package/templates/python/rest.mustache +254 -0
  251. package/templates/python/setup.mustache +60 -0
  252. package/templates/python/setup_cfg.mustache +2 -0
  253. package/templates/python/signing.mustache +422 -0
  254. package/templates/python/test-requirements.mustache +6 -0
  255. package/templates/python/tornado/rest.mustache +148 -0
  256. package/templates/python/tox.mustache +9 -0
  257. package/templates/python/travis.mustache +17 -0
  258. package/templates/typescript-fetch/ApiEntitiesRecord.mustache +26 -0
  259. package/templates/typescript-fetch/ApiEntitiesReducer.mustache +21 -0
  260. package/templates/typescript-fetch/ApiEntitiesSelectors.mustache +5 -0
  261. package/templates/typescript-fetch/README.mustache +127 -0
  262. package/templates/typescript-fetch/allSagas.mustache +19 -0
  263. package/templates/typescript-fetch/api_doc.mustache +63 -0
  264. package/templates/typescript-fetch/api_example.mustache +44 -0
  265. package/templates/typescript-fetch/apis.index.mustache +16 -0
  266. package/templates/typescript-fetch/apis.mustache +483 -0
  267. package/templates/typescript-fetch/apisAssignQueryParam.mustache +12 -0
  268. package/templates/typescript-fetch/gitignore +4 -0
  269. package/templates/typescript-fetch/index.mustache +17 -0
  270. package/templates/typescript-fetch/licenseInfo.mustache +11 -0
  271. package/templates/typescript-fetch/modelEnum.mustache +28 -0
  272. package/templates/typescript-fetch/modelEnumInterfaces.mustache +37 -0
  273. package/templates/typescript-fetch/modelGeneric.mustache +261 -0
  274. package/templates/typescript-fetch/modelGenericInterfaces.mustache +50 -0
  275. package/templates/typescript-fetch/modelOneOf.mustache +255 -0
  276. package/templates/typescript-fetch/modelOneOfInterfaces.mustache +6 -0
  277. package/templates/typescript-fetch/model_doc.mustache +41 -0
  278. package/templates/typescript-fetch/models.index.mustache +32 -0
  279. package/templates/typescript-fetch/models.mustache +24 -0
  280. package/templates/typescript-fetch/npmignore.mustache +1 -0
  281. package/templates/typescript-fetch/package.mustache +43 -0
  282. package/templates/typescript-fetch/recordGeneric.mustache +295 -0
  283. package/templates/typescript-fetch/records.mustache +17 -0
  284. package/templates/typescript-fetch/runtime.mustache +424 -0
  285. package/templates/typescript-fetch/runtimeSagasAndRecords.mustache +120 -0
  286. package/templates/typescript-fetch/sagaApiManager.mustache +28 -0
  287. package/templates/typescript-fetch/sagas.mustache +245 -0
  288. package/templates/typescript-fetch/sourceLibraryIndex.mustache +1 -0
  289. package/templates/typescript-fetch/tsconfig.esm.mustache +7 -0
  290. package/templates/typescript-fetch/tsconfig.mustache +25 -0
@@ -0,0 +1,761 @@
1
+ {{>partial_header}}
2
+ package {{packageName}}
3
+
4
+ import (
5
+ "bytes"
6
+ "context"
7
+ "encoding/json"
8
+ "encoding/xml"
9
+ "errors"
10
+ "fmt"
11
+ "io"
12
+ "log"
13
+ "mime/multipart"
14
+ "net/http"
15
+ "net/http/httputil"
16
+ "net/url"
17
+ "os"
18
+ "path/filepath"
19
+ "reflect"
20
+ "regexp"
21
+ "strconv"
22
+ "strings"
23
+ "time"
24
+ "unicode/utf8"
25
+
26
+ {{#hasOAuthMethods}}
27
+ "golang.org/x/oauth2"
28
+ {{/hasOAuthMethods}}
29
+ {{#withAWSV4Signature}}
30
+ awsv4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
31
+ awscredentials "github.com/aws/aws-sdk-go-v2/credentials"
32
+ "crypto/sha256"
33
+ "encoding/hex"
34
+ {{/withAWSV4Signature}}
35
+ )
36
+
37
+ var (
38
+ JsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:[^;]+\+)?json)`)
39
+ XmlCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:[^;]+\+)?xml)`)
40
+ queryParamSplit = regexp.MustCompile(`(^|&)([^&]+)`)
41
+ queryDescape = strings.NewReplacer( "%5B", "[", "%5D", "]" )
42
+ )
43
+
44
+ // APIClient manages communication with the {{appName}} API v{{version}}
45
+ // In most cases there should be only one, shared, APIClient.
46
+ type APIClient struct {
47
+ cfg *Configuration
48
+ common service // Reuse a single struct instead of allocating one for each service on the heap.
49
+
50
+ // API Services
51
+ {{#apiInfo}}
52
+ {{#apis}}
53
+ {{#operations}}
54
+
55
+ {{classname}} {{#generateInterfaces}}{{classname}}{{/generateInterfaces}}{{^generateInterfaces}}*{{classname}}Service{{/generateInterfaces}}
56
+ {{/operations}}
57
+ {{/apis}}
58
+ {{/apiInfo}}
59
+ }
60
+
61
+ type service struct {
62
+ client *APIClient
63
+ }
64
+
65
+ // NewAPIClient creates a new API client. Requires a userAgent string describing your application.
66
+ // optionally a custom http.Client to allow for advanced features such as caching.
67
+ func NewAPIClient(cfg *Configuration) *APIClient {
68
+ if cfg.HTTPClient == nil {
69
+ cfg.HTTPClient = http.DefaultClient
70
+ }
71
+
72
+ c := &APIClient{}
73
+ c.cfg = cfg
74
+ c.common.client = c
75
+
76
+ {{#apiInfo}}
77
+ // API Services
78
+ {{#apis}}
79
+ {{#operations}}
80
+ c.{{classname}} = (*{{classname}}Service)(&c.common)
81
+ {{/operations}}
82
+ {{/apis}}
83
+ {{/apiInfo}}
84
+
85
+ return c
86
+ }
87
+
88
+ func atoi(in string) (int, error) {
89
+ return strconv.Atoi(in)
90
+ }
91
+
92
+ // selectHeaderContentType select a content type from the available list.
93
+ func selectHeaderContentType(contentTypes []string) string {
94
+ if len(contentTypes) == 0 {
95
+ return ""
96
+ }
97
+ if contains(contentTypes, "application/json") {
98
+ return "application/json"
99
+ }
100
+ return contentTypes[0] // use the first content type specified in 'consumes'
101
+ }
102
+
103
+ // selectHeaderAccept join all accept types and return
104
+ func selectHeaderAccept(accepts []string) string {
105
+ if len(accepts) == 0 {
106
+ return ""
107
+ }
108
+
109
+ if contains(accepts, "application/json") {
110
+ return "application/json"
111
+ }
112
+
113
+ return strings.Join(accepts, ",")
114
+ }
115
+
116
+ // contains is a case insensitive match, finding needle in a haystack
117
+ func contains(haystack []string, needle string) bool {
118
+ for _, a := range haystack {
119
+ if strings.EqualFold(a, needle) {
120
+ return true
121
+ }
122
+ }
123
+ return false
124
+ }
125
+
126
+ // Verify optional parameters are of the correct type.
127
+ func typeCheckParameter(obj interface{}, expected string, name string) error {
128
+ // Make sure there is an object.
129
+ if obj == nil {
130
+ return nil
131
+ }
132
+
133
+ // Check the type is as expected.
134
+ if reflect.TypeOf(obj).String() != expected {
135
+ return fmt.Errorf("expected %s to be of type %s but received %s", name, expected, reflect.TypeOf(obj).String())
136
+ }
137
+ return nil
138
+ }
139
+
140
+ func parameterValueToString( obj interface{}, key string ) string {
141
+ if reflect.TypeOf(obj).Kind() != reflect.Ptr {
142
+ if actualObj, ok := obj.(interface{ GetActualInstanceValue() interface{} }); ok {
143
+ return fmt.Sprintf("%v", actualObj.GetActualInstanceValue())
144
+ }
145
+
146
+ return fmt.Sprintf("%v", obj)
147
+ }
148
+ var param,ok = obj.(MappedNullable)
149
+ if !ok {
150
+ return ""
151
+ }
152
+ dataMap,err := param.ToMap()
153
+ if err != nil {
154
+ return ""
155
+ }
156
+ return fmt.Sprintf("%v", dataMap[key])
157
+ }
158
+
159
+ // parameterAddToHeaderOrQuery adds the provided object to the request header or url query
160
+ // supporting deep object syntax
161
+ func parameterAddToHeaderOrQuery(headerOrQueryParams interface{}, keyPrefix string, obj interface{}, style string, collectionType string) {
162
+ var v = reflect.ValueOf(obj)
163
+ var value = ""
164
+ if v == reflect.ValueOf(nil) {
165
+ value = "null"
166
+ } else {
167
+ switch v.Kind() {
168
+ case reflect.Invalid:
169
+ value = "invalid"
170
+
171
+ case reflect.Struct:
172
+ if t,ok := obj.(MappedNullable); ok {
173
+ dataMap,err := t.ToMap()
174
+ if err != nil {
175
+ return
176
+ }
177
+ parameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefix, dataMap, style, collectionType)
178
+ return
179
+ }
180
+ if t, ok := obj.(time.Time); ok {
181
+ parameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefix, t.Format(time.RFC3339Nano), style, collectionType)
182
+ return
183
+ }
184
+ value = v.Type().String() + " value"
185
+ case reflect.Slice:
186
+ var indValue = reflect.ValueOf(obj)
187
+ if indValue == reflect.ValueOf(nil) {
188
+ return
189
+ }
190
+ var lenIndValue = indValue.Len()
191
+ for i:=0;i<lenIndValue;i++ {
192
+ var arrayValue = indValue.Index(i)
193
+ var keyPrefixForCollectionType = keyPrefix
194
+ if style == "deepObject" {
195
+ keyPrefixForCollectionType = keyPrefix + "[" + strconv.Itoa(i) + "]"
196
+ }
197
+ parameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefixForCollectionType, arrayValue.Interface(), style, collectionType)
198
+ }
199
+ return
200
+
201
+ case reflect.Map:
202
+ var indValue = reflect.ValueOf(obj)
203
+ if indValue == reflect.ValueOf(nil) {
204
+ return
205
+ }
206
+ iter := indValue.MapRange()
207
+ for iter.Next() {
208
+ k,v := iter.Key(), iter.Value()
209
+ parameterAddToHeaderOrQuery(headerOrQueryParams, fmt.Sprintf("%s[%s]", keyPrefix, k.String()), v.Interface(), style, collectionType)
210
+ }
211
+ return
212
+
213
+ case reflect.Interface:
214
+ fallthrough
215
+ case reflect.Ptr:
216
+ parameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefix, v.Elem().Interface(), style, collectionType)
217
+ return
218
+
219
+ case reflect.Int, reflect.Int8, reflect.Int16,
220
+ reflect.Int32, reflect.Int64:
221
+ value = strconv.FormatInt(v.Int(), 10)
222
+ case reflect.Uint, reflect.Uint8, reflect.Uint16,
223
+ reflect.Uint32, reflect.Uint64, reflect.Uintptr:
224
+ value = strconv.FormatUint(v.Uint(), 10)
225
+ case reflect.Float32, reflect.Float64:
226
+ value = strconv.FormatFloat(v.Float(), 'g', -1, 32)
227
+ case reflect.Bool:
228
+ value = strconv.FormatBool(v.Bool())
229
+ case reflect.String:
230
+ value = v.String()
231
+ default:
232
+ value = v.Type().String() + " value"
233
+ }
234
+ }
235
+
236
+ switch valuesMap := headerOrQueryParams.(type) {
237
+ case url.Values:
238
+ if collectionType == "csv" && valuesMap.Get(keyPrefix) != "" {
239
+ valuesMap.Set(keyPrefix, valuesMap.Get(keyPrefix) + "," + value)
240
+ } else {
241
+ valuesMap.Add(keyPrefix, value)
242
+ }
243
+ break
244
+ case map[string]string:
245
+ valuesMap[keyPrefix] = value
246
+ break
247
+ }
248
+ }
249
+
250
+ // helper for converting interface{} parameters to json strings
251
+ func parameterToJson(obj interface{}) (string, error) {
252
+ jsonBuf, err := json.Marshal(obj)
253
+ if err != nil {
254
+ return "", err
255
+ }
256
+ return string(jsonBuf), err
257
+ }
258
+
259
+ // callAPI do the request.
260
+ func (c *APIClient) callAPI(request *http.Request) (*http.Response, error) {
261
+ if c.cfg.Debug {
262
+ dump, err := httputil.DumpRequestOut(request, true)
263
+ if err != nil {
264
+ return nil, err
265
+ }
266
+ log.Printf("\n%s\n", string(dump))
267
+ }
268
+
269
+ resp, err := c.cfg.HTTPClient.Do(request)
270
+ if err != nil {
271
+ return resp, err
272
+ }
273
+
274
+ if c.cfg.Debug {
275
+ dump, err := httputil.DumpResponse(resp, true)
276
+ if err != nil {
277
+ return resp, err
278
+ }
279
+ log.Printf("\n%s\n", string(dump))
280
+ }
281
+ return resp, err
282
+ }
283
+
284
+ // Allow modification of underlying config for alternate implementations and testing
285
+ // Caution: modifying the configuration while live can cause data races and potentially unwanted behavior
286
+ func (c *APIClient) GetConfig() *Configuration {
287
+ return c.cfg
288
+ }
289
+
290
+ type formFile struct {
291
+ fileBytes []byte
292
+ fileName string
293
+ formFileName string
294
+ }
295
+
296
+ // prepareRequest build the request
297
+ func (c *APIClient) prepareRequest(
298
+ ctx context.Context,
299
+ path string, method string,
300
+ postBody interface{},
301
+ headerParams map[string]string,
302
+ queryParams url.Values,
303
+ formParams url.Values,
304
+ formFiles []formFile) (localVarRequest *http.Request, err error) {
305
+
306
+ var body *bytes.Buffer
307
+
308
+ // Detect postBody type and post.
309
+ if postBody != nil {
310
+ contentType := headerParams["Content-Type"]
311
+ if contentType == "" {
312
+ contentType = detectContentType(postBody)
313
+ headerParams["Content-Type"] = contentType
314
+ }
315
+
316
+ body, err = setBody(postBody, contentType)
317
+ if err != nil {
318
+ return nil, err
319
+ }
320
+ }
321
+
322
+ // add form parameters and file if available.
323
+ if strings.HasPrefix(headerParams["Content-Type"], "multipart/form-data") && len(formParams) > 0 || (len(formFiles) > 0) {
324
+ if body != nil {
325
+ return nil, errors.New("Cannot specify postBody and multipart form at the same time.")
326
+ }
327
+ body = &bytes.Buffer{}
328
+ w := multipart.NewWriter(body)
329
+
330
+ for k, v := range formParams {
331
+ for _, iv := range v {
332
+ if strings.HasPrefix(k, "@") { // file
333
+ err = addFile(w, k[1:], iv)
334
+ if err != nil {
335
+ return nil, err
336
+ }
337
+ } else { // form value
338
+ w.WriteField(k, iv)
339
+ }
340
+ }
341
+ }
342
+ for _, formFile := range formFiles {
343
+ if len(formFile.fileBytes) > 0 && formFile.fileName != "" {
344
+ w.Boundary()
345
+ part, err := w.CreateFormFile(formFile.formFileName, filepath.Base(formFile.fileName))
346
+ if err != nil {
347
+ return nil, err
348
+ }
349
+ _, err = part.Write(formFile.fileBytes)
350
+ if err != nil {
351
+ return nil, err
352
+ }
353
+ }
354
+ }
355
+
356
+ // Set the Boundary in the Content-Type
357
+ headerParams["Content-Type"] = w.FormDataContentType()
358
+
359
+ // Set Content-Length
360
+ headerParams["Content-Length"] = fmt.Sprintf("%d", body.Len())
361
+ w.Close()
362
+ }
363
+
364
+ if strings.HasPrefix(headerParams["Content-Type"], "application/x-www-form-urlencoded") && len(formParams) > 0 {
365
+ if body != nil {
366
+ return nil, errors.New("Cannot specify postBody and x-www-form-urlencoded form at the same time.")
367
+ }
368
+ body = &bytes.Buffer{}
369
+ body.WriteString(formParams.Encode())
370
+ // Set Content-Length
371
+ headerParams["Content-Length"] = fmt.Sprintf("%d", body.Len())
372
+ }
373
+
374
+ // Setup path and query parameters
375
+ url, err := url.Parse(path)
376
+ if err != nil {
377
+ return nil, err
378
+ }
379
+
380
+ // Override request host, if applicable
381
+ if c.cfg.Host != "" {
382
+ url.Host = c.cfg.Host
383
+ }
384
+
385
+ // Override request scheme, if applicable
386
+ if c.cfg.Scheme != "" {
387
+ url.Scheme = c.cfg.Scheme
388
+ }
389
+
390
+ // Adding Query Param
391
+ query := url.Query()
392
+ for k, v := range queryParams {
393
+ for _, iv := range v {
394
+ query.Add(k, iv)
395
+ }
396
+ }
397
+
398
+ // Encode the parameters.
399
+ url.RawQuery = queryParamSplit.ReplaceAllStringFunc(query.Encode(), func(s string) string {
400
+ pieces := strings.Split(s, "=")
401
+ pieces[0] = queryDescape.Replace(pieces[0])
402
+ return strings.Join(pieces, "=")
403
+ })
404
+
405
+ // Generate a new request
406
+ if body != nil {
407
+ localVarRequest, err = http.NewRequest(method, url.String(), body)
408
+ } else {
409
+ localVarRequest, err = http.NewRequest(method, url.String(), nil)
410
+ }
411
+ if err != nil {
412
+ return nil, err
413
+ }
414
+
415
+ // add header parameters, if any
416
+ if len(headerParams) > 0 {
417
+ headers := http.Header{}
418
+ for h, v := range headerParams {
419
+ headers[h] = []string{v}
420
+ }
421
+ localVarRequest.Header = headers
422
+ }
423
+
424
+ // Add the user agent to the request.
425
+ localVarRequest.Header.Add("User-Agent", c.cfg.UserAgent)
426
+
427
+ if ctx != nil {
428
+ // add context to the request
429
+ localVarRequest = localVarRequest.WithContext(ctx)
430
+
431
+ // Walk through any authentication.
432
+
433
+ {{#hasOAuthMethods}}
434
+ // OAuth2 authentication
435
+ if tok, ok := ctx.Value(ContextOAuth2).(oauth2.TokenSource); ok {
436
+ // We were able to grab an oauth2 token from the context
437
+ var latestToken *oauth2.Token
438
+ if latestToken, err = tok.Token(); err != nil {
439
+ return nil, err
440
+ }
441
+
442
+ latestToken.SetAuthHeader(localVarRequest)
443
+ }
444
+
445
+ {{/hasOAuthMethods}}
446
+ {{#hasHttpBasicMethods}}
447
+ // Basic HTTP Authentication
448
+ if auth, ok := ctx.Value(ContextBasicAuth).(BasicAuth); ok {
449
+ localVarRequest.SetBasicAuth(auth.UserName, auth.Password)
450
+ }
451
+
452
+ {{/hasHttpBasicMethods}}
453
+ {{#hasHttpBearerMethods}}
454
+ // AccessToken Authentication
455
+ if auth, ok := ctx.Value(ContextAccessToken).(string); ok {
456
+ localVarRequest.Header.Add("Authorization", "Bearer "+auth)
457
+ }
458
+
459
+ {{/hasHttpBearerMethods}}
460
+ {{#withAWSV4Signature}}
461
+ // AWS Signature v4 Authentication
462
+ if auth, ok := ctx.Value(ContextAWSv4).(AWSv4); ok {
463
+ credsProvider := awscredentials.NewStaticCredentialsProvider(auth.AccessKey, auth.SecretKey, auth.SessionToken)
464
+ creds, err := credsProvider.Retrieve(ctx)
465
+ if err != nil {
466
+ return nil, err
467
+ }
468
+
469
+ // Define default values for region and service to maintain backward compatibility
470
+ region := auth.Region
471
+ if region == "" {
472
+ region = "eu-west-2"
473
+ }
474
+ service := auth.Service
475
+ if service == "" {
476
+ service = "oapi"
477
+ }
478
+
479
+ // Compute payload hash from the request body
480
+ var payloadHash string
481
+ if body == nil {
482
+ // Empty body
483
+ hash := sha256.Sum256([]byte(""))
484
+ payloadHash = hex.EncodeToString(hash[:])
485
+ } else {
486
+ // Hash the actual body content
487
+ bodyBytes := []byte(body.String())
488
+ hash := sha256.Sum256(bodyBytes)
489
+ payloadHash = hex.EncodeToString(hash[:])
490
+ }
491
+
492
+ // Sign the request with the computed payload hash
493
+ signer := awsv4.NewSigner()
494
+ err = signer.SignHTTP(ctx, creds, localVarRequest, payloadHash, service, region, time.Now())
495
+ if err != nil {
496
+ return nil, err
497
+ }
498
+ }
499
+ {{/withAWSV4Signature}}
500
+ }
501
+
502
+ for header, value := range c.cfg.DefaultHeader {
503
+ localVarRequest.Header.Add(header, value)
504
+ }
505
+ {{#withCustomMiddlewareFunction}}
506
+
507
+ if c.cfg.Middleware != nil {
508
+ c.cfg.Middleware(localVarRequest)
509
+ }
510
+
511
+ if c.cfg.MiddlewareWithError != nil {
512
+ err = c.cfg.MiddlewareWithError(localVarRequest)
513
+ if err != nil {
514
+ return nil, err
515
+ }
516
+ }
517
+
518
+ {{/withCustomMiddlewareFunction}}
519
+ {{#hasHttpSignatureMethods}}
520
+ if ctx != nil {
521
+ // HTTP Signature Authentication. All request headers must be set (including default headers)
522
+ // because the headers may be included in the signature.
523
+ if auth, ok := ctx.Value(ContextHttpSignatureAuth).(HttpSignatureAuth); ok {
524
+ err = SignRequest(ctx, localVarRequest, auth)
525
+ if err != nil {
526
+ return nil, err
527
+ }
528
+ }
529
+ }
530
+ {{/hasHttpSignatureMethods}}
531
+ return localVarRequest, nil
532
+ }
533
+
534
+ func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err error) {
535
+ if len(b) == 0 {
536
+ return nil
537
+ }
538
+ if s, ok := v.(*string); ok {
539
+ *s = string(b)
540
+ return nil
541
+ }
542
+ if f, ok := v.(*os.File); ok {
543
+ f, err = os.CreateTemp("", "HttpClientFile")
544
+ if err != nil {
545
+ return
546
+ }
547
+ _, err = f.Write(b)
548
+ if err != nil {
549
+ return
550
+ }
551
+ _, err = f.Seek(0, io.SeekStart)
552
+ return
553
+ }
554
+ if f, ok := v.(**os.File); ok {
555
+ *f, err = os.CreateTemp("", "HttpClientFile")
556
+ if err != nil {
557
+ return
558
+ }
559
+ _, err = (*f).Write(b)
560
+ if err != nil {
561
+ return
562
+ }
563
+ _, err = (*f).Seek(0, io.SeekStart)
564
+ return
565
+ }
566
+ if XmlCheck.MatchString(contentType) {
567
+ if err = xml.Unmarshal(b, v); err != nil {
568
+ return err
569
+ }
570
+ return nil
571
+ }
572
+ if JsonCheck.MatchString(contentType) {
573
+ if actualObj, ok := v.(interface{ GetActualInstance() interface{} }); ok { // oneOf, anyOf schemas
574
+ if unmarshalObj, ok := actualObj.(interface{ UnmarshalJSON([]byte) error }); ok { // make sure it has UnmarshalJSON defined
575
+ if err = unmarshalObj.UnmarshalJSON(b); err != nil {
576
+ return err
577
+ }
578
+ } else {
579
+ return errors.New("Unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined")
580
+ }
581
+ } else if err = json.Unmarshal(b, v); err != nil { // simple model
582
+ return err
583
+ }
584
+ return nil
585
+ }
586
+ return errors.New("undefined response type")
587
+ }
588
+
589
+ // Add a file to the multipart request
590
+ func addFile(w *multipart.Writer, fieldName, path string) error {
591
+ file, err := os.Open(filepath.Clean(path))
592
+ if err != nil {
593
+ return err
594
+ }
595
+ err = file.Close()
596
+ if err != nil {
597
+ return err
598
+ }
599
+
600
+ part, err := w.CreateFormFile(fieldName, filepath.Base(path))
601
+ if err != nil {
602
+ return err
603
+ }
604
+ _, err = io.Copy(part, file)
605
+
606
+ return err
607
+ }
608
+
609
+ // Set request body from an interface{}
610
+ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err error) {
611
+ if bodyBuf == nil {
612
+ bodyBuf = &bytes.Buffer{}
613
+ }
614
+
615
+ if reader, ok := body.(io.Reader); ok {
616
+ _, err = bodyBuf.ReadFrom(reader)
617
+ } else if fp, ok := body.(*os.File); ok {
618
+ _, err = bodyBuf.ReadFrom(fp)
619
+ } else if b, ok := body.([]byte); ok {
620
+ _, err = bodyBuf.Write(b)
621
+ } else if s, ok := body.(string); ok {
622
+ _, err = bodyBuf.WriteString(s)
623
+ } else if s, ok := body.(*string); ok {
624
+ _, err = bodyBuf.WriteString(*s)
625
+ } else if JsonCheck.MatchString(contentType) {
626
+ err = json.NewEncoder(bodyBuf).Encode(body)
627
+ } else if XmlCheck.MatchString(contentType) {
628
+ var bs []byte
629
+ bs, err = xml.Marshal(body)
630
+ if err == nil {
631
+ bodyBuf.Write(bs)
632
+ }
633
+ }
634
+
635
+ if err != nil {
636
+ return nil, err
637
+ }
638
+
639
+ if bodyBuf.Len() == 0 {
640
+ err = fmt.Errorf("invalid body type %s\n", contentType)
641
+ return nil, err
642
+ }
643
+ return bodyBuf, nil
644
+ }
645
+
646
+ // detectContentType method is used to figure out `Request.Body` content type for request header
647
+ func detectContentType(body interface{}) string {
648
+ contentType := "text/plain; charset=utf-8"
649
+ kind := reflect.TypeOf(body).Kind()
650
+
651
+ switch kind {
652
+ case reflect.Struct, reflect.Map, reflect.Ptr:
653
+ contentType = "application/json; charset=utf-8"
654
+ case reflect.String:
655
+ contentType = "text/plain; charset=utf-8"
656
+ default:
657
+ if b, ok := body.([]byte); ok {
658
+ contentType = http.DetectContentType(b)
659
+ } else if kind == reflect.Slice {
660
+ contentType = "application/json; charset=utf-8"
661
+ }
662
+ }
663
+
664
+ return contentType
665
+ }
666
+
667
+ // Ripped from https://github.com/gregjones/httpcache/blob/master/httpcache.go
668
+ type cacheControl map[string]string
669
+
670
+ func parseCacheControl(headers http.Header) cacheControl {
671
+ cc := cacheControl{}
672
+ ccHeader := headers.Get("Cache-Control")
673
+ for _, part := range strings.Split(ccHeader, ",") {
674
+ part = strings.Trim(part, " ")
675
+ if part == "" {
676
+ continue
677
+ }
678
+ if strings.ContainsRune(part, '=') {
679
+ keyval := strings.Split(part, "=")
680
+ cc[strings.Trim(keyval[0], " ")] = strings.Trim(keyval[1], ",")
681
+ } else {
682
+ cc[part] = ""
683
+ }
684
+ }
685
+ return cc
686
+ }
687
+
688
+ // CacheExpires helper function to determine remaining time before repeating a request.
689
+ func CacheExpires(r *http.Response) time.Time {
690
+ // Figure out when the cache expires.
691
+ var expires time.Time
692
+ now, err := time.Parse(time.RFC1123, r.Header.Get("date"))
693
+ if err != nil {
694
+ return time.Now()
695
+ }
696
+ respCacheControl := parseCacheControl(r.Header)
697
+
698
+ if maxAge, ok := respCacheControl["max-age"]; ok {
699
+ lifetime, err := time.ParseDuration(maxAge + "s")
700
+ if err != nil {
701
+ expires = now
702
+ } else {
703
+ expires = now.Add(lifetime)
704
+ }
705
+ } else {
706
+ expiresHeader := r.Header.Get("Expires")
707
+ if expiresHeader != "" {
708
+ expires, err = time.Parse(time.RFC1123, expiresHeader)
709
+ if err != nil {
710
+ expires = now
711
+ }
712
+ }
713
+ }
714
+ return expires
715
+ }
716
+
717
+ func strlen(s string) int {
718
+ return utf8.RuneCountInString(s)
719
+ }
720
+
721
+ // GenericOpenAPIError Provides access to the body, error and model on returned errors.
722
+ type GenericOpenAPIError struct {
723
+ body []byte
724
+ error string
725
+ model interface{}
726
+ }
727
+
728
+ // Error returns non-empty string if there was an error.
729
+ func (e GenericOpenAPIError) Error() string {
730
+ return e.error
731
+ }
732
+
733
+ // Body returns the raw bytes of the response
734
+ func (e GenericOpenAPIError) Body() []byte {
735
+ return e.body
736
+ }
737
+
738
+ // Model returns the unpacked model of the error
739
+ func (e GenericOpenAPIError) Model() interface{} {
740
+ return e.model
741
+ }
742
+
743
+ // format error message using title and detail when model implements rfc7807
744
+ func formatErrorMessage(status string, v interface{}) string {
745
+ str := ""
746
+ metaValue := reflect.ValueOf(v).Elem()
747
+
748
+ if metaValue.Kind() == reflect.Struct {
749
+ field := metaValue.FieldByName("Title")
750
+ if field != (reflect.Value{}) {
751
+ str = fmt.Sprintf("%s", field.Interface())
752
+ }
753
+
754
+ field = metaValue.FieldByName("Detail")
755
+ if field != (reflect.Value{}) {
756
+ str = fmt.Sprintf("%s (%s)", str, field.Interface())
757
+ }
758
+ }
759
+
760
+ return strings.TrimSpace(fmt.Sprintf("%s %s", status, str))
761
+ }