Repository navigation
Expand file tree
/
Copy pathmonitor.go
More file actions
6964 lines (6362 loc) · 283 KB
/
Copy pathmonitor.go
File metadata and controls
6964 lines (6362 loc) · 283 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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package contextdev
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"slices"
"time"
"github.com/context-dot-dev/context-go-sdk/v2/internal/apijson"
"github.com/context-dot-dev/context-go-sdk/v2/internal/apiquery"
"github.com/context-dot-dev/context-go-sdk/v2/internal/requestconfig"
"github.com/context-dot-dev/context-go-sdk/v2/option"
"github.com/context-dot-dev/context-go-sdk/v2/packages/param"
"github.com/context-dot-dev/context-go-sdk/v2/packages/respjson"
"github.com/context-dot-dev/context-go-sdk/v2/shared/constant"
)
// Watch websites for exact or meaningful changes.
//
// MonitorService contains methods and other services that help with interacting
// with the context.dev 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 [NewMonitorService] method instead.
type MonitorService struct {
options []option.RequestOption
}
// NewMonitorService 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 NewMonitorService(opts ...option.RequestOption) (r MonitorService) {
r = MonitorService{}
r.options = opts
return
}
// Watch a page, URL inventory, or extracted website data on a schedule. A run
// starts immediately to capture the baseline.
func (r *MonitorService) New(ctx context.Context, body MonitorNewParams, opts ...option.RequestOption) (res *MonitorNewResponse, err error) {
opts = slices.Concat(r.options, opts)
path := "monitors"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return res, err
}
// Retrieve a monitor’s configuration and current state.
func (r *MonitorService) Get(ctx context.Context, monitorID string, opts ...option.RequestOption) (res *MonitorGetResponse, err error) {
opts = slices.Concat(r.options, opts)
if monitorID == "" {
err = errors.New("missing required monitor_id parameter")
return nil, err
}
path := fmt.Sprintf("monitors/%s", url.PathEscape(monitorID))
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return res, err
}
// Update a monitor. Changing its target or change detection replaces the baseline
// and queues a new baseline run.
func (r *MonitorService) Update(ctx context.Context, monitorID string, body MonitorUpdateParams, opts ...option.RequestOption) (res *MonitorUpdateResponse, err error) {
opts = slices.Concat(r.options, opts)
if monitorID == "" {
err = errors.New("missing required monitor_id parameter")
return nil, err
}
path := fmt.Sprintf("monitors/%s", url.PathEscape(monitorID))
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPatch, path, body, &res, opts...)
return res, err
}
// List your monitors with optional search and filters.
func (r *MonitorService) List(ctx context.Context, query MonitorListParams, opts ...option.RequestOption) (res *MonitorListResponse, err error) {
opts = slices.Concat(r.options, opts)
path := "monitors"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, query, &res, opts...)
return res, err
}
// Delete a monitor and stop future runs and webhook retries.
func (r *MonitorService) Delete(ctx context.Context, monitorID string, opts ...option.RequestOption) (res *MonitorDeleteResponse, err error) {
opts = slices.Concat(r.options, opts)
if monitorID == "" {
err = errors.New("missing required monitor_id parameter")
return nil, err
}
path := fmt.Sprintf("monitors/%s", url.PathEscape(monitorID))
err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, nil, &res, opts...)
return res, err
}
// Return usage per monitor, highest first, for up to the 10,000 most recent runs
// in the requested window.
func (r *MonitorService) GetCreditUsage(ctx context.Context, query MonitorGetCreditUsageParams, opts ...option.RequestOption) (res *MonitorGetCreditUsageResponse, err error) {
opts = slices.Concat(r.options, opts)
path := "monitors/credit-usage"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, query, &res, opts...)
return res, err
}
// Retrieve your organization’s monitor allowance and usage.
func (r *MonitorService) GetLimits(ctx context.Context, opts ...option.RequestOption) (res *MonitorGetLimitsResponse, err error) {
opts = slices.Concat(r.options, opts)
path := "monitors/limits"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return res, err
}
// List full change records across your monitors, newest first.
func (r *MonitorService) ListAccountChanges(ctx context.Context, query MonitorListAccountChangesParams, opts ...option.RequestOption) (res *MonitorListAccountChangesResponse, err error) {
opts = slices.Concat(r.options, opts)
path := "monitors/changes"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, query, &res, opts...)
return res, err
}
// List runs across your monitors, newest first.
func (r *MonitorService) ListAccountRuns(ctx context.Context, query MonitorListAccountRunsParams, opts ...option.RequestOption) (res *MonitorListAccountRunsResponse, err error) {
opts = slices.Concat(r.options, opts)
path := "monitors/runs"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, query, &res, opts...)
return res, err
}
// List full change records for a monitor, newest first.
func (r *MonitorService) ListChanges(ctx context.Context, monitorID string, query MonitorListChangesParams, opts ...option.RequestOption) (res *MonitorListChangesResponse, err error) {
opts = slices.Concat(r.options, opts)
if monitorID == "" {
err = errors.New("missing required monitor_id parameter")
return nil, err
}
path := fmt.Sprintf("monitors/%s/changes", url.PathEscape(monitorID))
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, query, &res, opts...)
return res, err
}
// List a monitor’s runs, newest first.
func (r *MonitorService) ListRuns(ctx context.Context, monitorID string, query MonitorListRunsParams, opts ...option.RequestOption) (res *MonitorListRunsResponse, err error) {
opts = slices.Concat(r.options, opts)
if monitorID == "" {
err = errors.New("missing required monitor_id parameter")
return nil, err
}
path := fmt.Sprintf("monitors/%s/runs", url.PathEscape(monitorID))
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, query, &res, opts...)
return res, err
}
// Retrieve a detected change, including its diff and available evidence.
func (r *MonitorService) GetChange(ctx context.Context, changeID string, opts ...option.RequestOption) (res *MonitorGetChangeResponse, err error) {
opts = slices.Concat(r.options, opts)
if changeID == "" {
err = errors.New("missing required change_id parameter")
return nil, err
}
path := fmt.Sprintf("monitors/changes/%s", url.PathEscape(changeID))
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return res, err
}
// Retrieve the status, timing, and results of one monitor run.
func (r *MonitorService) GetRun(ctx context.Context, runID string, query MonitorGetRunParams, opts ...option.RequestOption) (res *MonitorGetRunResponse, err error) {
opts = slices.Concat(r.options, opts)
if query.MonitorID == "" {
err = errors.New("missing required monitor_id parameter")
return nil, err
}
if runID == "" {
err = errors.New("missing required run_id parameter")
return nil, err
}
path := fmt.Sprintf("monitors/%s/runs/%s", url.PathEscape(query.MonitorID), url.PathEscape(runID))
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return res, err
}
// Generate and return a new signing secret. It takes effect immediately for all
// subsequent delivery attempts.
func (r *MonitorService) RotateWebhookSecret(ctx context.Context, monitorID string, opts ...option.RequestOption) (res *MonitorRotateWebhookSecretResponse, err error) {
opts = slices.Concat(r.options, opts)
if monitorID == "" {
err = errors.New("missing required monitor_id parameter")
return nil, err
}
path := fmt.Sprintf("monitors/%s/webhook/rotate-secret", url.PathEscape(monitorID))
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, nil, &res, opts...)
return res, err
}
// Queue a run without changing the regular schedule. Paused monitors return 409.
func (r *MonitorService) Run(ctx context.Context, monitorID string, opts ...option.RequestOption) (res *MonitorRunResponse, err error) {
opts = slices.Concat(r.options, opts)
if monitorID == "" {
err = errors.New("missing required monitor_id parameter")
return nil, err
}
path := fmt.Sprintf("monitors/%s/run", url.PathEscape(monitorID))
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, nil, &res, opts...)
return res, err
}
type WebhookDelivery struct {
AttemptedAt time.Time `json:"attempted_at" api:"required" format:"date-time"`
Error WebhookDeliveryError `json:"error" api:"required"`
// The event this delivery carried. Deliveries recorded before event selection
// existed report change.detected.
//
// Any of "change.detected", "run.completed".
Event WebhookDeliveryEvent `json:"event" api:"required"`
// Identifier sent in the X-Context-Id header.
EventID string `json:"event_id" api:"required"`
// The endpoint's final HTTP response status, or null when no response was
// received.
HTTPStatus int64 `json:"http_status" api:"required"`
// Outcome of the delivery attempt. Any 2xx response counts as delivered.
//
// Any of "delivered", "rejected", "failed", "skipped_unsafe_url".
Status WebhookDeliveryStatus `json:"status" api:"required"`
// Delivery ID for status checks and retries, when available.
DeliveryID string `json:"delivery_id"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
AttemptedAt respjson.Field
Error respjson.Field
Event respjson.Field
EventID respjson.Field
HTTPStatus respjson.Field
Status respjson.Field
DeliveryID respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r WebhookDelivery) RawJSON() string { return r.JSON.raw }
func (r *WebhookDelivery) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type WebhookDeliveryError struct {
Code string `json:"code" api:"required"`
Message string `json:"message" api:"required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Code respjson.Field
Message respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r WebhookDeliveryError) RawJSON() string { return r.JSON.raw }
func (r *WebhookDeliveryError) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// The event this delivery carried. Deliveries recorded before event selection
// existed report change.detected.
type WebhookDeliveryEvent string
const (
WebhookDeliveryEventChangeDetected WebhookDeliveryEvent = "change.detected"
WebhookDeliveryEventRunCompleted WebhookDeliveryEvent = "run.completed"
)
// Outcome of the delivery attempt. Any 2xx response counts as delivered.
type WebhookDeliveryStatus string
const (
WebhookDeliveryStatusDelivered WebhookDeliveryStatus = "delivered"
WebhookDeliveryStatusRejected WebhookDeliveryStatus = "rejected"
WebhookDeliveryStatusFailed WebhookDeliveryStatus = "failed"
WebhookDeliveryStatusSkippedUnsafeURL WebhookDeliveryStatus = "skipped_unsafe_url"
)
type MonitorNewResponse struct {
ID string `json:"id" api:"required"`
// How changes are judged. Defaults to `semantic` for extract targets and page
// targets with `instructions`, otherwise `exact`.
ChangeDetection MonitorNewResponseChangeDetectionUnion `json:"change_detection" api:"required"`
CreatedAt time.Time `json:"created_at" api:"required" format:"date-time"`
// ID of the baseline run queued at creation; null if it will start on the next
// scheduled tick.
InitialRunID string `json:"initial_run_id" api:"required"`
// Always `web`. Optional.
//
// Any of "web".
Mode MonitorNewResponseMode `json:"mode" api:"required"`
Name string `json:"name" api:"required"`
// Unique ID of this request, also in `X-Request-Id`. Include it when contacting
// support.
RequestID string `json:"request_id" api:"required" format:"uuid"`
// Current state. Failed monitors keep running; paused monitors must be resumed
// with `status: "active"`.
//
// Any of "active", "paused", "failed".
Status MonitorNewResponseStatus `json:"status" api:"required"`
// What to watch: a page, a sitemap, or data extracted from a site.
Target MonitorNewResponseTargetUnion `json:"target" api:"required"`
UpdatedAt time.Time `json:"updated_at" api:"required" format:"date-time"`
// Comparison baseline, included on Retrieve. Null until capture completes or after
// target changes.
Baseline MonitorNewResponseBaselineUnion `json:"baseline" api:"nullable"`
// Credits this request used and your remaining balance.
KeyMetadata MonitorNewResponseKeyMetadata `json:"key_metadata"`
LastChangeAt time.Time `json:"last_change_at" api:"nullable" format:"date-time"`
// Error from the most recent failed run; null when the last run succeeded.
LastError MonitorNewResponseLastError `json:"last_error" api:"nullable"`
LastRunAt time.Time `json:"last_run_at" api:"nullable" format:"date-time"`
// When the next scheduled run is due; null while paused.
NextRunAt time.Time `json:"next_run_at" api:"nullable" format:"date-time"`
// Run the monitor on a fixed interval defined by a frequency and a unit, e.g.
// every 6 hours or every 2 days. The total interval (frequency × unit) must be
// between 10 minutes and 1 year.
Schedule MonitorNewResponseSchedule `json:"schedule"`
// Labels for filtering monitors, their changes, and their usage.
Tags []string `json:"tags"`
// Webhook destination and delivery settings. Null means no webhook is configured.
Webhook MonitorNewResponseWebhook `json:"webhook" api:"nullable"`
// Present while webhook deliveries are failing consecutively; null when deliveries
// are healthy or no webhook is configured. Cleared on the next successful delivery
// and when the webhook URL changes.
WebhookFailure MonitorNewResponseWebhookFailure `json:"webhook_failure" api:"nullable"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
ID respjson.Field
ChangeDetection respjson.Field
CreatedAt respjson.Field
InitialRunID respjson.Field
Mode respjson.Field
Name respjson.Field
RequestID respjson.Field
Status respjson.Field
Target respjson.Field
UpdatedAt respjson.Field
Baseline respjson.Field
KeyMetadata respjson.Field
LastChangeAt respjson.Field
LastError respjson.Field
LastRunAt respjson.Field
NextRunAt respjson.Field
Schedule respjson.Field
Tags respjson.Field
Webhook respjson.Field
WebhookFailure respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r MonitorNewResponse) RawJSON() string { return r.JSON.raw }
func (r *MonitorNewResponse) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// MonitorNewResponseChangeDetectionUnion contains all possible properties and
// values from [MonitorNewResponseChangeDetectionExact],
// [MonitorNewResponseChangeDetectionSemantic].
//
// Use the [MonitorNewResponseChangeDetectionUnion.AsAny] method to switch on the
// variant.
//
// Use the methods beginning with 'As' to cast the union to one of its variants.
type MonitorNewResponseChangeDetectionUnion struct {
// Any of "exact", "semantic".
Type string `json:"type"`
// This field is from variant [MonitorNewResponseChangeDetectionSemantic].
ConfidenceThreshold float64 `json:"confidence_threshold"`
JSON struct {
Type respjson.Field
ConfidenceThreshold respjson.Field
raw string
} `json:"-"`
}
// anyMonitorNewResponseChangeDetection is implemented by each variant of
// [MonitorNewResponseChangeDetectionUnion] to add type safety for the return type
// of [MonitorNewResponseChangeDetectionUnion.AsAny]
type anyMonitorNewResponseChangeDetection interface {
implMonitorNewResponseChangeDetectionUnion()
}
func (MonitorNewResponseChangeDetectionExact) implMonitorNewResponseChangeDetectionUnion() {}
func (MonitorNewResponseChangeDetectionSemantic) implMonitorNewResponseChangeDetectionUnion() {}
// Use the following switch statement to find the correct variant
//
// switch variant := MonitorNewResponseChangeDetectionUnion.AsAny().(type) {
// case contextdev.MonitorNewResponseChangeDetectionExact:
// case contextdev.MonitorNewResponseChangeDetectionSemantic:
// default:
// fmt.Errorf("no variant present")
// }
func (u MonitorNewResponseChangeDetectionUnion) AsAny() anyMonitorNewResponseChangeDetection {
switch u.Type {
case "exact":
return u.AsExact()
case "semantic":
return u.AsSemantic()
}
return nil
}
func (u MonitorNewResponseChangeDetectionUnion) AsExact() (v MonitorNewResponseChangeDetectionExact) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
func (u MonitorNewResponseChangeDetectionUnion) AsSemantic() (v MonitorNewResponseChangeDetectionSemantic) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
// Returns the unmodified JSON received from the API
func (u MonitorNewResponseChangeDetectionUnion) RawJSON() string { return u.JSON.raw }
func (r *MonitorNewResponseChangeDetectionUnion) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Detect exact changes. For page targets, this means visible text diffs. For
// sitemap targets, this means URL additions and removals.
type MonitorNewResponseChangeDetectionExact struct {
// Use `exact` to compare visible text or sitemap URLs.
Type constant.Exact `json:"type" default:"exact"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Type respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r MonitorNewResponseChangeDetectionExact) RawJSON() string { return r.JSON.raw }
func (r *MonitorNewResponseChangeDetectionExact) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Detect meaningful content changes using the target’s instructions and optional
// schema.
type MonitorNewResponseChangeDetectionSemantic struct {
// Use `semantic` to judge changes against the target instructions.
Type constant.Semantic `json:"type" default:"semantic"`
// Minimum confidence required to report a meaningful change, from 0 to 1.
ConfidenceThreshold float64 `json:"confidence_threshold"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Type respjson.Field
ConfidenceThreshold respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r MonitorNewResponseChangeDetectionSemantic) RawJSON() string { return r.JSON.raw }
func (r *MonitorNewResponseChangeDetectionSemantic) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Always `web`. Optional.
type MonitorNewResponseMode string
const (
MonitorNewResponseModeWeb MonitorNewResponseMode = "web"
)
// Current state. Failed monitors keep running; paused monitors must be resumed
// with `status: "active"`.
type MonitorNewResponseStatus string
const (
MonitorNewResponseStatusActive MonitorNewResponseStatus = "active"
MonitorNewResponseStatusPaused MonitorNewResponseStatus = "paused"
MonitorNewResponseStatusFailed MonitorNewResponseStatus = "failed"
)
// MonitorNewResponseTargetUnion contains all possible properties and values from
// [MonitorNewResponseTargetPage], [MonitorNewResponseTargetSitemap],
// [MonitorNewResponseTargetExtract].
//
// Use the [MonitorNewResponseTargetUnion.AsAny] method to switch on the variant.
//
// Use the methods beginning with 'As' to cast the union to one of its variants.
type MonitorNewResponseTargetUnion struct {
// Any of "page", "sitemap", "extract".
Type string `json:"type"`
URL string `json:"url"`
// This field is from variant [MonitorNewResponseTargetPage].
Actions []MonitorNewResponseTargetPageActionUnion `json:"actions"`
// This field is from variant [MonitorNewResponseTargetPage].
ExcludeSelectors []string `json:"exclude_selectors"`
// This field is from variant [MonitorNewResponseTargetPage].
IncludeSelectors []string `json:"include_selectors"`
Instructions string `json:"instructions"`
// This field is from variant [MonitorNewResponseTargetPage].
NormalizeWhitespace bool `json:"normalize_whitespace"`
// This field is from variant [MonitorNewResponseTargetSitemap].
Exclude []string `json:"exclude"`
// This field is from variant [MonitorNewResponseTargetSitemap].
Include []string `json:"include"`
// This field is from variant [MonitorNewResponseTargetSitemap].
MaxURLs int64 `json:"max_urls"`
// This field is from variant [MonitorNewResponseTargetExtract].
FollowSubdomains bool `json:"follow_subdomains"`
// This field is from variant [MonitorNewResponseTargetExtract].
MaxDepth int64 `json:"max_depth"`
// This field is from variant [MonitorNewResponseTargetExtract].
MaxPages int64 `json:"max_pages"`
// This field is from variant [MonitorNewResponseTargetExtract].
Schema map[string]any `json:"schema"`
JSON struct {
Type respjson.Field
URL respjson.Field
Actions respjson.Field
ExcludeSelectors respjson.Field
IncludeSelectors respjson.Field
Instructions respjson.Field
NormalizeWhitespace respjson.Field
Exclude respjson.Field
Include respjson.Field
MaxURLs respjson.Field
FollowSubdomains respjson.Field
MaxDepth respjson.Field
MaxPages respjson.Field
Schema respjson.Field
raw string
} `json:"-"`
}
// anyMonitorNewResponseTarget is implemented by each variant of
// [MonitorNewResponseTargetUnion] to add type safety for the return type of
// [MonitorNewResponseTargetUnion.AsAny]
type anyMonitorNewResponseTarget interface {
implMonitorNewResponseTargetUnion()
}
func (MonitorNewResponseTargetPage) implMonitorNewResponseTargetUnion() {}
func (MonitorNewResponseTargetSitemap) implMonitorNewResponseTargetUnion() {}
func (MonitorNewResponseTargetExtract) implMonitorNewResponseTargetUnion() {}
// Use the following switch statement to find the correct variant
//
// switch variant := MonitorNewResponseTargetUnion.AsAny().(type) {
// case contextdev.MonitorNewResponseTargetPage:
// case contextdev.MonitorNewResponseTargetSitemap:
// case contextdev.MonitorNewResponseTargetExtract:
// default:
// fmt.Errorf("no variant present")
// }
func (u MonitorNewResponseTargetUnion) AsAny() anyMonitorNewResponseTarget {
switch u.Type {
case "page":
return u.AsPage()
case "sitemap":
return u.AsSitemap()
case "extract":
return u.AsExtract()
}
return nil
}
func (u MonitorNewResponseTargetUnion) AsPage() (v MonitorNewResponseTargetPage) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
func (u MonitorNewResponseTargetUnion) AsSitemap() (v MonitorNewResponseTargetSitemap) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
func (u MonitorNewResponseTargetUnion) AsExtract() (v MonitorNewResponseTargetExtract) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
// Returns the unmodified JSON received from the API
func (u MonitorNewResponseTargetUnion) RawJSON() string { return u.JSON.raw }
func (r *MonitorNewResponseTargetUnion) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Watch a single web page. Exact detection reports visible-text diffs; semantic
// detection judges confirmed stable diffs against `instructions`.
type MonitorNewResponseTargetPage struct {
// Use `page` to watch one web page.
Type constant.Page `json:"type" default:"page"`
// Public HTTP(S) page URL to monitor.
URL string `json:"url" api:"required" format:"uri"`
// Optional browser actions executed in array order after the page loads, before
// content is captured, on every run. Requires a paid plan. Maximum: 5 actions.
// Changes create a new baseline.
Actions []MonitorNewResponseTargetPageActionUnion `json:"actions" api:"nullable"`
// Remove matching regions after inclusions. Changes create a new baseline.
ExcludeSelectors []string `json:"exclude_selectors"`
// Monitor these CSS-selected regions. Empty or omitted uses main content. Changes
// create a new baseline.
IncludeSelectors []string `json:"include_selectors"`
// Plain-language goal describing which page changes matter. When provided without
// change_detection, semantic detection is inferred.
Instructions string `json:"instructions"`
// Normalize whitespace before comparing or analyzing text.
NormalizeWhitespace bool `json:"normalize_whitespace"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Type respjson.Field
URL respjson.Field
Actions respjson.Field
ExcludeSelectors respjson.Field
IncludeSelectors respjson.Field
Instructions respjson.Field
NormalizeWhitespace respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r MonitorNewResponseTargetPage) RawJSON() string { return r.JSON.raw }
func (r *MonitorNewResponseTargetPage) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// MonitorNewResponseTargetPageActionUnion contains all possible properties and
// values from [MonitorNewResponseTargetPageActionWait],
// [MonitorNewResponseTargetPageActionPerform],
// [MonitorNewResponseTargetPageActionScroll].
//
// Use the [MonitorNewResponseTargetPageActionUnion.AsAny] method to switch on the
// variant.
//
// Use the methods beginning with 'As' to cast the union to one of its variants.
type MonitorNewResponseTargetPageActionUnion struct {
// Any of "wait", "perform", "scroll".
Do string `json:"do"`
// This field is from variant [MonitorNewResponseTargetPageActionWait].
TimeMs int64 `json:"timeMs"`
// This field is from variant [MonitorNewResponseTargetPageActionPerform].
Action string `json:"action"`
// This field is from variant [MonitorNewResponseTargetPageActionScroll].
Amount MonitorNewResponseTargetPageActionScrollAmountUnion `json:"amount"`
// This field is from variant [MonitorNewResponseTargetPageActionScroll].
Container string `json:"container"`
// This field is from variant [MonitorNewResponseTargetPageActionScroll].
Direction string `json:"direction"`
// This field is from variant [MonitorNewResponseTargetPageActionScroll].
MaxScrolls int64 `json:"maxScrolls"`
JSON struct {
Do respjson.Field
TimeMs respjson.Field
Action respjson.Field
Amount respjson.Field
Container respjson.Field
Direction respjson.Field
MaxScrolls respjson.Field
raw string
} `json:"-"`
}
// anyMonitorNewResponseTargetPageAction is implemented by each variant of
// [MonitorNewResponseTargetPageActionUnion] to add type safety for the return type
// of [MonitorNewResponseTargetPageActionUnion.AsAny]
type anyMonitorNewResponseTargetPageAction interface {
implMonitorNewResponseTargetPageActionUnion()
}
func (MonitorNewResponseTargetPageActionWait) implMonitorNewResponseTargetPageActionUnion() {}
func (MonitorNewResponseTargetPageActionPerform) implMonitorNewResponseTargetPageActionUnion() {}
func (MonitorNewResponseTargetPageActionScroll) implMonitorNewResponseTargetPageActionUnion() {}
// Use the following switch statement to find the correct variant
//
// switch variant := MonitorNewResponseTargetPageActionUnion.AsAny().(type) {
// case contextdev.MonitorNewResponseTargetPageActionWait:
// case contextdev.MonitorNewResponseTargetPageActionPerform:
// case contextdev.MonitorNewResponseTargetPageActionScroll:
// default:
// fmt.Errorf("no variant present")
// }
func (u MonitorNewResponseTargetPageActionUnion) AsAny() anyMonitorNewResponseTargetPageAction {
switch u.Do {
case "wait":
return u.AsWait()
case "perform":
return u.AsPerform()
case "scroll":
return u.AsScroll()
}
return nil
}
func (u MonitorNewResponseTargetPageActionUnion) AsWait() (v MonitorNewResponseTargetPageActionWait) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
func (u MonitorNewResponseTargetPageActionUnion) AsPerform() (v MonitorNewResponseTargetPageActionPerform) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
func (u MonitorNewResponseTargetPageActionUnion) AsScroll() (v MonitorNewResponseTargetPageActionScroll) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
// Returns the unmodified JSON received from the API
func (u MonitorNewResponseTargetPageActionUnion) RawJSON() string { return u.JSON.raw }
func (r *MonitorNewResponseTargetPageActionUnion) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Pause for a fixed number of milliseconds before continuing to the next action.
type MonitorNewResponseTargetPageActionWait struct {
// Use `wait` to pause for a fixed duration.
Do constant.Wait `json:"do" default:"wait"`
// Time to pause in milliseconds before the next action.
TimeMs int64 `json:"timeMs" api:"required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Do respjson.Field
TimeMs respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r MonitorNewResponseTargetPageActionWait) RawJSON() string { return r.JSON.raw }
func (r *MonitorNewResponseTargetPageActionWait) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Resolve and perform one natural-language browser action.
type MonitorNewResponseTargetPageActionPerform struct {
// One browser instruction, such as clicking a button or entering text.
Action string `json:"action" api:"required"`
// Use `perform` for a plain-language browser instruction.
Do constant.Perform `json:"do" default:"perform"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Action respjson.Field
Do respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r MonitorNewResponseTargetPageActionPerform) RawJSON() string { return r.JSON.raw }
func (r *MonitorNewResponseTargetPageActionPerform) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Scroll the page or a selected scrollable container, waiting adaptively for
// content and dimensions to settle after each iteration.
type MonitorNewResponseTargetPageActionScroll struct {
// Use `scroll` to move through the page or a container.
Do constant.Scroll `json:"do" default:"scroll"`
// Pixels per scroll, one visible viewport, or the current scroll boundary.
// Defaults to viewport.
Amount MonitorNewResponseTargetPageActionScrollAmountUnion `json:"amount"`
// CSS selector for the first matching scroll container. Defaults to the page.
Container string `json:"container"`
// Direction to scroll. Defaults to down.
//
// Any of "up", "down", "left", "right".
Direction string `json:"direction"`
// Maximum scroll iterations. Stops early when scrolling and scrollable extent stop
// changing. Defaults to 1.
MaxScrolls int64 `json:"maxScrolls"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Do respjson.Field
Amount respjson.Field
Container respjson.Field
Direction respjson.Field
MaxScrolls respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r MonitorNewResponseTargetPageActionScroll) RawJSON() string { return r.JSON.raw }
func (r *MonitorNewResponseTargetPageActionScroll) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// MonitorNewResponseTargetPageActionScrollAmountUnion contains all possible
// properties and values from [int64], [string].
//
// Use the methods beginning with 'As' to cast the union to one of its variants.
//
// If the underlying value is not a json object, one of the following properties
// will be valid: OfInt OfMonitorNewResponseTargetPageActionScrollAmountString]
type MonitorNewResponseTargetPageActionScrollAmountUnion struct {
// This field will be present if the value is a [int64] instead of an object.
OfInt int64 `json:",inline"`
// This field will be present if the value is a [string] instead of an object.
OfMonitorNewResponseTargetPageActionScrollAmountString string `json:",inline"`
JSON struct {
OfInt respjson.Field
OfMonitorNewResponseTargetPageActionScrollAmountString respjson.Field
raw string
} `json:"-"`
}
func (u MonitorNewResponseTargetPageActionScrollAmountUnion) AsInt() (v int64) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
func (u MonitorNewResponseTargetPageActionScrollAmountUnion) AsMonitorNewResponseTargetPageActionScrollAmountString() (v string) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
// Returns the unmodified JSON received from the API
func (u MonitorNewResponseTargetPageActionScrollAmountUnion) RawJSON() string { return u.JSON.raw }
func (r *MonitorNewResponseTargetPageActionScrollAmountUnion) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type MonitorNewResponseTargetPageActionScrollAmountString string
const (
MonitorNewResponseTargetPageActionScrollAmountStringViewport MonitorNewResponseTargetPageActionScrollAmountString = "viewport"
MonitorNewResponseTargetPageActionScrollAmountStringMax MonitorNewResponseTargetPageActionScrollAmountString = "max"
)
// Watch a site’s URL inventory for confirmed additions and removals.
type MonitorNewResponseTargetSitemap struct {
// Use `sitemap` to watch a site for added or removed URLs.
Type constant.Sitemap `json:"type" default:"sitemap"`
// Sitemap URL to monitor.
URL string `json:"url" api:"required" format:"uri"`
// URL path patterns to exclude (max 50).
Exclude []string `json:"exclude"`
// URL path patterns to include (max 50).
Include []string `json:"include"`
// Maximum number of sitemap URLs to track (capped at 10,000).
MaxURLs int64 `json:"max_urls"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Type respjson.Field
URL respjson.Field
Exclude respjson.Field
Include respjson.Field
MaxURLs respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r MonitorNewResponseTargetSitemap) RawJSON() string { return r.JSON.raw }
func (r *MonitorNewResponseTargetSitemap) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Track relevant pages selected by `schema` and `instructions`; refresh the page
// set periodically.
type MonitorNewResponseTargetExtract struct {
// Natural-language instructions guiding which pages and facts to track and which
// changes to report.
Instructions string `json:"instructions" api:"required"`
// Use `extract` to watch structured data across selected pages.
Type constant.Extract `json:"type" default:"extract"`
// Root URL to extract structured data from.
URL string `json:"url" api:"required" format:"uri"`
// Allow page discovery on subdomains of the target site.
FollowSubdomains bool `json:"follow_subdomains"`
// Optional maximum link depth from the starting URL (0 = only the starting page).
MaxDepth int64 `json:"max_depth"`
// Maximum number of pages to track.
MaxPages int64 `json:"max_pages"`
// JSON Schema for page selection and the baseline snapshot. Changes return diffs
// and evidence.
Schema map[string]any `json:"schema"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Instructions respjson.Field
Type respjson.Field
URL respjson.Field
FollowSubdomains respjson.Field
MaxDepth respjson.Field
MaxPages respjson.Field
Schema respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r MonitorNewResponseTargetExtract) RawJSON() string { return r.JSON.raw }
func (r *MonitorNewResponseTargetExtract) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// MonitorNewResponseBaselineUnion contains all possible properties and values from
// [MonitorNewResponseBaselinePageBaseline],
// [MonitorNewResponseBaselineSitemapBaseline],
// [MonitorNewResponseBaselineExtractBaseline].
//
// Use the methods beginning with 'As' to cast the union to one of its variants.
type MonitorNewResponseBaselineUnion struct {
CapturedAt time.Time `json:"captured_at"`
// This field is from variant [MonitorNewResponseBaselinePageBaseline].
Text string `json:"text"`
// This field is from variant [MonitorNewResponseBaselineSitemapBaseline].
URLCount int64 `json:"url_count"`
// This field is from variant [MonitorNewResponseBaselineSitemapBaseline].
URLs []string `json:"urls"`
// This field is from variant [MonitorNewResponseBaselineExtractBaseline].
Data any `json:"data"`
// This field is from variant [MonitorNewResponseBaselineExtractBaseline].
URLsAnalyzed []string `json:"urls_analyzed"`
JSON struct {
CapturedAt respjson.Field
Text respjson.Field
URLCount respjson.Field
URLs respjson.Field
Data respjson.Field
URLsAnalyzed respjson.Field
raw string
} `json:"-"`
}
func (u MonitorNewResponseBaselineUnion) AsPageBaseline() (v MonitorNewResponseBaselinePageBaseline) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
func (u MonitorNewResponseBaselineUnion) AsSitemapBaseline() (v MonitorNewResponseBaselineSitemapBaseline) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
func (u MonitorNewResponseBaselineUnion) AsExtractBaseline() (v MonitorNewResponseBaselineExtractBaseline) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
// Returns the unmodified JSON received from the API
func (u MonitorNewResponseBaselineUnion) RawJSON() string { return u.JSON.raw }
func (r *MonitorNewResponseBaselineUnion) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Current baseline of a `page` monitor: the visible page text as last observed.
type MonitorNewResponseBaselinePageBaseline struct {
// When this baseline was last captured or replaced.
CapturedAt time.Time `json:"captured_at" api:"required" format:"date-time"`
// The page's visible text as last observed.
Text string `json:"text" api:"required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
CapturedAt respjson.Field
Text respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r MonitorNewResponseBaselinePageBaseline) RawJSON() string { return r.JSON.raw }
func (r *MonitorNewResponseBaselinePageBaseline) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Current baseline of a `sitemap` monitor: the normalized URL set as last
// observed.
type MonitorNewResponseBaselineSitemapBaseline struct {
// When this baseline was last captured or replaced.
CapturedAt time.Time `json:"captured_at" api:"required" format:"date-time"`
// Number of URLs in the baseline.
URLCount int64 `json:"url_count" api:"required"`
// The sitemap URLs as last observed (sorted, normalized).
URLs []string `json:"urls" api:"required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
CapturedAt respjson.Field
URLCount respjson.Field