client.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2. package opencode
  3. import (
  4. "context"
  5. "net/http"
  6. "os"
  7. "slices"
  8. "github.com/sst/opencode-sdk-go/internal/requestconfig"
  9. "github.com/sst/opencode-sdk-go/option"
  10. )
  11. // Client creates a struct with services and top level methods that help with
  12. // interacting with the opencode API. You should not instantiate this client
  13. // directly, and instead use the [NewClient] method instead.
  14. type Client struct {
  15. Options []option.RequestOption
  16. Event *EventService
  17. Path *PathService
  18. App *AppService
  19. Agent *AgentService
  20. Find *FindService
  21. File *FileService
  22. Config *ConfigService
  23. Command *CommandService
  24. Project *ProjectService
  25. Session *SessionService
  26. Tui *TuiService
  27. }
  28. // DefaultClientOptions read from the environment (OPENCODE_BASE_URL). This should
  29. // be used to initialize new clients.
  30. func DefaultClientOptions() []option.RequestOption {
  31. defaults := []option.RequestOption{option.WithEnvironmentProduction()}
  32. if o, ok := os.LookupEnv("OPENCODE_BASE_URL"); ok {
  33. defaults = append(defaults, option.WithBaseURL(o))
  34. }
  35. return defaults
  36. }
  37. // NewClient generates a new client with the default option read from the
  38. // environment (OPENCODE_BASE_URL). The option passed in as arguments are applied
  39. // after these default arguments, and all option will be passed down to the
  40. // services and requests that this client makes.
  41. func NewClient(opts ...option.RequestOption) (r *Client) {
  42. opts = append(DefaultClientOptions(), opts...)
  43. r = &Client{Options: opts}
  44. r.Event = NewEventService(opts...)
  45. r.Path = NewPathService(opts...)
  46. r.App = NewAppService(opts...)
  47. r.Agent = NewAgentService(opts...)
  48. r.Find = NewFindService(opts...)
  49. r.File = NewFileService(opts...)
  50. r.Config = NewConfigService(opts...)
  51. r.Command = NewCommandService(opts...)
  52. r.Project = NewProjectService(opts...)
  53. r.Session = NewSessionService(opts...)
  54. r.Tui = NewTuiService(opts...)
  55. return
  56. }
  57. // Execute makes a request with the given context, method, URL, request params,
  58. // response, and request options. This is useful for hitting undocumented endpoints
  59. // while retaining the base URL, auth, retries, and other options from the client.
  60. //
  61. // If a byte slice or an [io.Reader] is supplied to params, it will be used as-is
  62. // for the request body.
  63. //
  64. // The params is by default serialized into the body using [encoding/json]. If your
  65. // type implements a MarshalJSON function, it will be used instead to serialize the
  66. // request. If a URLQuery method is implemented, the returned [url.Values] will be
  67. // used as query strings to the url.
  68. //
  69. // If your params struct uses [param.Field], you must provide either [MarshalJSON],
  70. // [URLQuery], and/or [MarshalForm] functions. It is undefined behavior to use a
  71. // struct uses [param.Field] without specifying how it is serialized.
  72. //
  73. // Any "…Params" object defined in this library can be used as the request
  74. // argument. Note that 'path' arguments will not be forwarded into the url.
  75. //
  76. // The response body will be deserialized into the res variable, depending on its
  77. // type:
  78. //
  79. // - A pointer to a [*http.Response] is populated by the raw response.
  80. // - A pointer to a byte array will be populated with the contents of the request
  81. // body.
  82. // - A pointer to any other type uses this library's default JSON decoding, which
  83. // respects UnmarshalJSON if it is defined on the type.
  84. // - A nil value will not read the response body.
  85. //
  86. // For even greater flexibility, see [option.WithResponseInto] and
  87. // [option.WithResponseBodyInto].
  88. func (r *Client) Execute(ctx context.Context, method string, path string, params interface{}, res interface{}, opts ...option.RequestOption) error {
  89. opts = slices.Concat(r.Options, opts)
  90. return requestconfig.ExecuteNewRequest(ctx, method, path, params, res, opts...)
  91. }
  92. // Get makes a GET request with the given URL, params, and optionally deserializes
  93. // to a response. See [Execute] documentation on the params and response.
  94. func (r *Client) Get(ctx context.Context, path string, params interface{}, res interface{}, opts ...option.RequestOption) error {
  95. return r.Execute(ctx, http.MethodGet, path, params, res, opts...)
  96. }
  97. // Post makes a POST request with the given URL, params, and optionally
  98. // deserializes to a response. See [Execute] documentation on the params and
  99. // response.
  100. func (r *Client) Post(ctx context.Context, path string, params interface{}, res interface{}, opts ...option.RequestOption) error {
  101. return r.Execute(ctx, http.MethodPost, path, params, res, opts...)
  102. }
  103. // Put makes a PUT request with the given URL, params, and optionally deserializes
  104. // to a response. See [Execute] documentation on the params and response.
  105. func (r *Client) Put(ctx context.Context, path string, params interface{}, res interface{}, opts ...option.RequestOption) error {
  106. return r.Execute(ctx, http.MethodPut, path, params, res, opts...)
  107. }
  108. // Patch makes a PATCH request with the given URL, params, and optionally
  109. // deserializes to a response. See [Execute] documentation on the params and
  110. // response.
  111. func (r *Client) Patch(ctx context.Context, path string, params interface{}, res interface{}, opts ...option.RequestOption) error {
  112. return r.Execute(ctx, http.MethodPatch, path, params, res, opts...)
  113. }
  114. // Delete makes a DELETE request with the given URL, params, and optionally
  115. // deserializes to a response. See [Execute] documentation on the params and
  116. // response.
  117. func (r *Client) Delete(ctx context.Context, path string, params interface{}, res interface{}, opts ...option.RequestOption) error {
  118. return r.Execute(ctx, http.MethodDelete, path, params, res, opts...)
  119. }