client.go 5.2 KB

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