file.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. "net/url"
  7. "github.com/sst/opencode-sdk-go/internal/apijson"
  8. "github.com/sst/opencode-sdk-go/internal/apiquery"
  9. "github.com/sst/opencode-sdk-go/internal/param"
  10. "github.com/sst/opencode-sdk-go/internal/requestconfig"
  11. "github.com/sst/opencode-sdk-go/option"
  12. )
  13. // FileService contains methods and other services that help with interacting with
  14. // the opencode API.
  15. //
  16. // Note, unlike clients, this service does not read variables from the environment
  17. // automatically. You should not instantiate this service directly, and instead use
  18. // the [NewFileService] method instead.
  19. type FileService struct {
  20. Options []option.RequestOption
  21. }
  22. // NewFileService generates a new service that applies the given options to each
  23. // request. These options are applied after the parent client's options (if there
  24. // is one), and before any request-specific options.
  25. func NewFileService(opts ...option.RequestOption) (r *FileService) {
  26. r = &FileService{}
  27. r.Options = opts
  28. return
  29. }
  30. // List files and directories
  31. func (r *FileService) List(ctx context.Context, query FileListParams, opts ...option.RequestOption) (res *[]FileNode, err error) {
  32. opts = append(r.Options[:], opts...)
  33. path := "file"
  34. err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, query, &res, opts...)
  35. return
  36. }
  37. // Read a file
  38. func (r *FileService) Read(ctx context.Context, query FileReadParams, opts ...option.RequestOption) (res *FileReadResponse, err error) {
  39. opts = append(r.Options[:], opts...)
  40. path := "file/content"
  41. err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, query, &res, opts...)
  42. return
  43. }
  44. // Get file status
  45. func (r *FileService) Status(ctx context.Context, query FileStatusParams, opts ...option.RequestOption) (res *[]File, err error) {
  46. opts = append(r.Options[:], opts...)
  47. path := "file/status"
  48. err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, query, &res, opts...)
  49. return
  50. }
  51. type File struct {
  52. Added int64 `json:"added,required"`
  53. Path string `json:"path,required"`
  54. Removed int64 `json:"removed,required"`
  55. Status FileStatus `json:"status,required"`
  56. JSON fileJSON `json:"-"`
  57. }
  58. // fileJSON contains the JSON metadata for the struct [File]
  59. type fileJSON struct {
  60. Added apijson.Field
  61. Path apijson.Field
  62. Removed apijson.Field
  63. Status apijson.Field
  64. raw string
  65. ExtraFields map[string]apijson.Field
  66. }
  67. func (r *File) UnmarshalJSON(data []byte) (err error) {
  68. return apijson.UnmarshalRoot(data, r)
  69. }
  70. func (r fileJSON) RawJSON() string {
  71. return r.raw
  72. }
  73. type FileStatus string
  74. const (
  75. FileStatusAdded FileStatus = "added"
  76. FileStatusDeleted FileStatus = "deleted"
  77. FileStatusModified FileStatus = "modified"
  78. )
  79. func (r FileStatus) IsKnown() bool {
  80. switch r {
  81. case FileStatusAdded, FileStatusDeleted, FileStatusModified:
  82. return true
  83. }
  84. return false
  85. }
  86. type FileNode struct {
  87. Ignored bool `json:"ignored,required"`
  88. Name string `json:"name,required"`
  89. Path string `json:"path,required"`
  90. Type FileNodeType `json:"type,required"`
  91. JSON fileNodeJSON `json:"-"`
  92. }
  93. // fileNodeJSON contains the JSON metadata for the struct [FileNode]
  94. type fileNodeJSON struct {
  95. Ignored apijson.Field
  96. Name apijson.Field
  97. Path apijson.Field
  98. Type apijson.Field
  99. raw string
  100. ExtraFields map[string]apijson.Field
  101. }
  102. func (r *FileNode) UnmarshalJSON(data []byte) (err error) {
  103. return apijson.UnmarshalRoot(data, r)
  104. }
  105. func (r fileNodeJSON) RawJSON() string {
  106. return r.raw
  107. }
  108. type FileNodeType string
  109. const (
  110. FileNodeTypeFile FileNodeType = "file"
  111. FileNodeTypeDirectory FileNodeType = "directory"
  112. )
  113. func (r FileNodeType) IsKnown() bool {
  114. switch r {
  115. case FileNodeTypeFile, FileNodeTypeDirectory:
  116. return true
  117. }
  118. return false
  119. }
  120. type FileReadResponse struct {
  121. Content string `json:"content,required"`
  122. Type FileReadResponseType `json:"type,required"`
  123. JSON fileReadResponseJSON `json:"-"`
  124. }
  125. // fileReadResponseJSON contains the JSON metadata for the struct
  126. // [FileReadResponse]
  127. type fileReadResponseJSON struct {
  128. Content apijson.Field
  129. Type apijson.Field
  130. raw string
  131. ExtraFields map[string]apijson.Field
  132. }
  133. func (r *FileReadResponse) UnmarshalJSON(data []byte) (err error) {
  134. return apijson.UnmarshalRoot(data, r)
  135. }
  136. func (r fileReadResponseJSON) RawJSON() string {
  137. return r.raw
  138. }
  139. type FileReadResponseType string
  140. const (
  141. FileReadResponseTypeRaw FileReadResponseType = "raw"
  142. FileReadResponseTypePatch FileReadResponseType = "patch"
  143. )
  144. func (r FileReadResponseType) IsKnown() bool {
  145. switch r {
  146. case FileReadResponseTypeRaw, FileReadResponseTypePatch:
  147. return true
  148. }
  149. return false
  150. }
  151. type FileListParams struct {
  152. Path param.Field[string] `query:"path,required"`
  153. Directory param.Field[string] `query:"directory"`
  154. }
  155. // URLQuery serializes [FileListParams]'s query parameters as `url.Values`.
  156. func (r FileListParams) URLQuery() (v url.Values) {
  157. return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
  158. ArrayFormat: apiquery.ArrayQueryFormatComma,
  159. NestedFormat: apiquery.NestedQueryFormatBrackets,
  160. })
  161. }
  162. type FileReadParams struct {
  163. Path param.Field[string] `query:"path,required"`
  164. Directory param.Field[string] `query:"directory"`
  165. }
  166. // URLQuery serializes [FileReadParams]'s query parameters as `url.Values`.
  167. func (r FileReadParams) URLQuery() (v url.Values) {
  168. return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
  169. ArrayFormat: apiquery.ArrayQueryFormatComma,
  170. NestedFormat: apiquery.NestedQueryFormatBrackets,
  171. })
  172. }
  173. type FileStatusParams struct {
  174. Directory param.Field[string] `query:"directory"`
  175. }
  176. // URLQuery serializes [FileStatusParams]'s query parameters as `url.Values`.
  177. func (r FileStatusParams) URLQuery() (v url.Values) {
  178. return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
  179. ArrayFormat: apiquery.ArrayQueryFormatComma,
  180. NestedFormat: apiquery.NestedQueryFormatBrackets,
  181. })
  182. }