client.go 5.4 KB

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