client.go 5.3 KB

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