client.go 5.5 KB

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