/
project.go
201 lines (178 loc) · 7.92 KB
/
project.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// File generated from our OpenAPI spec by Stainless.
package braintrust
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
"github.com/braintrustdata/braintrust-go/internal/apijson"
"github.com/braintrustdata/braintrust-go/internal/apiquery"
"github.com/braintrustdata/braintrust-go/internal/param"
"github.com/braintrustdata/braintrust-go/internal/requestconfig"
"github.com/braintrustdata/braintrust-go/internal/shared"
"github.com/braintrustdata/braintrust-go/option"
)
// ProjectService contains methods and other services that help with interacting
// with the braintrust API. Note, unlike clients, this service does not read
// variables from the environment automatically. You should not instantiate this
// service directly, and instead use the [NewProjectService] method instead.
type ProjectService struct {
Options []option.RequestOption
Logs *ProjectLogService
}
// NewProjectService generates a new service that applies the given options to each
// request. These options are applied after the parent client's options (if there
// is one), and before any request-specific options.
func NewProjectService(opts ...option.RequestOption) (r *ProjectService) {
r = &ProjectService{}
r.Options = opts
r.Logs = NewProjectLogService(opts...)
return
}
// Create a new project. If there is an existing project with the same name as the
// one specified in the request, will return the existing project unmodified
func (r *ProjectService) New(ctx context.Context, body ProjectNewParams, opts ...option.RequestOption) (res *Project, err error) {
opts = append(r.Options[:], opts...)
path := "v1/project"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// Get a project object by its id
func (r *ProjectService) Get(ctx context.Context, projectID string, opts ...option.RequestOption) (res *Project, err error) {
opts = append(r.Options[:], opts...)
path := fmt.Sprintf("v1/project/%s", projectID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return
}
// Partially update a project object. Specify the fields to update in the payload.
// Any object-type fields will be deep-merged with existing content. Currently we
// do not support removing fields or setting them to null. As a workaround, you may
// retrieve the full object with `GET /project/{id}` and then replace it with
// `PUT /project`.
func (r *ProjectService) Update(ctx context.Context, projectID string, body ProjectUpdateParams, opts ...option.RequestOption) (res *Project, err error) {
opts = append(r.Options[:], opts...)
path := fmt.Sprintf("v1/project/%s", projectID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPatch, path, body, &res, opts...)
return
}
// List out all projects. The projects are sorted by creation date, with the most
// recently-created projects coming first
func (r *ProjectService) List(ctx context.Context, query ProjectListParams, opts ...option.RequestOption) (res *shared.ListObjects[Project], err error) {
var raw *http.Response
opts = append(r.Options, opts...)
opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
path := "v1/project"
cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, query, &res, opts...)
if err != nil {
return nil, err
}
err = cfg.Execute()
if err != nil {
return nil, err
}
res.SetPageConfig(cfg, raw)
return res, nil
}
// List out all projects. The projects are sorted by creation date, with the most
// recently-created projects coming first
func (r *ProjectService) ListAutoPaging(ctx context.Context, query ProjectListParams, opts ...option.RequestOption) *shared.ListObjectsAutoPager[Project] {
return shared.NewListObjectsAutoPager(r.List(ctx, query, opts...))
}
// Delete a project object by its id
func (r *ProjectService) Delete(ctx context.Context, projectID string, opts ...option.RequestOption) (res *Project, err error) {
opts = append(r.Options[:], opts...)
path := fmt.Sprintf("v1/project/%s", projectID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, nil, &res, opts...)
return
}
// Create or replace a new project. If there is an existing project with the same
// name as the one specified in the request, will replace the existing project with
// the provided fields
func (r *ProjectService) Replace(ctx context.Context, body ProjectReplaceParams, opts ...option.RequestOption) (res *Project, err error) {
opts = append(r.Options[:], opts...)
path := "v1/project"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPut, path, body, &res, opts...)
return
}
type Project struct {
// Unique identifier for the project
ID string `json:"id,required" format:"uuid"`
// Name of the project
Name string `json:"name,required"`
// Unique id for the organization that the project belongs under
OrgID string `json:"org_id,required" format:"uuid"`
// Date of project creation
Created time.Time `json:"created,nullable" format:"date-time"`
// Date of project deletion, or null if the project is still active
DeletedAt time.Time `json:"deleted_at,nullable" format:"date-time"`
// Identifies the user who created the project
UserID string `json:"user_id,nullable" format:"uuid"`
JSON projectJSON `json:"-"`
}
// projectJSON contains the JSON metadata for the struct [Project]
type projectJSON struct {
ID apijson.Field
Name apijson.Field
OrgID apijson.Field
Created apijson.Field
DeletedAt apijson.Field
UserID apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *Project) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
type ProjectNewParams struct {
// Name of the project
Name param.Field[string] `json:"name,required"`
// For nearly all users, this parameter should be unnecessary. But in the rare case
// that your API key belongs to multiple organizations, you may specify the name of
// the organization the project belongs in.
OrgName param.Field[string] `json:"org_name"`
}
func (r ProjectNewParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type ProjectUpdateParams struct {
// Name of the project
Name param.Field[string] `json:"name"`
}
func (r ProjectUpdateParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type ProjectListParams struct {
// A cursor for pagination. For example, if the initial item in the last page you
// fetched had an id of `foo`, pass `ending_before=foo` to fetch the previous page.
// Note: you may only pass one of `starting_after` and `ending_before`
EndingBefore param.Field[string] `query:"ending_before" format:"uuid"`
// Limit the number of objects to return
Limit param.Field[int64] `query:"limit"`
// Filter search results to within a particular organization
OrgName param.Field[string] `query:"org_name"`
// Name of the project to search for
ProjectName param.Field[string] `query:"project_name"`
// A cursor for pagination. For example, if the final item in the last page you
// fetched had an id of `foo`, pass `starting_after=foo` to fetch the next page.
// Note: you may only pass one of `starting_after` and `ending_before`
StartingAfter param.Field[string] `query:"starting_after" format:"uuid"`
}
// URLQuery serializes [ProjectListParams]'s query parameters as `url.Values`.
func (r ProjectListParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatComma,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}
type ProjectReplaceParams struct {
// Name of the project
Name param.Field[string] `json:"name,required"`
// For nearly all users, this parameter should be unnecessary. But in the rare case
// that your API key belongs to multiple organizations, you may specify the name of
// the organization the project belongs in.
OrgName param.Field[string] `json:"org_name"`
}
func (r ProjectReplaceParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}