Repository navigation
Expand file tree
/
Copy pathconnection.go
More file actions
609 lines (513 loc) · 19.3 KB
/
Copy pathconnection.go
File metadata and controls
609 lines (513 loc) · 19.3 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package cercago
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"reflect"
"slices"
"github.com/matrices/cerca-go/internal/apijson"
"github.com/matrices/cerca-go/internal/apiquery"
"github.com/matrices/cerca-go/internal/param"
"github.com/matrices/cerca-go/internal/requestconfig"
"github.com/matrices/cerca-go/option"
"github.com/matrices/cerca-go/packages/pagination"
"github.com/tidwall/gjson"
)
// ConnectionService contains methods and other services that help with interacting
// with the cerca 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 [NewConnectionService] method instead.
type ConnectionService struct {
Options []option.RequestOption
}
// NewConnectionService 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 NewConnectionService(opts ...option.RequestOption) (r *ConnectionService) {
r = &ConnectionService{}
r.Options = opts
return
}
// Create API key
func (r *ConnectionService) New(ctx context.Context, body ConnectionNewParams, opts ...option.RequestOption) (res *Connection, err error) {
opts = slices.Concat(r.Options, opts)
path := "connections/api-keys"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return res, err
}
// List connections
func (r *ConnectionService) List(ctx context.Context, query ConnectionListParams, opts ...option.RequestOption) (res *pagination.ConnectionsCursorPage[Connection], err error) {
var raw *http.Response
opts = slices.Concat(r.Options, opts)
opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
path := "connections"
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 connections
func (r *ConnectionService) ListAutoPaging(ctx context.Context, query ConnectionListParams, opts ...option.RequestOption) *pagination.ConnectionsCursorPageAutoPager[Connection] {
return pagination.NewConnectionsCursorPageAutoPager(r.List(ctx, query, opts...))
}
// Delete connection
func (r *ConnectionService) Delete(ctx context.Context, connectionID string, body ConnectionDeleteParams, opts ...option.RequestOption) (res *ConnectionDeleteResponse, err error) {
opts = slices.Concat(r.Options, opts)
if connectionID == "" {
err = errors.New("missing required connectionId parameter")
return nil, err
}
path := fmt.Sprintf("connections/%s", connectionID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, body, &res, opts...)
return res, err
}
// Attach connection
func (r *ConnectionService) Attach(ctx context.Context, agentID string, body ConnectionAttachParams, opts ...option.RequestOption) (res *AttachedConnection, err error) {
opts = slices.Concat(r.Options, opts)
if agentID == "" {
err = errors.New("missing required agentId parameter")
return nil, err
}
path := fmt.Sprintf("agents/%s/connections", agentID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return res, err
}
// Detach connection
func (r *ConnectionService) Detach(ctx context.Context, agentID string, connectionID string, opts ...option.RequestOption) (res *ConnectionDetachResponse, err error) {
opts = slices.Concat(r.Options, opts)
if agentID == "" {
err = errors.New("missing required agentId parameter")
return nil, err
}
if connectionID == "" {
err = errors.New("missing required connectionId parameter")
return nil, err
}
path := fmt.Sprintf("agents/%s/connections/%s", agentID, connectionID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, nil, &res, opts...)
return res, err
}
// List connections
func (r *ConnectionService) ListForAgent(ctx context.Context, agentID string, opts ...option.RequestOption) (res *ConnectionListForAgentResponse, err error) {
opts = slices.Concat(r.Options, opts)
if agentID == "" {
err = errors.New("missing required agentId parameter")
return nil, err
}
path := fmt.Sprintf("agents/%s/connections", agentID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return res, err
}
type AttachedConnection struct {
AttachedAt string `json:"attachedAt" api:"required"`
Metadata ToolConnectionMetadata `json:"metadata"`
JSON attachedConnectionJSON `json:"-"`
Connection
}
// attachedConnectionJSON contains the JSON metadata for the struct
// [AttachedConnection]
type attachedConnectionJSON struct {
AttachedAt apijson.Field
Metadata apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *AttachedConnection) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r attachedConnectionJSON) RawJSON() string {
return r.raw
}
type Connection struct {
ID string `json:"id" api:"required" format:"uuid"`
AccountLabel string `json:"accountLabel" api:"required,nullable"`
CreatedAt string `json:"createdAt" api:"required"`
// Public owner for a reusable connection. Organization owners use the
// authenticated organization; fleet owners add a fleetId.
Owner ConnectionOwner `json:"owner" api:"required"`
Provider CredentialProvider `json:"provider" api:"required"`
Scopes []string `json:"scopes" api:"required"`
Type CredentialType `json:"type" api:"required"`
UpdatedAt string `json:"updatedAt" api:"required"`
JSON connectionJSON `json:"-"`
}
// connectionJSON contains the JSON metadata for the struct [Connection]
type connectionJSON struct {
ID apijson.Field
AccountLabel apijson.Field
CreatedAt apijson.Field
Owner apijson.Field
Provider apijson.Field
Scopes apijson.Field
Type apijson.Field
UpdatedAt apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *Connection) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r connectionJSON) RawJSON() string {
return r.raw
}
// Public owner for a reusable connection. Organization owners use the
// authenticated organization; fleet owners add a fleetId.
type ConnectionOwner struct {
Type ConnectionOwnerType `json:"type" api:"required"`
FleetID string `json:"fleetId"`
JSON connectionOwnerJSON `json:"-"`
union ConnectionOwnerUnion
}
// connectionOwnerJSON contains the JSON metadata for the struct [ConnectionOwner]
type connectionOwnerJSON struct {
Type apijson.Field
FleetID apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r connectionOwnerJSON) RawJSON() string {
return r.raw
}
func (r *ConnectionOwner) UnmarshalJSON(data []byte) (err error) {
*r = ConnectionOwner{}
err = apijson.UnmarshalRoot(data, &r.union)
if err != nil {
return err
}
return apijson.Port(r.union, &r)
}
// AsUnion returns a [ConnectionOwnerUnion] interface which you can cast to the
// specific types for more type safety.
//
// Possible runtime types of the union are
// [ConnectionOwnerOrganizationConnectionOwner],
// [ConnectionOwnerFleetConnectionOwner].
func (r ConnectionOwner) AsUnion() ConnectionOwnerUnion {
return r.union
}
// Public owner for a reusable connection. Organization owners use the
// authenticated organization; fleet owners add a fleetId.
//
// Union satisfied by [ConnectionOwnerOrganizationConnectionOwner] or
// [ConnectionOwnerFleetConnectionOwner].
type ConnectionOwnerUnion interface {
implementsConnectionOwner()
}
func init() {
apijson.RegisterUnion(
reflect.TypeOf((*ConnectionOwnerUnion)(nil)).Elem(),
"type",
apijson.UnionVariant{
TypeFilter: gjson.JSON,
Type: reflect.TypeOf(ConnectionOwnerOrganizationConnectionOwner{}),
DiscriminatorValue: "organization",
},
apijson.UnionVariant{
TypeFilter: gjson.JSON,
Type: reflect.TypeOf(ConnectionOwnerFleetConnectionOwner{}),
DiscriminatorValue: "fleet",
},
)
}
type ConnectionOwnerOrganizationConnectionOwner struct {
Type ConnectionOwnerOrganizationConnectionOwnerType `json:"type" api:"required"`
JSON connectionOwnerOrganizationConnectionOwnerJSON `json:"-"`
}
// connectionOwnerOrganizationConnectionOwnerJSON contains the JSON metadata for
// the struct [ConnectionOwnerOrganizationConnectionOwner]
type connectionOwnerOrganizationConnectionOwnerJSON struct {
Type apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *ConnectionOwnerOrganizationConnectionOwner) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r connectionOwnerOrganizationConnectionOwnerJSON) RawJSON() string {
return r.raw
}
func (r ConnectionOwnerOrganizationConnectionOwner) implementsConnectionOwner() {}
type ConnectionOwnerOrganizationConnectionOwnerType string
const (
ConnectionOwnerOrganizationConnectionOwnerTypeOrganization ConnectionOwnerOrganizationConnectionOwnerType = "organization"
)
func (r ConnectionOwnerOrganizationConnectionOwnerType) IsKnown() bool {
switch r {
case ConnectionOwnerOrganizationConnectionOwnerTypeOrganization:
return true
}
return false
}
type ConnectionOwnerFleetConnectionOwner struct {
FleetID string `json:"fleetId" api:"required"`
Type ConnectionOwnerFleetConnectionOwnerType `json:"type" api:"required"`
JSON connectionOwnerFleetConnectionOwnerJSON `json:"-"`
}
// connectionOwnerFleetConnectionOwnerJSON contains the JSON metadata for the
// struct [ConnectionOwnerFleetConnectionOwner]
type connectionOwnerFleetConnectionOwnerJSON struct {
FleetID apijson.Field
Type apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *ConnectionOwnerFleetConnectionOwner) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r connectionOwnerFleetConnectionOwnerJSON) RawJSON() string {
return r.raw
}
func (r ConnectionOwnerFleetConnectionOwner) implementsConnectionOwner() {}
type ConnectionOwnerFleetConnectionOwnerType string
const (
ConnectionOwnerFleetConnectionOwnerTypeFleet ConnectionOwnerFleetConnectionOwnerType = "fleet"
)
func (r ConnectionOwnerFleetConnectionOwnerType) IsKnown() bool {
switch r {
case ConnectionOwnerFleetConnectionOwnerTypeFleet:
return true
}
return false
}
type ConnectionOwnerType string
const (
ConnectionOwnerTypeOrganization ConnectionOwnerType = "organization"
ConnectionOwnerTypeFleet ConnectionOwnerType = "fleet"
)
func (r ConnectionOwnerType) IsKnown() bool {
switch r {
case ConnectionOwnerTypeOrganization, ConnectionOwnerTypeFleet:
return true
}
return false
}
// Public owner for a reusable connection. Organization owners use the
// authenticated organization; fleet owners add a fleetId.
type ConnectionOwnerParam struct {
Type param.Field[ConnectionOwnerType] `json:"type" api:"required"`
FleetID param.Field[string] `json:"fleetId"`
}
func (r ConnectionOwnerParam) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
func (r ConnectionOwnerParam) implementsConnectionOwnerUnionParam() {}
// Public owner for a reusable connection. Organization owners use the
// authenticated organization; fleet owners add a fleetId.
//
// Satisfied by [ConnectionOwnerOrganizationConnectionOwnerParam],
// [ConnectionOwnerFleetConnectionOwnerParam], [ConnectionOwnerParam].
type ConnectionOwnerUnionParam interface {
implementsConnectionOwnerUnionParam()
}
type ConnectionOwnerOrganizationConnectionOwnerParam struct {
Type param.Field[ConnectionOwnerOrganizationConnectionOwnerType] `json:"type" api:"required"`
}
func (r ConnectionOwnerOrganizationConnectionOwnerParam) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
func (r ConnectionOwnerOrganizationConnectionOwnerParam) implementsConnectionOwnerUnionParam() {}
type ConnectionOwnerFleetConnectionOwnerParam struct {
FleetID param.Field[string] `json:"fleetId" api:"required"`
Type param.Field[ConnectionOwnerFleetConnectionOwnerType] `json:"type" api:"required"`
}
func (r ConnectionOwnerFleetConnectionOwnerParam) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
func (r ConnectionOwnerFleetConnectionOwnerParam) implementsConnectionOwnerUnionParam() {}
type CredentialProvider string
const (
CredentialProviderGoogle CredentialProvider = "google"
CredentialProviderGitHub CredentialProvider = "github"
CredentialProviderSlack CredentialProvider = "slack"
CredentialProviderLinear CredentialProvider = "linear"
CredentialProviderNotion CredentialProvider = "notion"
CredentialProviderCustom CredentialProvider = "custom"
CredentialProviderWebhook CredentialProvider = "webhook"
)
func (r CredentialProvider) IsKnown() bool {
switch r {
case CredentialProviderGoogle, CredentialProviderGitHub, CredentialProviderSlack, CredentialProviderLinear, CredentialProviderNotion, CredentialProviderCustom, CredentialProviderWebhook:
return true
}
return false
}
type CredentialType string
const (
CredentialTypeOAuth CredentialType = "oauth"
CredentialTypeAPIKey CredentialType = "api_key"
)
func (r CredentialType) IsKnown() bool {
switch r {
case CredentialTypeOAuth, CredentialTypeAPIKey:
return true
}
return false
}
type ToolConnectionMetadata map[string]string
type ToolConnectionMetadataParam map[string]string
type ConnectionDeleteResponse struct {
Success ConnectionDeleteResponseSuccess `json:"success" api:"required"`
JSON connectionDeleteResponseJSON `json:"-"`
}
// connectionDeleteResponseJSON contains the JSON metadata for the struct
// [ConnectionDeleteResponse]
type connectionDeleteResponseJSON struct {
Success apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *ConnectionDeleteResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r connectionDeleteResponseJSON) RawJSON() string {
return r.raw
}
type ConnectionDeleteResponseSuccess bool
const (
ConnectionDeleteResponseSuccessTrue ConnectionDeleteResponseSuccess = true
)
func (r ConnectionDeleteResponseSuccess) IsKnown() bool {
switch r {
case ConnectionDeleteResponseSuccessTrue:
return true
}
return false
}
type ConnectionDetachResponse struct {
Success ConnectionDetachResponseSuccess `json:"success" api:"required"`
JSON connectionDetachResponseJSON `json:"-"`
}
// connectionDetachResponseJSON contains the JSON metadata for the struct
// [ConnectionDetachResponse]
type connectionDetachResponseJSON struct {
Success apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *ConnectionDetachResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r connectionDetachResponseJSON) RawJSON() string {
return r.raw
}
type ConnectionDetachResponseSuccess bool
const (
ConnectionDetachResponseSuccessTrue ConnectionDetachResponseSuccess = true
)
func (r ConnectionDetachResponseSuccess) IsKnown() bool {
switch r {
case ConnectionDetachResponseSuccessTrue:
return true
}
return false
}
type ConnectionListForAgentResponse struct {
Connections []AttachedConnection `json:"connections" api:"required"`
Cursor string `json:"cursor" api:"required,nullable"`
HasMore bool `json:"hasMore" api:"required"`
JSON connectionListForAgentResponseJSON `json:"-"`
}
// connectionListForAgentResponseJSON contains the JSON metadata for the struct
// [ConnectionListForAgentResponse]
type connectionListForAgentResponseJSON struct {
Connections apijson.Field
Cursor apijson.Field
HasMore apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *ConnectionListForAgentResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r connectionListForAgentResponseJSON) RawJSON() string {
return r.raw
}
type ConnectionNewParams struct {
// API key secret. It is stored securely and is not returned.
APIKey param.Field[string] `json:"apiKey" api:"required"`
// Public owner for a reusable connection. Organization owners use the
// authenticated organization; fleet owners add a fleetId.
Owner param.Field[ConnectionOwnerUnionParam] `json:"owner" api:"required"`
// Credential provider to store an API key for.
Provider param.Field[string] `json:"provider" api:"required"`
// Optional human-readable account label.
AccountLabel param.Field[string] `json:"accountLabel"`
}
func (r ConnectionNewParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type ConnectionListParams struct {
// Public connection owner type.
OwnerType param.Field[ConnectionListParamsOwnerType] `query:"ownerType" api:"required"`
// Opaque pagination cursor returned by a previous request.
Cursor param.Field[string] `query:"cursor"`
// Required when ownerType is fleet.
FleetID param.Field[string] `query:"fleetId"`
// Maximum number of items to return. Defaults to 20 and preserves parseInt
// semantics.
Limit param.Field[string] `query:"limit"`
}
// URLQuery serializes [ConnectionListParams]'s query parameters as `url.Values`.
func (r ConnectionListParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatComma,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}
// Public connection owner type.
type ConnectionListParamsOwnerType string
const (
ConnectionListParamsOwnerTypeOrganization ConnectionListParamsOwnerType = "organization"
ConnectionListParamsOwnerTypeFleet ConnectionListParamsOwnerType = "fleet"
)
func (r ConnectionListParamsOwnerType) IsKnown() bool {
switch r {
case ConnectionListParamsOwnerTypeOrganization, ConnectionListParamsOwnerTypeFleet:
return true
}
return false
}
type ConnectionDeleteParams struct {
// Public connection owner type.
OwnerType param.Field[ConnectionDeleteParamsOwnerType] `query:"ownerType" api:"required"`
// Required when ownerType is fleet.
FleetID param.Field[string] `query:"fleetId"`
}
// URLQuery serializes [ConnectionDeleteParams]'s query parameters as `url.Values`.
func (r ConnectionDeleteParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatComma,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}
// Public connection owner type.
type ConnectionDeleteParamsOwnerType string
const (
ConnectionDeleteParamsOwnerTypeOrganization ConnectionDeleteParamsOwnerType = "organization"
ConnectionDeleteParamsOwnerTypeFleet ConnectionDeleteParamsOwnerType = "fleet"
)
func (r ConnectionDeleteParamsOwnerType) IsKnown() bool {
switch r {
case ConnectionDeleteParamsOwnerTypeOrganization, ConnectionDeleteParamsOwnerTypeFleet:
return true
}
return false
}
type ConnectionAttachParams struct {
ConnectionID param.Field[string] `json:"connectionId" api:"required" format:"uuid"`
Metadata param.Field[ToolConnectionMetadataParam] `json:"metadata"`
}
func (r ConnectionAttachParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}