|
| 1 | +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | + |
| 3 | +package courier |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "encoding/json" |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + "net/http" |
| 11 | + "net/url" |
| 12 | + "slices" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/trycourier/courier-go/v4/internal/apijson" |
| 16 | + "github.com/trycourier/courier-go/v4/internal/apiquery" |
| 17 | + shimjson "github.com/trycourier/courier-go/v4/internal/encoding/json" |
| 18 | + "github.com/trycourier/courier-go/v4/internal/requestconfig" |
| 19 | + "github.com/trycourier/courier-go/v4/option" |
| 20 | + "github.com/trycourier/courier-go/v4/packages/param" |
| 21 | + "github.com/trycourier/courier-go/v4/packages/respjson" |
| 22 | +) |
| 23 | + |
| 24 | +// JourneyService contains methods and other services that help with interacting |
| 25 | +// with the Courier API. |
| 26 | +// |
| 27 | +// Note, unlike clients, this service does not read variables from the environment |
| 28 | +// automatically. You should not instantiate this service directly, and instead use |
| 29 | +// the [NewJourneyService] method instead. |
| 30 | +type JourneyService struct { |
| 31 | + Options []option.RequestOption |
| 32 | +} |
| 33 | + |
| 34 | +// NewJourneyService generates a new service that applies the given options to each |
| 35 | +// request. These options are applied after the parent client's options (if there |
| 36 | +// is one), and before any request-specific options. |
| 37 | +func NewJourneyService(opts ...option.RequestOption) (r JourneyService) { |
| 38 | + r = JourneyService{} |
| 39 | + r.Options = opts |
| 40 | + return |
| 41 | +} |
| 42 | + |
| 43 | +// Get the list of journeys. |
| 44 | +func (r *JourneyService) List(ctx context.Context, query JourneyListParams, opts ...option.RequestOption) (res *JourneysListResponse, err error) { |
| 45 | + opts = slices.Concat(r.Options, opts) |
| 46 | + path := "journeys" |
| 47 | + err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, query, &res, opts...) |
| 48 | + return res, err |
| 49 | +} |
| 50 | + |
| 51 | +// Invoke a journey run from a journey template. |
| 52 | +func (r *JourneyService) Invoke(ctx context.Context, templateID string, body JourneyInvokeParams, opts ...option.RequestOption) (res *JourneysInvokeResponse, err error) { |
| 53 | + opts = slices.Concat(r.Options, opts) |
| 54 | + if templateID == "" { |
| 55 | + err = errors.New("missing required templateId parameter") |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + path := fmt.Sprintf("journeys/%s/invoke", templateID) |
| 59 | + err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...) |
| 60 | + return res, err |
| 61 | +} |
| 62 | + |
| 63 | +// A journey template representing an automation workflow. |
| 64 | +type Journey struct { |
| 65 | + // The unique identifier of the journey. |
| 66 | + ID string `json:"id" api:"required"` |
| 67 | + // The name of the journey. |
| 68 | + Name string `json:"name" api:"required"` |
| 69 | + // The version of the journey (published or draft). |
| 70 | + // |
| 71 | + // Any of "published", "draft". |
| 72 | + Version JourneyVersion `json:"version" api:"required"` |
| 73 | + // ISO 8601 timestamp when the journey was created. |
| 74 | + CreatedAt time.Time `json:"createdAt" format:"date-time"` |
| 75 | + // ISO 8601 timestamp when the journey was last updated. |
| 76 | + UpdatedAt time.Time `json:"updatedAt" format:"date-time"` |
| 77 | + // JSON contains metadata for fields, check presence with [respjson.Field.Valid]. |
| 78 | + JSON struct { |
| 79 | + ID respjson.Field |
| 80 | + Name respjson.Field |
| 81 | + Version respjson.Field |
| 82 | + CreatedAt respjson.Field |
| 83 | + UpdatedAt respjson.Field |
| 84 | + ExtraFields map[string]respjson.Field |
| 85 | + raw string |
| 86 | + } `json:"-"` |
| 87 | +} |
| 88 | + |
| 89 | +// Returns the unmodified JSON received from the API |
| 90 | +func (r Journey) RawJSON() string { return r.JSON.raw } |
| 91 | +func (r *Journey) UnmarshalJSON(data []byte) error { |
| 92 | + return apijson.UnmarshalRoot(data, r) |
| 93 | +} |
| 94 | + |
| 95 | +// The version of the journey (published or draft). |
| 96 | +type JourneyVersion string |
| 97 | + |
| 98 | +const ( |
| 99 | + JourneyVersionPublished JourneyVersion = "published" |
| 100 | + JourneyVersionDraft JourneyVersion = "draft" |
| 101 | +) |
| 102 | + |
| 103 | +// Request body for invoking a journey. Requires either a user identifier or a |
| 104 | +// profile with contact information. User identifiers can be provided via user_id |
| 105 | +// field, or resolved from profile/data objects (user_id, userId, or anonymousId |
| 106 | +// fields). |
| 107 | +type JourneysInvokeRequestParam struct { |
| 108 | + // A unique identifier for the user. If not provided, the system will attempt to |
| 109 | + // resolve the user identifier from profile or data objects. |
| 110 | + UserID param.Opt[string] `json:"user_id,omitzero"` |
| 111 | + // Data payload passed to the journey. The expected shape can be predefined using |
| 112 | + // the schema builder in the journey editor. This data is available in journey |
| 113 | + // steps for condition evaluation and template variable interpolation. Can also |
| 114 | + // contain user identifiers (user_id, userId, anonymousId) if not provided |
| 115 | + // elsewhere. |
| 116 | + Data map[string]any `json:"data,omitzero"` |
| 117 | + // Profile data for the user. Can contain contact information (email, |
| 118 | + // phone_number), user identifiers (user_id, userId, anonymousId), or any custom |
| 119 | + // profile fields. Profile fields are merged with any existing stored profile for |
| 120 | + // the user. Include context.tenant_id to load a tenant-scoped profile for |
| 121 | + // multi-tenant scenarios. |
| 122 | + Profile map[string]any `json:"profile,omitzero"` |
| 123 | + paramObj |
| 124 | +} |
| 125 | + |
| 126 | +func (r JourneysInvokeRequestParam) MarshalJSON() (data []byte, err error) { |
| 127 | + type shadow JourneysInvokeRequestParam |
| 128 | + return param.MarshalObject(r, (*shadow)(&r)) |
| 129 | +} |
| 130 | +func (r *JourneysInvokeRequestParam) UnmarshalJSON(data []byte) error { |
| 131 | + return apijson.UnmarshalRoot(data, r) |
| 132 | +} |
| 133 | + |
| 134 | +type JourneysInvokeResponse struct { |
| 135 | + // A unique identifier for the journey run that was created. |
| 136 | + RunID string `json:"runId" api:"required"` |
| 137 | + // JSON contains metadata for fields, check presence with [respjson.Field.Valid]. |
| 138 | + JSON struct { |
| 139 | + RunID respjson.Field |
| 140 | + ExtraFields map[string]respjson.Field |
| 141 | + raw string |
| 142 | + } `json:"-"` |
| 143 | +} |
| 144 | + |
| 145 | +// Returns the unmodified JSON received from the API |
| 146 | +func (r JourneysInvokeResponse) RawJSON() string { return r.JSON.raw } |
| 147 | +func (r *JourneysInvokeResponse) UnmarshalJSON(data []byte) error { |
| 148 | + return apijson.UnmarshalRoot(data, r) |
| 149 | +} |
| 150 | + |
| 151 | +type JourneysListResponse struct { |
| 152 | + // A cursor token for pagination. Present when there are more results available. |
| 153 | + Cursor string `json:"cursor"` |
| 154 | + Templates []Journey `json:"templates"` |
| 155 | + // JSON contains metadata for fields, check presence with [respjson.Field.Valid]. |
| 156 | + JSON struct { |
| 157 | + Cursor respjson.Field |
| 158 | + Templates respjson.Field |
| 159 | + ExtraFields map[string]respjson.Field |
| 160 | + raw string |
| 161 | + } `json:"-"` |
| 162 | +} |
| 163 | + |
| 164 | +// Returns the unmodified JSON received from the API |
| 165 | +func (r JourneysListResponse) RawJSON() string { return r.JSON.raw } |
| 166 | +func (r *JourneysListResponse) UnmarshalJSON(data []byte) error { |
| 167 | + return apijson.UnmarshalRoot(data, r) |
| 168 | +} |
| 169 | + |
| 170 | +type JourneyListParams struct { |
| 171 | + // A cursor token for pagination. Use the cursor from the previous response to |
| 172 | + // fetch the next page of results. |
| 173 | + Cursor param.Opt[string] `query:"cursor,omitzero" json:"-"` |
| 174 | + // The version of journeys to retrieve. Accepted values are published (for |
| 175 | + // published journeys) or draft (for draft journeys). Defaults to published. |
| 176 | + // |
| 177 | + // Any of "published", "draft". |
| 178 | + Version JourneyListParamsVersion `query:"version,omitzero" json:"-"` |
| 179 | + paramObj |
| 180 | +} |
| 181 | + |
| 182 | +// URLQuery serializes [JourneyListParams]'s query parameters as `url.Values`. |
| 183 | +func (r JourneyListParams) URLQuery() (v url.Values, err error) { |
| 184 | + return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{ |
| 185 | + ArrayFormat: apiquery.ArrayQueryFormatComma, |
| 186 | + NestedFormat: apiquery.NestedQueryFormatBrackets, |
| 187 | + }) |
| 188 | +} |
| 189 | + |
| 190 | +// The version of journeys to retrieve. Accepted values are published (for |
| 191 | +// published journeys) or draft (for draft journeys). Defaults to published. |
| 192 | +type JourneyListParamsVersion string |
| 193 | + |
| 194 | +const ( |
| 195 | + JourneyListParamsVersionPublished JourneyListParamsVersion = "published" |
| 196 | + JourneyListParamsVersionDraft JourneyListParamsVersion = "draft" |
| 197 | +) |
| 198 | + |
| 199 | +type JourneyInvokeParams struct { |
| 200 | + // Request body for invoking a journey. Requires either a user identifier or a |
| 201 | + // profile with contact information. User identifiers can be provided via user_id |
| 202 | + // field, or resolved from profile/data objects (user_id, userId, or anonymousId |
| 203 | + // fields). |
| 204 | + JourneysInvokeRequest JourneysInvokeRequestParam |
| 205 | + paramObj |
| 206 | +} |
| 207 | + |
| 208 | +func (r JourneyInvokeParams) MarshalJSON() (data []byte, err error) { |
| 209 | + return shimjson.Marshal(r.JourneysInvokeRequest) |
| 210 | +} |
| 211 | +func (r *JourneyInvokeParams) UnmarshalJSON(data []byte) error { |
| 212 | + return json.Unmarshal(data, &r.JourneysInvokeRequest) |
| 213 | +} |
0 commit comments