xdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +123 -0
- data/lib/xdk/client.rb +410 -0
- data/lib/xdk/clients/account_activity_client.rb +109 -0
- data/lib/xdk/clients/account_client.rb +53 -0
- data/lib/xdk/clients/activity_client.rb +119 -0
- data/lib/xdk/clients/articles_client.rb +54 -0
- data/lib/xdk/clients/bots_client.rb +138 -0
- data/lib/xdk/clients/broadcasts_client.rb +854 -0
- data/lib/xdk/clients/calls_client.rb +255 -0
- data/lib/xdk/clients/chat_client.rb +1017 -0
- data/lib/xdk/clients/communities_client.rb +69 -0
- data/lib/xdk/clients/community_notes_client.rb +147 -0
- data/lib/xdk/clients/compliance_client.rb +157 -0
- data/lib/xdk/clients/connections_client.rb +102 -0
- data/lib/xdk/clients/direct_messages_client.rb +269 -0
- data/lib/xdk/clients/general_client.rb +34 -0
- data/lib/xdk/clients/lists_client.rb +245 -0
- data/lib/xdk/clients/media_client.rb +400 -0
- data/lib/xdk/clients/news_client.rb +67 -0
- data/lib/xdk/clients/posts_client.rb +596 -0
- data/lib/xdk/clients/spaces_client.rb +222 -0
- data/lib/xdk/clients/stream_client.rb +690 -0
- data/lib/xdk/clients/trends_client.rb +81 -0
- data/lib/xdk/clients/usage_client.rb +57 -0
- data/lib/xdk/clients/users_client.rb +1280 -0
- data/lib/xdk/clients/webhooks_client.rb +164 -0
- data/lib/xdk/errors.rb +69 -0
- data/lib/xdk/http_client.rb +126 -0
- data/lib/xdk/oauth1.rb +183 -0
- data/lib/xdk/oauth2_pkce.rb +156 -0
- data/lib/xdk/paginator.rb +41 -0
- data/lib/xdk/streaming.rb +294 -0
- data/lib/xdk/version.rb +9 -0
- data/lib/xdk.rb +62 -0
- metadata +84 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: d84fb93dbd29a7dd822fca405ab6579b27601f9281990072bd92f03e08a839ca
|
|
4
|
+
data.tar.gz: a1bb49ac9a140c4c0f3466451320ca4a7673ed5cb552ff3944d031b45f6a0a52
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: b5b605cb266b074d560db5ddb52b73fbd187480b94ad180ae7ee01c81589b6d52327c3c459b6a0ce295dd1855c1a37fe4338e53334151adc33839522cac88184
|
|
7
|
+
data.tar.gz: 5c97d76d3c9c906035b054202c71c757a779d6357163ca51db97ea9004bf2f865defdd041934bf39e4d6e834a28c34b5dc60fc3a17097bf794d345ebb862d92e
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 XDK Project
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
AUTO-GENERATED FILE - DO NOT EDIT
|
|
3
|
+
This file was automatically generated by the XDK build tool.
|
|
4
|
+
-->
|
|
5
|
+
# XDK for Ruby
|
|
6
|
+
|
|
7
|
+
Official Ruby SDK for the **X API v2** (`v0.1.0`).
|
|
8
|
+
|
|
9
|
+
- **Zero runtime dependencies** (uses Ruby 3.0+ standard library `net/http`, `openssl`, `json`)
|
|
10
|
+
- **Full X API v2 coverage**: Posts, Users, Chat, Media, Lists, Direct Messages, Streaming, Activity, Trends, Communities, and more
|
|
11
|
+
- **Authentication**: Bearer Token (app-only), OAuth 2.0 PKCE (with automatic token refresh), and OAuth 1.0a (HMAC-SHA1)
|
|
12
|
+
- **Pagination**: Lazy Ruby `Enumerator` over `next_token` / `pagination_token`
|
|
13
|
+
- **Streaming**: Real-time NDJSON streaming with keep-alive callbacks, idle-timeout recovery, and exponential backoff
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
gem install xdk
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Or add to your `Gemfile`:
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
gem "xdk"
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Requires **Ruby 3.0+**.
|
|
28
|
+
|
|
29
|
+
## Quick Start
|
|
30
|
+
|
|
31
|
+
### Bearer Token (App-Only)
|
|
32
|
+
|
|
33
|
+
```ruby
|
|
34
|
+
require "xdk"
|
|
35
|
+
|
|
36
|
+
client = Xdk::Client.new(bearer_token: ENV.fetch("X_BEARER_TOKEN"))
|
|
37
|
+
user = client.users.get_by_username(username: "XDevelopers")
|
|
38
|
+
puts user.dig("data", "id")
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### OAuth 2.0 User Context (PKCE)
|
|
42
|
+
|
|
43
|
+
```ruby
|
|
44
|
+
require "xdk"
|
|
45
|
+
|
|
46
|
+
client = Xdk::Client.new(
|
|
47
|
+
client_id: ENV.fetch("X_CLIENT_ID"),
|
|
48
|
+
client_secret: ENV["X_CLIENT_SECRET"],
|
|
49
|
+
redirect_uri: "http://localhost:8080/callback",
|
|
50
|
+
scope: %w[tweet.read users.read dm.read dm.write offline.access]
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
auth_url = client.get_authorization_url(state: "random-state")
|
|
54
|
+
# Redirect user to auth_url, then exchange the returned authorization code:
|
|
55
|
+
token = client.exchange_code(code)
|
|
56
|
+
me = client.users.get_me
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Or pass an existing user access token directly:
|
|
60
|
+
|
|
61
|
+
```ruby
|
|
62
|
+
client = Xdk::Client.new(access_token: ENV.fetch("X_ACCESS_TOKEN"))
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### OAuth 1.0a User Context
|
|
66
|
+
|
|
67
|
+
```ruby
|
|
68
|
+
require "xdk"
|
|
69
|
+
|
|
70
|
+
oauth1 = Xdk::OAuth1.new(
|
|
71
|
+
api_key: ENV.fetch("X_API_KEY"),
|
|
72
|
+
api_secret: ENV.fetch("X_API_SECRET"),
|
|
73
|
+
access_token: ENV.fetch("X_ACCESS_TOKEN"),
|
|
74
|
+
access_token_secret: ENV.fetch("X_ACCESS_TOKEN_SECRET")
|
|
75
|
+
)
|
|
76
|
+
client = Xdk::Client.new(auth: oauth1)
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Pagination
|
|
80
|
+
|
|
81
|
+
Paginated endpoints return a lazy Ruby `Enumerator` (or accept a block directly):
|
|
82
|
+
|
|
83
|
+
```ruby
|
|
84
|
+
client.posts.search_recent(query: "ruby lang:en", max_results: 10).take(3).each do |page|
|
|
85
|
+
(page["data"] || []).each { |post| puts post["text"] }
|
|
86
|
+
end
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Streaming
|
|
90
|
+
|
|
91
|
+
```ruby
|
|
92
|
+
config = Xdk::StreamConfig.new(
|
|
93
|
+
max_retries: 10,
|
|
94
|
+
idle_timeout: 60.0,
|
|
95
|
+
on_keep_alive: -> { puts "heartbeat" },
|
|
96
|
+
on_reconnect: ->(attempt, delay) { warn "reconnecting ##{attempt} in #{delay}s" }
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
client.stream.posts(stream_config: config) do |event|
|
|
100
|
+
puts event.dig("data", "text")
|
|
101
|
+
end
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Pairing with `chat_xdk` for Encrypted X Chat DMs
|
|
105
|
+
|
|
106
|
+
```ruby
|
|
107
|
+
require "xdk"
|
|
108
|
+
require "chat_xdk"
|
|
109
|
+
|
|
110
|
+
api = Xdk::Client.new(access_token: ENV.fetch("X_ACCESS_TOKEN"))
|
|
111
|
+
pub_keys = api.users.get_public_keys(id: recipient_id)["data"]
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Development & Tests
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
bundle install
|
|
118
|
+
bundle exec rake test
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## License
|
|
122
|
+
|
|
123
|
+
MIT
|
data/lib/xdk/client.rb
ADDED
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
# AUTO-GENERATED FILE - DO NOT EDIT
|
|
2
|
+
# This file was automatically generated by the XDK build tool.
|
|
3
|
+
# Any manual changes will be overwritten on the next generation.
|
|
4
|
+
# frozen_string_literal: true
|
|
5
|
+
|
|
6
|
+
require "json"
|
|
7
|
+
require "uri"
|
|
8
|
+
|
|
9
|
+
module Xdk
|
|
10
|
+
# Primary client for interacting with the X API v2.
|
|
11
|
+
class Client
|
|
12
|
+
WRITE_METHODS = %w[POST PUT DELETE PATCH].freeze
|
|
13
|
+
|
|
14
|
+
attr_reader :base_url, :bearer_token, :oauth2_auth, :auth, :http_client
|
|
15
|
+
|
|
16
|
+
attr_reader :bots
|
|
17
|
+
|
|
18
|
+
attr_reader :connections
|
|
19
|
+
|
|
20
|
+
attr_reader :usage
|
|
21
|
+
|
|
22
|
+
attr_reader :trends
|
|
23
|
+
|
|
24
|
+
attr_reader :communities
|
|
25
|
+
|
|
26
|
+
attr_reader :spaces
|
|
27
|
+
|
|
28
|
+
attr_reader :calls
|
|
29
|
+
|
|
30
|
+
attr_reader :community_notes
|
|
31
|
+
|
|
32
|
+
attr_reader :webhooks
|
|
33
|
+
|
|
34
|
+
attr_reader :lists
|
|
35
|
+
|
|
36
|
+
attr_reader :broadcasts
|
|
37
|
+
|
|
38
|
+
attr_reader :media
|
|
39
|
+
|
|
40
|
+
attr_reader :account_activity
|
|
41
|
+
|
|
42
|
+
attr_reader :activity
|
|
43
|
+
|
|
44
|
+
attr_reader :users
|
|
45
|
+
|
|
46
|
+
attr_reader :chat
|
|
47
|
+
|
|
48
|
+
attr_reader :account
|
|
49
|
+
|
|
50
|
+
attr_reader :direct_messages
|
|
51
|
+
|
|
52
|
+
attr_reader :general
|
|
53
|
+
|
|
54
|
+
attr_reader :posts
|
|
55
|
+
|
|
56
|
+
attr_reader :stream
|
|
57
|
+
|
|
58
|
+
attr_reader :compliance
|
|
59
|
+
|
|
60
|
+
attr_reader :articles
|
|
61
|
+
|
|
62
|
+
attr_reader :news
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def initialize(
|
|
66
|
+
base_url: "https://api.x.com",
|
|
67
|
+
bearer_token: nil,
|
|
68
|
+
access_token: nil,
|
|
69
|
+
client_id: nil,
|
|
70
|
+
client_secret: nil,
|
|
71
|
+
redirect_uri: nil,
|
|
72
|
+
token: nil,
|
|
73
|
+
scope: nil,
|
|
74
|
+
authorization_base_url: "https://x.com/i",
|
|
75
|
+
auth: nil,
|
|
76
|
+
oauth2_auth: nil,
|
|
77
|
+
timeout: nil,
|
|
78
|
+
max_retries: 0,
|
|
79
|
+
retry_backoff: 0.5,
|
|
80
|
+
http_adapter: nil
|
|
81
|
+
)
|
|
82
|
+
@base_url = base_url.chomp("/")
|
|
83
|
+
@bearer_token = bearer_token
|
|
84
|
+
@auth = auth
|
|
85
|
+
@http_client = HttpClient.new(
|
|
86
|
+
timeout: timeout,
|
|
87
|
+
max_retries: max_retries,
|
|
88
|
+
retry_backoff: retry_backoff,
|
|
89
|
+
http_adapter: http_adapter
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
@access_token = if token.is_a?(Hash) && (token["access_token"] || token[:access_token])
|
|
93
|
+
token["access_token"] || token[:access_token]
|
|
94
|
+
else
|
|
95
|
+
access_token
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
@oauth2_auth = oauth2_auth
|
|
99
|
+
if @oauth2_auth.nil? && (client_id || token)
|
|
100
|
+
@oauth2_auth = OAuth2PKCEAuth.new(
|
|
101
|
+
base_url: @base_url,
|
|
102
|
+
authorization_base_url: authorization_base_url,
|
|
103
|
+
client_id: client_id,
|
|
104
|
+
client_secret: client_secret,
|
|
105
|
+
redirect_uri: redirect_uri,
|
|
106
|
+
token: token,
|
|
107
|
+
scope: scope
|
|
108
|
+
)
|
|
109
|
+
end
|
|
110
|
+
if @oauth2_auth&.access_token
|
|
111
|
+
@access_token = @oauth2_auth.access_token
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
@bots = Clients::BotsClient.new(self)
|
|
116
|
+
|
|
117
|
+
@connections = Clients::ConnectionsClient.new(self)
|
|
118
|
+
|
|
119
|
+
@usage = Clients::UsageClient.new(self)
|
|
120
|
+
|
|
121
|
+
@trends = Clients::TrendsClient.new(self)
|
|
122
|
+
|
|
123
|
+
@communities = Clients::CommunitiesClient.new(self)
|
|
124
|
+
|
|
125
|
+
@spaces = Clients::SpacesClient.new(self)
|
|
126
|
+
|
|
127
|
+
@calls = Clients::CallsClient.new(self)
|
|
128
|
+
|
|
129
|
+
@community_notes = Clients::CommunityNotesClient.new(self)
|
|
130
|
+
|
|
131
|
+
@webhooks = Clients::WebhooksClient.new(self)
|
|
132
|
+
|
|
133
|
+
@lists = Clients::ListsClient.new(self)
|
|
134
|
+
|
|
135
|
+
@broadcasts = Clients::BroadcastsClient.new(self)
|
|
136
|
+
|
|
137
|
+
@media = Clients::MediaClient.new(self)
|
|
138
|
+
|
|
139
|
+
@account_activity = Clients::AccountActivityClient.new(self)
|
|
140
|
+
|
|
141
|
+
@activity = Clients::ActivityClient.new(self)
|
|
142
|
+
|
|
143
|
+
@users = Clients::UsersClient.new(self)
|
|
144
|
+
|
|
145
|
+
@chat = Clients::ChatClient.new(self)
|
|
146
|
+
|
|
147
|
+
@account = Clients::AccountClient.new(self)
|
|
148
|
+
|
|
149
|
+
@direct_messages = Clients::DirectMessagesClient.new(self)
|
|
150
|
+
|
|
151
|
+
@general = Clients::GeneralClient.new(self)
|
|
152
|
+
|
|
153
|
+
@posts = Clients::PostsClient.new(self)
|
|
154
|
+
|
|
155
|
+
@stream = Clients::StreamClient.new(self)
|
|
156
|
+
|
|
157
|
+
@compliance = Clients::ComplianceClient.new(self)
|
|
158
|
+
|
|
159
|
+
@articles = Clients::ArticlesClient.new(self)
|
|
160
|
+
|
|
161
|
+
@news = Clients::NewsClient.new(self)
|
|
162
|
+
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def token
|
|
166
|
+
@oauth2_auth&.token
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def access_token
|
|
170
|
+
return @oauth2_auth.access_token if @oauth2_auth&.access_token
|
|
171
|
+
|
|
172
|
+
@access_token
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def access_token=(value)
|
|
176
|
+
@access_token = value
|
|
177
|
+
@oauth2_auth.token["access_token"] = value if @oauth2_auth&.token.is_a?(Hash)
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def get_authorization_url(state: nil)
|
|
181
|
+
raise AuthenticationError, "OAuth2 credentials not configured" unless @oauth2_auth
|
|
182
|
+
|
|
183
|
+
@oauth2_auth.get_authorization_url(state: state)
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def exchange_code(code, code_verifier: nil)
|
|
187
|
+
raise AuthenticationError, "OAuth2 credentials not configured" unless @oauth2_auth
|
|
188
|
+
|
|
189
|
+
tok = @oauth2_auth.exchange_code(code, code_verifier: code_verifier)
|
|
190
|
+
@access_token = tok["access_token"] if tok && tok["access_token"]
|
|
191
|
+
tok
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def fetch_token(authorization_response)
|
|
195
|
+
raise AuthenticationError, "OAuth2 credentials not configured" unless @oauth2_auth
|
|
196
|
+
|
|
197
|
+
tok = @oauth2_auth.fetch_token(authorization_response)
|
|
198
|
+
@access_token = tok["access_token"] if tok && tok["access_token"]
|
|
199
|
+
tok
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
def refresh_token
|
|
203
|
+
raise AuthenticationError, "OAuth2 credentials not configured" unless @oauth2_auth
|
|
204
|
+
|
|
205
|
+
tok = @oauth2_auth.refresh_token
|
|
206
|
+
@access_token = tok["access_token"] if tok && tok["access_token"]
|
|
207
|
+
tok
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
def token_expired?
|
|
211
|
+
return true unless @oauth2_auth
|
|
212
|
+
|
|
213
|
+
@oauth2_auth.token_expired?
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
# Select authentication scheme per endpoint security requirements:
|
|
217
|
+
# - Single scheme: use it if available
|
|
218
|
+
# - Multiple schemes: write ops prefer OAuth1 > OAuth2 > Bearer;
|
|
219
|
+
# read ops prefer Bearer > OAuth2 > OAuth1
|
|
220
|
+
# - Empty schemes: unauthenticated endpoint
|
|
221
|
+
def select_auth_scheme(schemes, http_method)
|
|
222
|
+
return nil if schemes.nil? || schemes.empty?
|
|
223
|
+
|
|
224
|
+
has_bearer = !@bearer_token.nil? && !@bearer_token.empty?
|
|
225
|
+
has_oauth2 = !access_token.nil? && !access_token.empty?
|
|
226
|
+
has_oauth1 = !@auth.nil? && !@auth.access_token.nil?
|
|
227
|
+
|
|
228
|
+
selected = if schemes.length == 1
|
|
229
|
+
case schemes.first
|
|
230
|
+
when "BearerToken" then has_bearer ? :bearer_token : nil
|
|
231
|
+
when "OAuth2UserToken" then has_oauth2 ? :oauth2_user_context : nil
|
|
232
|
+
when "UserToken" then has_oauth1 ? :oauth1 : nil
|
|
233
|
+
end
|
|
234
|
+
elsif WRITE_METHODS.include?(http_method.to_s.upcase)
|
|
235
|
+
if schemes.include?("UserToken") && has_oauth1
|
|
236
|
+
:oauth1
|
|
237
|
+
elsif schemes.include?("OAuth2UserToken") && has_oauth2
|
|
238
|
+
:oauth2_user_context
|
|
239
|
+
elsif schemes.include?("BearerToken") && has_bearer
|
|
240
|
+
:bearer_token
|
|
241
|
+
end
|
|
242
|
+
else
|
|
243
|
+
if schemes.include?("BearerToken") && has_bearer
|
|
244
|
+
:bearer_token
|
|
245
|
+
elsif schemes.include?("OAuth2UserToken") && has_oauth2
|
|
246
|
+
:oauth2_user_context
|
|
247
|
+
elsif schemes.include?("UserToken") && has_oauth1
|
|
248
|
+
:oauth1
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
unless selected
|
|
253
|
+
raise AuthenticationError,
|
|
254
|
+
"Authentication required for this endpoint. Accepted schemes: #{schemes.join(', ')}"
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
selected
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
# Build fresh request headers (including Authorization) for one attempt.
|
|
261
|
+
def build_auth_headers(selected_auth, http_method:, url:, query:, base_headers:)
|
|
262
|
+
headers = base_headers.dup
|
|
263
|
+
case selected_auth
|
|
264
|
+
when :oauth1
|
|
265
|
+
full_url = @http_client.append_query(url, query)
|
|
266
|
+
headers["Authorization"] = @auth.build_request_header(
|
|
267
|
+
method: http_method.to_s.upcase,
|
|
268
|
+
url: full_url
|
|
269
|
+
)
|
|
270
|
+
when :bearer_token
|
|
271
|
+
tok = @bearer_token || access_token
|
|
272
|
+
headers["Authorization"] = "Bearer #{tok}" if tok
|
|
273
|
+
when :oauth2_user_context
|
|
274
|
+
refresh_token if @oauth2_auth && token && token_expired?
|
|
275
|
+
tok = access_token
|
|
276
|
+
headers["Authorization"] = "Bearer #{tok}" if tok
|
|
277
|
+
end
|
|
278
|
+
headers
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
# Perform a standard (non-streaming) API request.
|
|
282
|
+
def execute_request(
|
|
283
|
+
method:,
|
|
284
|
+
path:,
|
|
285
|
+
schemes:,
|
|
286
|
+
query: {},
|
|
287
|
+
headers: {},
|
|
288
|
+
body: nil,
|
|
289
|
+
binary_request: false,
|
|
290
|
+
binary_response: false
|
|
291
|
+
)
|
|
292
|
+
url = "#{@base_url}#{path}"
|
|
293
|
+
selected_auth = select_auth_scheme(schemes, method)
|
|
294
|
+
|
|
295
|
+
req_headers = headers.dup
|
|
296
|
+
encoded_body = nil
|
|
297
|
+
unless body.nil?
|
|
298
|
+
if binary_request
|
|
299
|
+
req_headers["Content-Type"] ||= "application/octet-stream"
|
|
300
|
+
encoded_body = body.b
|
|
301
|
+
else
|
|
302
|
+
req_headers["Content-Type"] ||= "application/json"
|
|
303
|
+
payload = body.respond_to?(:to_h) && !body.is_a?(Array) ? body.to_h : body
|
|
304
|
+
encoded_body = payload.is_a?(String) ? payload : JSON.generate(payload)
|
|
305
|
+
end
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
factory = lambda do
|
|
309
|
+
build_auth_headers(
|
|
310
|
+
selected_auth,
|
|
311
|
+
http_method: method,
|
|
312
|
+
url: url,
|
|
313
|
+
query: query,
|
|
314
|
+
base_headers: req_headers
|
|
315
|
+
)
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
response = @http_client.request(
|
|
319
|
+
method: method,
|
|
320
|
+
url: url,
|
|
321
|
+
query: query,
|
|
322
|
+
headers_factory: factory,
|
|
323
|
+
body: encoded_body
|
|
324
|
+
)
|
|
325
|
+
|
|
326
|
+
unless response.status.between?(200, 299)
|
|
327
|
+
raise ApiError.new(status: response.status, body: response.body, headers: response.headers)
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
return response.body.b if binary_response
|
|
331
|
+
return {} if response.status == 204 || response.body.nil? || response.body.empty?
|
|
332
|
+
|
|
333
|
+
JSON.parse(response.body)
|
|
334
|
+
end
|
|
335
|
+
|
|
336
|
+
# Execute a paginated request, returning an Enumerator of response pages.
|
|
337
|
+
def execute_paginated(
|
|
338
|
+
method:,
|
|
339
|
+
path:,
|
|
340
|
+
schemes:,
|
|
341
|
+
pagination_param:,
|
|
342
|
+
initial_token: nil,
|
|
343
|
+
base_query: {},
|
|
344
|
+
headers: {},
|
|
345
|
+
body: nil,
|
|
346
|
+
binary_request: false
|
|
347
|
+
)
|
|
348
|
+
Paginator.paginate(initial_token: initial_token) do |page_token|
|
|
349
|
+
page_query = base_query.dup
|
|
350
|
+
page_query[pagination_param] = page_token if page_token && !page_token.empty?
|
|
351
|
+
execute_request(
|
|
352
|
+
method: method,
|
|
353
|
+
path: path,
|
|
354
|
+
schemes: schemes,
|
|
355
|
+
query: page_query,
|
|
356
|
+
headers: headers,
|
|
357
|
+
body: body,
|
|
358
|
+
binary_request: binary_request,
|
|
359
|
+
binary_response: false
|
|
360
|
+
)
|
|
361
|
+
end
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
# Execute a streaming request, yielding parsed JSON events.
|
|
365
|
+
def execute_stream(
|
|
366
|
+
method:,
|
|
367
|
+
path:,
|
|
368
|
+
schemes:,
|
|
369
|
+
query: {},
|
|
370
|
+
headers: {},
|
|
371
|
+
body: nil,
|
|
372
|
+
stream_config: nil,
|
|
373
|
+
&block
|
|
374
|
+
)
|
|
375
|
+
url = "#{@base_url}#{path}"
|
|
376
|
+
selected_auth = select_auth_scheme(schemes, method)
|
|
377
|
+
|
|
378
|
+
req_headers = { "Accept" => "application/json" }.merge(headers)
|
|
379
|
+
encoded_body = nil
|
|
380
|
+
unless body.nil?
|
|
381
|
+
req_headers["Content-Type"] ||= "application/json"
|
|
382
|
+
payload = body.respond_to?(:to_h) && !body.is_a?(Array) ? body.to_h : body
|
|
383
|
+
encoded_body = payload.is_a?(String) ? payload : JSON.generate(payload)
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
factory = lambda do
|
|
387
|
+
build_auth_headers(
|
|
388
|
+
selected_auth,
|
|
389
|
+
http_method: method,
|
|
390
|
+
url: url,
|
|
391
|
+
query: query,
|
|
392
|
+
base_headers: req_headers
|
|
393
|
+
)
|
|
394
|
+
end
|
|
395
|
+
|
|
396
|
+
# Validate auth eagerly before opening stream.
|
|
397
|
+
factory.call
|
|
398
|
+
|
|
399
|
+
Streaming.stream_with_retry(
|
|
400
|
+
method: method,
|
|
401
|
+
url: url,
|
|
402
|
+
query: query,
|
|
403
|
+
headers_factory: factory,
|
|
404
|
+
body: encoded_body,
|
|
405
|
+
config: stream_config,
|
|
406
|
+
&block
|
|
407
|
+
)
|
|
408
|
+
end
|
|
409
|
+
end
|
|
410
|
+
end
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# AUTO-GENERATED FILE - DO NOT EDIT
|
|
2
|
+
# This file was automatically generated by the XDK build tool.
|
|
3
|
+
# Any manual changes will be overwritten on the next generation.
|
|
4
|
+
# frozen_string_literal: true
|
|
5
|
+
|
|
6
|
+
require "uri"
|
|
7
|
+
|
|
8
|
+
module Xdk
|
|
9
|
+
module Clients
|
|
10
|
+
# Auto-generated account activity client for the X API v2.
|
|
11
|
+
class AccountActivityClient
|
|
12
|
+
def initialize(client)
|
|
13
|
+
@client = client
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Validate Subscription
|
|
17
|
+
def validate_subscription(webhook_id:)
|
|
18
|
+
path = +"/2/account_activity/webhooks/{webhook_id}/subscriptions/all"
|
|
19
|
+
path.gsub!("{webhook_id}", URI.encode_www_form_component(webhook_id.to_s))
|
|
20
|
+
|
|
21
|
+
query = {}
|
|
22
|
+
|
|
23
|
+
headers = {}
|
|
24
|
+
@client.execute_request(
|
|
25
|
+
method: "GET",
|
|
26
|
+
path: path,
|
|
27
|
+
schemes: ["OAuth2UserToken", "UserToken"],
|
|
28
|
+
query: query,
|
|
29
|
+
headers: headers,
|
|
30
|
+
binary_response: false
|
|
31
|
+
)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Create Subscription
|
|
35
|
+
def create_subscription(webhook_id:, body:)
|
|
36
|
+
path = +"/2/account_activity/webhooks/{webhook_id}/subscriptions/all"
|
|
37
|
+
path.gsub!("{webhook_id}", URI.encode_www_form_component(webhook_id.to_s))
|
|
38
|
+
|
|
39
|
+
query = {}
|
|
40
|
+
|
|
41
|
+
headers = {}
|
|
42
|
+
@client.execute_request(
|
|
43
|
+
method: "POST",
|
|
44
|
+
path: path,
|
|
45
|
+
schemes: ["OAuth2UserToken", "UserToken"],
|
|
46
|
+
query: query,
|
|
47
|
+
headers: headers,
|
|
48
|
+
body: body,
|
|
49
|
+
binary_request: false,
|
|
50
|
+
binary_response: false
|
|
51
|
+
)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Get Subscription Count
|
|
55
|
+
def get_subscription_count()
|
|
56
|
+
path = +"/2/account_activity/subscriptions/count"
|
|
57
|
+
|
|
58
|
+
query = {}
|
|
59
|
+
|
|
60
|
+
headers = {}
|
|
61
|
+
@client.execute_request(
|
|
62
|
+
method: "GET",
|
|
63
|
+
path: path,
|
|
64
|
+
schemes: ["BearerToken"],
|
|
65
|
+
query: query,
|
|
66
|
+
headers: headers,
|
|
67
|
+
binary_response: false
|
|
68
|
+
)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Delete Subscription
|
|
72
|
+
def delete_subscription(webhook_id:, user_id:)
|
|
73
|
+
path = +"/2/account_activity/webhooks/{webhook_id}/subscriptions/{user_id}/all"
|
|
74
|
+
path.gsub!("{webhook_id}", URI.encode_www_form_component(webhook_id.to_s))
|
|
75
|
+
path.gsub!("{user_id}", URI.encode_www_form_component(user_id.to_s))
|
|
76
|
+
|
|
77
|
+
query = {}
|
|
78
|
+
|
|
79
|
+
headers = {}
|
|
80
|
+
@client.execute_request(
|
|
81
|
+
method: "DELETE",
|
|
82
|
+
path: path,
|
|
83
|
+
schemes: ["BearerToken"],
|
|
84
|
+
query: query,
|
|
85
|
+
headers: headers,
|
|
86
|
+
binary_response: false
|
|
87
|
+
)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Get Subscriptions
|
|
91
|
+
def get_subscriptions(webhook_id:)
|
|
92
|
+
path = +"/2/account_activity/webhooks/{webhook_id}/subscriptions/all/list"
|
|
93
|
+
path.gsub!("{webhook_id}", URI.encode_www_form_component(webhook_id.to_s))
|
|
94
|
+
|
|
95
|
+
query = {}
|
|
96
|
+
|
|
97
|
+
headers = {}
|
|
98
|
+
@client.execute_request(
|
|
99
|
+
method: "GET",
|
|
100
|
+
path: path,
|
|
101
|
+
schemes: ["BearerToken"],
|
|
102
|
+
query: query,
|
|
103
|
+
headers: headers,
|
|
104
|
+
binary_response: false
|
|
105
|
+
)
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|