-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraft.go
More file actions
195 lines (174 loc) · 6.58 KB
/
draft.go
File metadata and controls
195 lines (174 loc) · 6.58 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
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package xtwitterscraper
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"slices"
"time"
"github.com/Xquik-dev/x-twitter-scraper-go/internal/apijson"
"github.com/Xquik-dev/x-twitter-scraper-go/internal/apiquery"
"github.com/Xquik-dev/x-twitter-scraper-go/internal/requestconfig"
"github.com/Xquik-dev/x-twitter-scraper-go/option"
"github.com/Xquik-dev/x-twitter-scraper-go/packages/param"
"github.com/Xquik-dev/x-twitter-scraper-go/packages/respjson"
)
// AI tweet composition, drafts, writing styles, and radar
//
// DraftService contains methods and other services that help with interacting with
// the x-twitter-scraper 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 [NewDraftService] method instead.
type DraftService struct {
options []option.RequestOption
}
// NewDraftService 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 NewDraftService(opts ...option.RequestOption) (r DraftService) {
r = DraftService{}
r.options = opts
return
}
// Save a tweet draft
func (r *DraftService) New(ctx context.Context, body DraftNewParams, opts ...option.RequestOption) (res *DraftDetail, err error) {
opts = slices.Concat(r.options, opts)
path := "drafts"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return res, err
}
// Get draft by ID
func (r *DraftService) Get(ctx context.Context, id string, opts ...option.RequestOption) (res *DraftDetail, err error) {
opts = slices.Concat(r.options, opts)
if id == "" {
err = errors.New("missing required id parameter")
return nil, err
}
path := fmt.Sprintf("drafts/%s", url.PathEscape(id))
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return res, err
}
// List saved drafts
func (r *DraftService) List(ctx context.Context, query DraftListParams, opts ...option.RequestOption) (res *DraftListResponse, err error) {
opts = slices.Concat(r.options, opts)
path := "drafts"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, query, &res, opts...)
return res, err
}
// Delete a draft
func (r *DraftService) Delete(ctx context.Context, id string, opts ...option.RequestOption) (err error) {
opts = slices.Concat(r.options, opts)
opts = append([]option.RequestOption{option.WithHeader("Accept", "*/*")}, opts...)
if id == "" {
err = errors.New("missing required id parameter")
return err
}
path := fmt.Sprintf("drafts/%s", url.PathEscape(id))
err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, nil, nil, opts...)
return err
}
// Saved tweet draft with optional topic and goal.
type Draft struct {
ID string `json:"id" api:"required"`
CreatedAt time.Time `json:"createdAt" api:"required" format:"date-time"`
Text string `json:"text" api:"required"`
Goal string `json:"goal"`
Topic string `json:"topic"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
ID respjson.Field
CreatedAt respjson.Field
Text respjson.Field
Goal respjson.Field
Topic respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r Draft) RawJSON() string { return r.JSON.raw }
func (r *Draft) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Full tweet draft including update timestamp.
type DraftDetail struct {
ID string `json:"id" api:"required"`
CreatedAt time.Time `json:"createdAt" api:"required" format:"date-time"`
Text string `json:"text" api:"required"`
UpdatedAt time.Time `json:"updatedAt" api:"required" format:"date-time"`
Goal string `json:"goal"`
Topic string `json:"topic"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
ID respjson.Field
CreatedAt respjson.Field
Text respjson.Field
UpdatedAt respjson.Field
Goal respjson.Field
Topic respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r DraftDetail) RawJSON() string { return r.JSON.raw }
func (r *DraftDetail) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type DraftListResponse struct {
Drafts []Draft `json:"drafts" api:"required"`
HasMore bool `json:"hasMore" api:"required"`
NextCursor string `json:"nextCursor"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Drafts respjson.Field
HasMore respjson.Field
NextCursor respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r DraftListResponse) RawJSON() string { return r.JSON.raw }
func (r *DraftListResponse) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type DraftNewParams struct {
Text string `json:"text" api:"required"`
Topic param.Opt[string] `json:"topic,omitzero"`
// Any of "engagement", "followers", "authority", "conversation".
Goal DraftNewParamsGoal `json:"goal,omitzero"`
paramObj
}
func (r DraftNewParams) MarshalJSON() (data []byte, err error) {
type shadow DraftNewParams
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *DraftNewParams) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type DraftNewParamsGoal string
const (
DraftNewParamsGoalEngagement DraftNewParamsGoal = "engagement"
DraftNewParamsGoalFollowers DraftNewParamsGoal = "followers"
DraftNewParamsGoalAuthority DraftNewParamsGoal = "authority"
DraftNewParamsGoalConversation DraftNewParamsGoal = "conversation"
)
type DraftListParams struct {
// Cursor for pagination
AfterCursor param.Opt[string] `query:"afterCursor,omitzero" json:"-"`
// Maximum number of items to return (1-100, default 50)
Limit param.Opt[int64] `query:"limit,omitzero" json:"-"`
paramObj
}
// URLQuery serializes [DraftListParams]'s query parameters as `url.Values`.
func (r DraftListParams) URLQuery() (v url.Values, err error) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatComma,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}