sessionpermission.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2. package opencode
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "net/http"
  8. "net/url"
  9. "reflect"
  10. "github.com/sst/opencode-sdk-go/internal/apijson"
  11. "github.com/sst/opencode-sdk-go/internal/apiquery"
  12. "github.com/sst/opencode-sdk-go/internal/param"
  13. "github.com/sst/opencode-sdk-go/internal/requestconfig"
  14. "github.com/sst/opencode-sdk-go/option"
  15. "github.com/sst/opencode-sdk-go/shared"
  16. "github.com/tidwall/gjson"
  17. )
  18. // SessionPermissionService contains methods and other services that help with
  19. // interacting with the opencode API.
  20. //
  21. // Note, unlike clients, this service does not read variables from the environment
  22. // automatically. You should not instantiate this service directly, and instead use
  23. // the [NewSessionPermissionService] method instead.
  24. type SessionPermissionService struct {
  25. Options []option.RequestOption
  26. }
  27. // NewSessionPermissionService generates a new service that applies the given
  28. // options to each request. These options are applied after the parent client's
  29. // options (if there is one), and before any request-specific options.
  30. func NewSessionPermissionService(opts ...option.RequestOption) (r *SessionPermissionService) {
  31. r = &SessionPermissionService{}
  32. r.Options = opts
  33. return
  34. }
  35. // Respond to a permission request
  36. func (r *SessionPermissionService) Respond(ctx context.Context, id string, permissionID string, params SessionPermissionRespondParams, opts ...option.RequestOption) (res *bool, err error) {
  37. opts = append(r.Options[:], opts...)
  38. if id == "" {
  39. err = errors.New("missing required id parameter")
  40. return
  41. }
  42. if permissionID == "" {
  43. err = errors.New("missing required permissionID parameter")
  44. return
  45. }
  46. path := fmt.Sprintf("session/%s/permissions/%s", id, permissionID)
  47. err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, params, &res, opts...)
  48. return
  49. }
  50. type Permission struct {
  51. ID string `json:"id,required"`
  52. MessageID string `json:"messageID,required"`
  53. Metadata map[string]interface{} `json:"metadata,required"`
  54. SessionID string `json:"sessionID,required"`
  55. Time PermissionTime `json:"time,required"`
  56. Title string `json:"title,required"`
  57. Type string `json:"type,required"`
  58. CallID string `json:"callID"`
  59. Pattern PermissionPatternUnion `json:"pattern"`
  60. JSON permissionJSON `json:"-"`
  61. }
  62. // permissionJSON contains the JSON metadata for the struct [Permission]
  63. type permissionJSON struct {
  64. ID apijson.Field
  65. MessageID apijson.Field
  66. Metadata apijson.Field
  67. SessionID apijson.Field
  68. Time apijson.Field
  69. Title apijson.Field
  70. Type apijson.Field
  71. CallID apijson.Field
  72. Pattern apijson.Field
  73. raw string
  74. ExtraFields map[string]apijson.Field
  75. }
  76. func (r *Permission) UnmarshalJSON(data []byte) (err error) {
  77. return apijson.UnmarshalRoot(data, r)
  78. }
  79. func (r permissionJSON) RawJSON() string {
  80. return r.raw
  81. }
  82. type PermissionTime struct {
  83. Created float64 `json:"created,required"`
  84. JSON permissionTimeJSON `json:"-"`
  85. }
  86. // permissionTimeJSON contains the JSON metadata for the struct [PermissionTime]
  87. type permissionTimeJSON struct {
  88. Created apijson.Field
  89. raw string
  90. ExtraFields map[string]apijson.Field
  91. }
  92. func (r *PermissionTime) UnmarshalJSON(data []byte) (err error) {
  93. return apijson.UnmarshalRoot(data, r)
  94. }
  95. func (r permissionTimeJSON) RawJSON() string {
  96. return r.raw
  97. }
  98. // Union satisfied by [shared.UnionString] or [PermissionPatternArray].
  99. type PermissionPatternUnion interface {
  100. ImplementsPermissionPatternUnion()
  101. }
  102. func init() {
  103. apijson.RegisterUnion(
  104. reflect.TypeOf((*PermissionPatternUnion)(nil)).Elem(),
  105. "",
  106. apijson.UnionVariant{
  107. TypeFilter: gjson.String,
  108. Type: reflect.TypeOf(shared.UnionString("")),
  109. },
  110. apijson.UnionVariant{
  111. TypeFilter: gjson.JSON,
  112. Type: reflect.TypeOf(PermissionPatternArray{}),
  113. },
  114. )
  115. }
  116. type PermissionPatternArray []string
  117. func (r PermissionPatternArray) ImplementsPermissionPatternUnion() {}
  118. type SessionPermissionRespondParams struct {
  119. Response param.Field[SessionPermissionRespondParamsResponse] `json:"response,required"`
  120. Directory param.Field[string] `query:"directory"`
  121. }
  122. func (r SessionPermissionRespondParams) MarshalJSON() (data []byte, err error) {
  123. return apijson.MarshalRoot(r)
  124. }
  125. // URLQuery serializes [SessionPermissionRespondParams]'s query parameters as
  126. // `url.Values`.
  127. func (r SessionPermissionRespondParams) URLQuery() (v url.Values) {
  128. return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
  129. ArrayFormat: apiquery.ArrayQueryFormatComma,
  130. NestedFormat: apiquery.NestedQueryFormatBrackets,
  131. })
  132. }
  133. type SessionPermissionRespondParamsResponse string
  134. const (
  135. SessionPermissionRespondParamsResponseOnce SessionPermissionRespondParamsResponse = "once"
  136. SessionPermissionRespondParamsResponseAlways SessionPermissionRespondParamsResponse = "always"
  137. SessionPermissionRespondParamsResponseReject SessionPermissionRespondParamsResponse = "reject"
  138. )
  139. func (r SessionPermissionRespondParamsResponse) IsKnown() bool {
  140. switch r {
  141. case SessionPermissionRespondParamsResponseOnce, SessionPermissionRespondParamsResponseAlways, SessionPermissionRespondParamsResponseReject:
  142. return true
  143. }
  144. return false
  145. }