generated-client.go 11 KB

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