|
| 1 | +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | + |
| 3 | +package kernel |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | + "net/url" |
| 11 | + "slices" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/kernel/kernel-go-sdk/internal/apijson" |
| 15 | + "github.com/kernel/kernel-go-sdk/internal/apiquery" |
| 16 | + shimjson "github.com/kernel/kernel-go-sdk/internal/encoding/json" |
| 17 | + "github.com/kernel/kernel-go-sdk/internal/requestconfig" |
| 18 | + "github.com/kernel/kernel-go-sdk/option" |
| 19 | + "github.com/kernel/kernel-go-sdk/packages/pagination" |
| 20 | + "github.com/kernel/kernel-go-sdk/packages/param" |
| 21 | + "github.com/kernel/kernel-go-sdk/packages/respjson" |
| 22 | +) |
| 23 | + |
| 24 | +// Create and manage projects for resource isolation within an organization. |
| 25 | +// |
| 26 | +// ProjectService contains methods and other services that help with interacting |
| 27 | +// with the kernel API. |
| 28 | +// |
| 29 | +// Note, unlike clients, this service does not read variables from the environment |
| 30 | +// automatically. You should not instantiate this service directly, and instead use |
| 31 | +// the [NewProjectService] method instead. |
| 32 | +type ProjectService struct { |
| 33 | + Options []option.RequestOption |
| 34 | + // Create and manage projects for resource isolation within an organization. |
| 35 | + Limits ProjectLimitService |
| 36 | +} |
| 37 | + |
| 38 | +// NewProjectService generates a new service that applies the given options to each |
| 39 | +// request. These options are applied after the parent client's options (if there |
| 40 | +// is one), and before any request-specific options. |
| 41 | +func NewProjectService(opts ...option.RequestOption) (r ProjectService) { |
| 42 | + r = ProjectService{} |
| 43 | + r.Options = opts |
| 44 | + r.Limits = NewProjectLimitService(opts...) |
| 45 | + return |
| 46 | +} |
| 47 | + |
| 48 | +// Create a new project within the authenticated organization. Requires a paid plan |
| 49 | +// and the projects feature flag. |
| 50 | +func (r *ProjectService) New(ctx context.Context, body ProjectNewParams, opts ...option.RequestOption) (res *Project, err error) { |
| 51 | + opts = slices.Concat(r.Options, opts) |
| 52 | + path := "projects" |
| 53 | + err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...) |
| 54 | + return res, err |
| 55 | +} |
| 56 | + |
| 57 | +// Get a project by ID. |
| 58 | +func (r *ProjectService) Get(ctx context.Context, id string, opts ...option.RequestOption) (res *Project, err error) { |
| 59 | + opts = slices.Concat(r.Options, opts) |
| 60 | + if id == "" { |
| 61 | + err = errors.New("missing required id parameter") |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + path := fmt.Sprintf("projects/%s", id) |
| 65 | + err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...) |
| 66 | + return res, err |
| 67 | +} |
| 68 | + |
| 69 | +// Update a project's name or status. |
| 70 | +func (r *ProjectService) Update(ctx context.Context, id string, body ProjectUpdateParams, opts ...option.RequestOption) (res *Project, err error) { |
| 71 | + opts = slices.Concat(r.Options, opts) |
| 72 | + if id == "" { |
| 73 | + err = errors.New("missing required id parameter") |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + path := fmt.Sprintf("projects/%s", id) |
| 77 | + err = requestconfig.ExecuteNewRequest(ctx, http.MethodPatch, path, body, &res, opts...) |
| 78 | + return res, err |
| 79 | +} |
| 80 | + |
| 81 | +// List projects for the authenticated organization. |
| 82 | +func (r *ProjectService) List(ctx context.Context, query ProjectListParams, opts ...option.RequestOption) (res *pagination.OffsetPagination[Project], err error) { |
| 83 | + var raw *http.Response |
| 84 | + opts = slices.Concat(r.Options, opts) |
| 85 | + opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...) |
| 86 | + path := "projects" |
| 87 | + cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, query, &res, opts...) |
| 88 | + if err != nil { |
| 89 | + return nil, err |
| 90 | + } |
| 91 | + err = cfg.Execute() |
| 92 | + if err != nil { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + res.SetPageConfig(cfg, raw) |
| 96 | + return res, nil |
| 97 | +} |
| 98 | + |
| 99 | +// List projects for the authenticated organization. |
| 100 | +func (r *ProjectService) ListAutoPaging(ctx context.Context, query ProjectListParams, opts ...option.RequestOption) *pagination.OffsetPaginationAutoPager[Project] { |
| 101 | + return pagination.NewOffsetPaginationAutoPager(r.List(ctx, query, opts...)) |
| 102 | +} |
| 103 | + |
| 104 | +// Soft-delete a project. The project must be empty (no active resources). |
| 105 | +func (r *ProjectService) Delete(ctx context.Context, id string, opts ...option.RequestOption) (err error) { |
| 106 | + opts = slices.Concat(r.Options, opts) |
| 107 | + opts = append([]option.RequestOption{option.WithHeader("Accept", "*/*")}, opts...) |
| 108 | + if id == "" { |
| 109 | + err = errors.New("missing required id parameter") |
| 110 | + return err |
| 111 | + } |
| 112 | + path := fmt.Sprintf("projects/%s", id) |
| 113 | + err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, nil, nil, opts...) |
| 114 | + return err |
| 115 | +} |
| 116 | + |
| 117 | +// The property Name is required. |
| 118 | +type CreateProjectRequestParam struct { |
| 119 | + // Project name (1-255 characters) |
| 120 | + Name string `json:"name" api:"required"` |
| 121 | + paramObj |
| 122 | +} |
| 123 | + |
| 124 | +func (r CreateProjectRequestParam) MarshalJSON() (data []byte, err error) { |
| 125 | + type shadow CreateProjectRequestParam |
| 126 | + return param.MarshalObject(r, (*shadow)(&r)) |
| 127 | +} |
| 128 | +func (r *CreateProjectRequestParam) UnmarshalJSON(data []byte) error { |
| 129 | + return apijson.UnmarshalRoot(data, r) |
| 130 | +} |
| 131 | + |
| 132 | +type Project struct { |
| 133 | + // Unique project identifier |
| 134 | + ID string `json:"id" api:"required"` |
| 135 | + // When the project was created |
| 136 | + CreatedAt time.Time `json:"created_at" api:"required" format:"date-time"` |
| 137 | + // Project name |
| 138 | + Name string `json:"name" api:"required"` |
| 139 | + // Project status |
| 140 | + // |
| 141 | + // Any of "active", "archived". |
| 142 | + Status ProjectStatus `json:"status" api:"required"` |
| 143 | + // When the project was last updated |
| 144 | + UpdatedAt time.Time `json:"updated_at" api:"required" format:"date-time"` |
| 145 | + // JSON contains metadata for fields, check presence with [respjson.Field.Valid]. |
| 146 | + JSON struct { |
| 147 | + ID respjson.Field |
| 148 | + CreatedAt respjson.Field |
| 149 | + Name respjson.Field |
| 150 | + Status respjson.Field |
| 151 | + UpdatedAt respjson.Field |
| 152 | + ExtraFields map[string]respjson.Field |
| 153 | + raw string |
| 154 | + } `json:"-"` |
| 155 | +} |
| 156 | + |
| 157 | +// Returns the unmodified JSON received from the API |
| 158 | +func (r Project) RawJSON() string { return r.JSON.raw } |
| 159 | +func (r *Project) UnmarshalJSON(data []byte) error { |
| 160 | + return apijson.UnmarshalRoot(data, r) |
| 161 | +} |
| 162 | + |
| 163 | +// Project status |
| 164 | +type ProjectStatus string |
| 165 | + |
| 166 | +const ( |
| 167 | + ProjectStatusActive ProjectStatus = "active" |
| 168 | + ProjectStatusArchived ProjectStatus = "archived" |
| 169 | +) |
| 170 | + |
| 171 | +type UpdateProjectRequestParam struct { |
| 172 | + // New project name |
| 173 | + Name param.Opt[string] `json:"name,omitzero"` |
| 174 | + // New project status |
| 175 | + // |
| 176 | + // Any of "active", "archived". |
| 177 | + Status UpdateProjectRequestStatus `json:"status,omitzero"` |
| 178 | + paramObj |
| 179 | +} |
| 180 | + |
| 181 | +func (r UpdateProjectRequestParam) MarshalJSON() (data []byte, err error) { |
| 182 | + type shadow UpdateProjectRequestParam |
| 183 | + return param.MarshalObject(r, (*shadow)(&r)) |
| 184 | +} |
| 185 | +func (r *UpdateProjectRequestParam) UnmarshalJSON(data []byte) error { |
| 186 | + return apijson.UnmarshalRoot(data, r) |
| 187 | +} |
| 188 | + |
| 189 | +// New project status |
| 190 | +type UpdateProjectRequestStatus string |
| 191 | + |
| 192 | +const ( |
| 193 | + UpdateProjectRequestStatusActive UpdateProjectRequestStatus = "active" |
| 194 | + UpdateProjectRequestStatusArchived UpdateProjectRequestStatus = "archived" |
| 195 | +) |
| 196 | + |
| 197 | +type ProjectNewParams struct { |
| 198 | + CreateProjectRequest CreateProjectRequestParam |
| 199 | + paramObj |
| 200 | +} |
| 201 | + |
| 202 | +func (r ProjectNewParams) MarshalJSON() (data []byte, err error) { |
| 203 | + return shimjson.Marshal(r.CreateProjectRequest) |
| 204 | +} |
| 205 | +func (r *ProjectNewParams) UnmarshalJSON(data []byte) error { |
| 206 | + return apijson.UnmarshalRoot(data, r) |
| 207 | +} |
| 208 | + |
| 209 | +type ProjectUpdateParams struct { |
| 210 | + UpdateProjectRequest UpdateProjectRequestParam |
| 211 | + paramObj |
| 212 | +} |
| 213 | + |
| 214 | +func (r ProjectUpdateParams) MarshalJSON() (data []byte, err error) { |
| 215 | + return shimjson.Marshal(r.UpdateProjectRequest) |
| 216 | +} |
| 217 | +func (r *ProjectUpdateParams) UnmarshalJSON(data []byte) error { |
| 218 | + return apijson.UnmarshalRoot(data, r) |
| 219 | +} |
| 220 | + |
| 221 | +type ProjectListParams struct { |
| 222 | + // Maximum number of results to return |
| 223 | + Limit param.Opt[int64] `query:"limit,omitzero" json:"-"` |
| 224 | + // Number of results to skip |
| 225 | + Offset param.Opt[int64] `query:"offset,omitzero" json:"-"` |
| 226 | + paramObj |
| 227 | +} |
| 228 | + |
| 229 | +// URLQuery serializes [ProjectListParams]'s query parameters as `url.Values`. |
| 230 | +func (r ProjectListParams) URLQuery() (v url.Values, err error) { |
| 231 | + return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{ |
| 232 | + ArrayFormat: apiquery.ArrayQueryFormatComma, |
| 233 | + NestedFormat: apiquery.NestedQueryFormatBrackets, |
| 234 | + }) |
| 235 | +} |
0 commit comments