generated.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // Package client provides primitives to interact with the openapi HTTP API.
  2. //
  3. // Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.4.1 DO NOT EDIT.
  4. package client
  5. import (
  6. "bytes"
  7. "context"
  8. "encoding/json"
  9. "fmt"
  10. "io"
  11. "net/http"
  12. "net/url"
  13. "strings"
  14. )
  15. // PostSessionChatJSONBody defines parameters for PostSessionChat.
  16. type PostSessionChatJSONBody struct {
  17. Parts *interface{} `json:"parts,omitempty"`
  18. SessionID string `json:"sessionID"`
  19. }
  20. // PostSessionChatJSONRequestBody defines body for PostSessionChat for application/json ContentType.
  21. type PostSessionChatJSONRequestBody PostSessionChatJSONBody
  22. // RequestEditorFn is the function signature for the RequestEditor callback function
  23. type RequestEditorFn func(ctx context.Context, req *http.Request) error
  24. // Doer performs HTTP requests.
  25. //
  26. // The standard http.Client implements this interface.
  27. type HttpRequestDoer interface {
  28. Do(req *http.Request) (*http.Response, error)
  29. }
  30. // Client which conforms to the OpenAPI3 specification for this service.
  31. type Client struct {
  32. // The endpoint of the server conforming to this interface, with scheme,
  33. // https://api.deepmap.com for example. This can contain a path relative
  34. // to the server, such as https://api.deepmap.com/dev-test, and all the
  35. // paths in the swagger spec will be appended to the server.
  36. Server string
  37. // Doer for performing requests, typically a *http.Client with any
  38. // customized settings, such as certificate chains.
  39. Client HttpRequestDoer
  40. // A list of callbacks for modifying requests which are generated before sending over
  41. // the network.
  42. RequestEditors []RequestEditorFn
  43. }
  44. // ClientOption allows setting custom parameters during construction
  45. type ClientOption func(*Client) error
  46. // Creates a new Client, with reasonable defaults
  47. func NewClient(server string, opts ...ClientOption) (*Client, error) {
  48. // create a client with sane default values
  49. client := Client{
  50. Server: server,
  51. }
  52. // mutate client and add all optional params
  53. for _, o := range opts {
  54. if err := o(&client); err != nil {
  55. return nil, err
  56. }
  57. }
  58. // ensure the server URL always has a trailing slash
  59. if !strings.HasSuffix(client.Server, "/") {
  60. client.Server += "/"
  61. }
  62. // create httpClient, if not already present
  63. if client.Client == nil {
  64. client.Client = &http.Client{}
  65. }
  66. return &client, nil
  67. }
  68. // WithHTTPClient allows overriding the default Doer, which is
  69. // automatically created using http.Client. This is useful for tests.
  70. func WithHTTPClient(doer HttpRequestDoer) ClientOption {
  71. return func(c *Client) error {
  72. c.Client = doer
  73. return nil
  74. }
  75. }
  76. // WithRequestEditorFn allows setting up a callback function, which will be
  77. // called right before sending the request. This can be used to mutate the request.
  78. func WithRequestEditorFn(fn RequestEditorFn) ClientOption {
  79. return func(c *Client) error {
  80. c.RequestEditors = append(c.RequestEditors, fn)
  81. return nil
  82. }
  83. }
  84. // The interface specification for the client above.
  85. type ClientInterface interface {
  86. // PostSessionChatWithBody request with any body
  87. PostSessionChatWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error)
  88. PostSessionChat(ctx context.Context, body PostSessionChatJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error)
  89. // PostSessionCreate request
  90. PostSessionCreate(ctx context.Context, reqEditors ...RequestEditorFn) (*http.Response, error)
  91. }
  92. func (c *Client) PostSessionChatWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) {
  93. req, err := NewPostSessionChatRequestWithBody(c.Server, contentType, body)
  94. if err != nil {
  95. return nil, err
  96. }
  97. req = req.WithContext(ctx)
  98. if err := c.applyEditors(ctx, req, reqEditors); err != nil {
  99. return nil, err
  100. }
  101. return c.Client.Do(req)
  102. }
  103. func (c *Client) PostSessionChat(ctx context.Context, body PostSessionChatJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) {
  104. req, err := NewPostSessionChatRequest(c.Server, body)
  105. if err != nil {
  106. return nil, err
  107. }
  108. req = req.WithContext(ctx)
  109. if err := c.applyEditors(ctx, req, reqEditors); err != nil {
  110. return nil, err
  111. }
  112. return c.Client.Do(req)
  113. }
  114. func (c *Client) PostSessionCreate(ctx context.Context, reqEditors ...RequestEditorFn) (*http.Response, error) {
  115. req, err := NewPostSessionCreateRequest(c.Server)
  116. if err != nil {
  117. return nil, err
  118. }
  119. req = req.WithContext(ctx)
  120. if err := c.applyEditors(ctx, req, reqEditors); err != nil {
  121. return nil, err
  122. }
  123. return c.Client.Do(req)
  124. }
  125. // NewPostSessionChatRequest calls the generic PostSessionChat builder with application/json body
  126. func NewPostSessionChatRequest(server string, body PostSessionChatJSONRequestBody) (*http.Request, error) {
  127. var bodyReader io.Reader
  128. buf, err := json.Marshal(body)
  129. if err != nil {
  130. return nil, err
  131. }
  132. bodyReader = bytes.NewReader(buf)
  133. return NewPostSessionChatRequestWithBody(server, "application/json", bodyReader)
  134. }
  135. // NewPostSessionChatRequestWithBody generates requests for PostSessionChat with any type of body
  136. func NewPostSessionChatRequestWithBody(server string, contentType string, body io.Reader) (*http.Request, error) {
  137. var err error
  138. serverURL, err := url.Parse(server)
  139. if err != nil {
  140. return nil, err
  141. }
  142. operationPath := fmt.Sprintf("/session_chat")
  143. if operationPath[0] == '/' {
  144. operationPath = "." + operationPath
  145. }
  146. queryURL, err := serverURL.Parse(operationPath)
  147. if err != nil {
  148. return nil, err
  149. }
  150. req, err := http.NewRequest("POST", queryURL.String(), body)
  151. if err != nil {
  152. return nil, err
  153. }
  154. req.Header.Add("Content-Type", contentType)
  155. return req, nil
  156. }
  157. // NewPostSessionCreateRequest generates requests for PostSessionCreate
  158. func NewPostSessionCreateRequest(server string) (*http.Request, error) {
  159. var err error
  160. serverURL, err := url.Parse(server)
  161. if err != nil {
  162. return nil, err
  163. }
  164. operationPath := fmt.Sprintf("/session_create")
  165. if operationPath[0] == '/' {
  166. operationPath = "." + operationPath
  167. }
  168. queryURL, err := serverURL.Parse(operationPath)
  169. if err != nil {
  170. return nil, err
  171. }
  172. req, err := http.NewRequest("POST", queryURL.String(), nil)
  173. if err != nil {
  174. return nil, err
  175. }
  176. return req, nil
  177. }
  178. func (c *Client) applyEditors(ctx context.Context, req *http.Request, additionalEditors []RequestEditorFn) error {
  179. for _, r := range c.RequestEditors {
  180. if err := r(ctx, req); err != nil {
  181. return err
  182. }
  183. }
  184. for _, r := range additionalEditors {
  185. if err := r(ctx, req); err != nil {
  186. return err
  187. }
  188. }
  189. return nil
  190. }
  191. // ClientWithResponses builds on ClientInterface to offer response payloads
  192. type ClientWithResponses struct {
  193. ClientInterface
  194. }
  195. // NewClientWithResponses creates a new ClientWithResponses, which wraps
  196. // Client with return type handling
  197. func NewClientWithResponses(server string, opts ...ClientOption) (*ClientWithResponses, error) {
  198. client, err := NewClient(server, opts...)
  199. if err != nil {
  200. return nil, err
  201. }
  202. return &ClientWithResponses{client}, nil
  203. }
  204. // WithBaseURL overrides the baseURL.
  205. func WithBaseURL(baseURL string) ClientOption {
  206. return func(c *Client) error {
  207. newBaseURL, err := url.Parse(baseURL)
  208. if err != nil {
  209. return err
  210. }
  211. c.Server = newBaseURL.String()
  212. return nil
  213. }
  214. }
  215. // ClientWithResponsesInterface is the interface specification for the client with responses above.
  216. type ClientWithResponsesInterface interface {
  217. // PostSessionChatWithBodyWithResponse request with any body
  218. PostSessionChatWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*PostSessionChatResponse, error)
  219. PostSessionChatWithResponse(ctx context.Context, body PostSessionChatJSONRequestBody, reqEditors ...RequestEditorFn) (*PostSessionChatResponse, error)
  220. // PostSessionCreateWithResponse request
  221. PostSessionCreateWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*PostSessionCreateResponse, error)
  222. }
  223. type PostSessionChatResponse struct {
  224. Body []byte
  225. HTTPResponse *http.Response
  226. }
  227. // Status returns HTTPResponse.Status
  228. func (r PostSessionChatResponse) Status() string {
  229. if r.HTTPResponse != nil {
  230. return r.HTTPResponse.Status
  231. }
  232. return http.StatusText(0)
  233. }
  234. // StatusCode returns HTTPResponse.StatusCode
  235. func (r PostSessionChatResponse) StatusCode() int {
  236. if r.HTTPResponse != nil {
  237. return r.HTTPResponse.StatusCode
  238. }
  239. return 0
  240. }
  241. type PostSessionCreateResponse struct {
  242. Body []byte
  243. HTTPResponse *http.Response
  244. JSON200 *struct {
  245. Id string `json:"id"`
  246. Title string `json:"title"`
  247. Tokens struct {
  248. Input float32 `json:"input"`
  249. Output float32 `json:"output"`
  250. Reasoning float32 `json:"reasoning"`
  251. } `json:"tokens"`
  252. }
  253. }
  254. // Status returns HTTPResponse.Status
  255. func (r PostSessionCreateResponse) Status() string {
  256. if r.HTTPResponse != nil {
  257. return r.HTTPResponse.Status
  258. }
  259. return http.StatusText(0)
  260. }
  261. // StatusCode returns HTTPResponse.StatusCode
  262. func (r PostSessionCreateResponse) StatusCode() int {
  263. if r.HTTPResponse != nil {
  264. return r.HTTPResponse.StatusCode
  265. }
  266. return 0
  267. }
  268. // PostSessionChatWithBodyWithResponse request with arbitrary body returning *PostSessionChatResponse
  269. func (c *ClientWithResponses) PostSessionChatWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*PostSessionChatResponse, error) {
  270. rsp, err := c.PostSessionChatWithBody(ctx, contentType, body, reqEditors...)
  271. if err != nil {
  272. return nil, err
  273. }
  274. return ParsePostSessionChatResponse(rsp)
  275. }
  276. func (c *ClientWithResponses) PostSessionChatWithResponse(ctx context.Context, body PostSessionChatJSONRequestBody, reqEditors ...RequestEditorFn) (*PostSessionChatResponse, error) {
  277. rsp, err := c.PostSessionChat(ctx, body, reqEditors...)
  278. if err != nil {
  279. return nil, err
  280. }
  281. return ParsePostSessionChatResponse(rsp)
  282. }
  283. // PostSessionCreateWithResponse request returning *PostSessionCreateResponse
  284. func (c *ClientWithResponses) PostSessionCreateWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*PostSessionCreateResponse, error) {
  285. rsp, err := c.PostSessionCreate(ctx, reqEditors...)
  286. if err != nil {
  287. return nil, err
  288. }
  289. return ParsePostSessionCreateResponse(rsp)
  290. }
  291. // ParsePostSessionChatResponse parses an HTTP response from a PostSessionChatWithResponse call
  292. func ParsePostSessionChatResponse(rsp *http.Response) (*PostSessionChatResponse, error) {
  293. bodyBytes, err := io.ReadAll(rsp.Body)
  294. defer func() { _ = rsp.Body.Close() }()
  295. if err != nil {
  296. return nil, err
  297. }
  298. response := &PostSessionChatResponse{
  299. Body: bodyBytes,
  300. HTTPResponse: rsp,
  301. }
  302. return response, nil
  303. }
  304. // ParsePostSessionCreateResponse parses an HTTP response from a PostSessionCreateWithResponse call
  305. func ParsePostSessionCreateResponse(rsp *http.Response) (*PostSessionCreateResponse, error) {
  306. bodyBytes, err := io.ReadAll(rsp.Body)
  307. defer func() { _ = rsp.Body.Close() }()
  308. if err != nil {
  309. return nil, err
  310. }
  311. response := &PostSessionCreateResponse{
  312. Body: bodyBytes,
  313. HTTPResponse: rsp,
  314. }
  315. switch {
  316. case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200:
  317. var dest struct {
  318. Id string `json:"id"`
  319. Title string `json:"title"`
  320. Tokens struct {
  321. Input float32 `json:"input"`
  322. Output float32 `json:"output"`
  323. Reasoning float32 `json:"reasoning"`
  324. } `json:"tokens"`
  325. }
  326. if err := json.Unmarshal(bodyBytes, &dest); err != nil {
  327. return nil, err
  328. }
  329. response.JSON200 = &dest
  330. }
  331. return response, nil
  332. }