buzzsprout-cli 0.1.1
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 +271 -0
- data/bin/buzzsprout +6 -0
- data/lib/buzzsprout/animation/wordmark.rb +41 -0
- data/lib/buzzsprout/animation.rb +181 -0
- data/lib/buzzsprout/cli.rb +215 -0
- data/lib/buzzsprout/client.rb +306 -0
- data/lib/buzzsprout/color.rb +195 -0
- data/lib/buzzsprout/commands/base.rb +188 -0
- data/lib/buzzsprout/commands/brand_affiliations.rb +98 -0
- data/lib/buzzsprout/commands/chapters.rb +58 -0
- data/lib/buzzsprout/commands/contributors.rb +119 -0
- data/lib/buzzsprout/commands/downloads.rb +139 -0
- data/lib/buzzsprout/commands/episodes.rb +116 -0
- data/lib/buzzsprout/commands/fan_mail.rb +216 -0
- data/lib/buzzsprout/commands/feed_verifications.rb +74 -0
- data/lib/buzzsprout/commands/listings.rb +159 -0
- data/lib/buzzsprout/commands/login.rb +264 -0
- data/lib/buzzsprout/commands/podcasts.rb +85 -0
- data/lib/buzzsprout/commands/podrolls.rb +74 -0
- data/lib/buzzsprout/commands/skills.rb +144 -0
- data/lib/buzzsprout/commands/status.rb +64 -0
- data/lib/buzzsprout/commands/team.rb +75 -0
- data/lib/buzzsprout/commands/transcripts.rb +100 -0
- data/lib/buzzsprout/commands/update.rb +36 -0
- data/lib/buzzsprout/commands/uploads.rb +134 -0
- data/lib/buzzsprout/config.rb +125 -0
- data/lib/buzzsprout/formatter.rb +68 -0
- data/lib/buzzsprout/group_help.rb +74 -0
- data/lib/buzzsprout/oauth.rb +203 -0
- data/lib/buzzsprout/options.rb +155 -0
- data/lib/buzzsprout/podcast_picker.rb +328 -0
- data/lib/buzzsprout/redact.rb +65 -0
- data/lib/buzzsprout/updater.rb +230 -0
- data/lib/buzzsprout/version.rb +5 -0
- data/skills/README.md +62 -0
- data/skills/brand_affiliations/SKILL.md +52 -0
- data/skills/buzzsprout/SKILL.md +111 -0
- data/skills/chapters/SKILL.md +49 -0
- data/skills/contributors/SKILL.md +58 -0
- data/skills/episodes/SKILL.md +61 -0
- data/skills/fan_mail/SKILL.md +53 -0
- data/skills/feed_verifications/SKILL.md +51 -0
- data/skills/insertion_points/SKILL.md +50 -0
- data/skills/listings/SKILL.md +52 -0
- data/skills/podcasts/SKILL.md +57 -0
- data/skills/podrolls/SKILL.md +49 -0
- data/skills/soundbites/SKILL.md +51 -0
- data/skills/stats/SKILL.md +65 -0
- data/skills/team/SKILL.md +10 -0
- data/skills/transcripts/SKILL.md +60 -0
- data/skills/uploads/SKILL.md +68 -0
- metadata +102 -0
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require_relative 'version'
|
|
5
|
+
require_relative 'config'
|
|
6
|
+
require_relative 'redact'
|
|
7
|
+
require_relative 'color'
|
|
8
|
+
require_relative 'options'
|
|
9
|
+
require_relative 'client'
|
|
10
|
+
require_relative 'oauth'
|
|
11
|
+
require_relative 'formatter'
|
|
12
|
+
require_relative 'animation'
|
|
13
|
+
require_relative 'podcast_picker'
|
|
14
|
+
require_relative 'commands/base'
|
|
15
|
+
require_relative 'commands/login'
|
|
16
|
+
require_relative 'commands/status'
|
|
17
|
+
require_relative 'commands/podcasts'
|
|
18
|
+
require_relative 'commands/episodes'
|
|
19
|
+
require_relative 'commands/uploads'
|
|
20
|
+
require_relative 'commands/downloads'
|
|
21
|
+
require_relative 'commands/team'
|
|
22
|
+
require_relative 'commands/contributors'
|
|
23
|
+
require_relative 'commands/brand_affiliations'
|
|
24
|
+
require_relative 'commands/chapters'
|
|
25
|
+
require_relative 'commands/transcripts'
|
|
26
|
+
require_relative 'commands/listings'
|
|
27
|
+
require_relative 'commands/podrolls'
|
|
28
|
+
require_relative 'commands/feed_verifications'
|
|
29
|
+
require_relative 'commands/fan_mail'
|
|
30
|
+
require_relative 'commands/skills'
|
|
31
|
+
require_relative 'updater'
|
|
32
|
+
require_relative 'commands/update'
|
|
33
|
+
|
|
34
|
+
module Buzzsprout
|
|
35
|
+
class CLI
|
|
36
|
+
HELP = <<~HELP
|
|
37
|
+
Buzzsprout CLI #{VERSION} — agent-friendly API client
|
|
38
|
+
|
|
39
|
+
Usage: buzzsprout [command] [args] [options]
|
|
40
|
+
|
|
41
|
+
First run / auth
|
|
42
|
+
login, auth, init Connect in your browser; --api-token for token login
|
|
43
|
+
After success, may offer to install agent skills
|
|
44
|
+
logout Remove ~/.buzzsprout.json
|
|
45
|
+
status, whoami Show whether a token is saved
|
|
46
|
+
update [--check] Git installs: update the CLI and skills. Other installs: report the package-manager command
|
|
47
|
+
|
|
48
|
+
Podcasts & episodes
|
|
49
|
+
podcasts [list]
|
|
50
|
+
podcast use [id] Set the saved podcast (picker if omitted)
|
|
51
|
+
podcasts show <id>
|
|
52
|
+
podcasts update <id> --title "…"
|
|
53
|
+
episodes list [podcast_id]
|
|
54
|
+
episodes show <podcast_id> <episode_id>
|
|
55
|
+
episodes create <podcast_id> --title "…" [--private]
|
|
56
|
+
episodes update|publish|unpublish <podcast_id> <episode_id>
|
|
57
|
+
episode <id> downloads today|yesterday|YYYY-MM-DD
|
|
58
|
+
downloads today|yesterday|YYYY-MM-DD
|
|
59
|
+
uploads start <podcast_id> <episode_id> --file PATH
|
|
60
|
+
uploads complete|abort <podcast_id> <episode_id> <upload_id>
|
|
61
|
+
|
|
62
|
+
More resources
|
|
63
|
+
team list|invite|update|delete <podcast_id> [id]
|
|
64
|
+
contributors list|show|create|update|delete|assign|unassign …
|
|
65
|
+
brand-affiliations list|show|create|update|delete|assign|unassign …
|
|
66
|
+
chapters list|replace <podcast_id> <episode_id>
|
|
67
|
+
transcripts show|upload|delete|high-fidelity|replace-high-fidelity …
|
|
68
|
+
listings [directory] [url|status]
|
|
69
|
+
listings submit-all
|
|
70
|
+
podrolls list|create|update|delete <podcast_id> [id]
|
|
71
|
+
feed-verifications list|create|update|delete <podcast_id> [id]
|
|
72
|
+
fan-mail list|show|publish|unpublish|read|block <podcast_id> [id]
|
|
73
|
+
insertion-points list|create|update|delete <podcast_id> <episode_id> [id]
|
|
74
|
+
soundbites show|create|delete <podcast_id> <episode_id>
|
|
75
|
+
skills [list|path|install --dir DIR]
|
|
76
|
+
|
|
77
|
+
Global options
|
|
78
|
+
--json Print raw JSON
|
|
79
|
+
--body '{…}' JSON request body
|
|
80
|
+
--file PATH Upload a local file
|
|
81
|
+
--token TOKEN Login only; leaks via process list/history — prefer prompt or env
|
|
82
|
+
--podcast ID, --in ID Podcast id for this invocation (skips the picker)
|
|
83
|
+
--dry-run Print the request without sending it
|
|
84
|
+
--no-animation Skip the first-run ASCII animation
|
|
85
|
+
--install-skills After login, install agent skills without prompting
|
|
86
|
+
--no-skills Skip the post-login skills prompt
|
|
87
|
+
--help, -h Show this help
|
|
88
|
+
--version, -v Show version
|
|
89
|
+
|
|
90
|
+
Environment
|
|
91
|
+
BUZZSPROUT_API_TOKEN Token override (never print or commit this)
|
|
92
|
+
BUZZSPROUT_PODCAST_ID Explicit podcast id (skips the picker)
|
|
93
|
+
BUZZSPROUT_NONINTERACTIVE=1 Never open the podcast picker or skills prompt
|
|
94
|
+
BUZZSPROUT_CONFIG Config path (default ~/.buzzsprout.json)
|
|
95
|
+
BUZZSPROUT_SKILLS_DIR Override detected agent skills directories
|
|
96
|
+
BUZZSPROUT_API_BASE API base (default https://www.buzzsprout.com/api)
|
|
97
|
+
BUZZSPROUT_NO_ANIMATION=1
|
|
98
|
+
BUZZSPROUT_NO_UPDATE_CHECK=1 Disable automatic update checks
|
|
99
|
+
BUZZSPROUT_THEME dark|light (intro contrast; auto-detects otherwise)
|
|
100
|
+
NO_COLOR Disable ANSI color
|
|
101
|
+
HELP
|
|
102
|
+
|
|
103
|
+
DOWNLOAD_VERBS = %w[downloads stats].freeze
|
|
104
|
+
|
|
105
|
+
ROUTES = {
|
|
106
|
+
'login' => Commands::Login,
|
|
107
|
+
'auth' => Commands::Login,
|
|
108
|
+
'init' => Commands::Login,
|
|
109
|
+
'logout' => Commands::Logout,
|
|
110
|
+
'status' => Commands::Status,
|
|
111
|
+
'whoami' => Commands::Status,
|
|
112
|
+
'podcasts' => Commands::Podcasts,
|
|
113
|
+
'podcast' => Commands::Podcasts,
|
|
114
|
+
'episodes' => Commands::Episodes,
|
|
115
|
+
'episode' => Commands::Episodes,
|
|
116
|
+
'uploads' => Commands::Uploads,
|
|
117
|
+
'upload' => Commands::Uploads,
|
|
118
|
+
'downloads' => Commands::Downloads,
|
|
119
|
+
'stats' => Commands::Downloads,
|
|
120
|
+
'team' => Commands::Team,
|
|
121
|
+
'contributors' => Commands::Contributors,
|
|
122
|
+
'brand-affiliations' => Commands::BrandAffiliations,
|
|
123
|
+
'brand_affiliations' => Commands::BrandAffiliations,
|
|
124
|
+
'brands' => Commands::BrandAffiliations,
|
|
125
|
+
'chapters' => Commands::Chapters,
|
|
126
|
+
'transcripts' => Commands::Transcripts,
|
|
127
|
+
'listings' => Commands::Listings,
|
|
128
|
+
'podrolls' => Commands::Podrolls,
|
|
129
|
+
'feed-verifications' => Commands::FeedVerifications,
|
|
130
|
+
'feed_verifications' => Commands::FeedVerifications,
|
|
131
|
+
'fan-mail' => Commands::FanMail,
|
|
132
|
+
'fan_mail' => Commands::FanMail,
|
|
133
|
+
'insertion-points' => Commands::InsertionPoints,
|
|
134
|
+
'insertion_points' => Commands::InsertionPoints,
|
|
135
|
+
'soundbites' => Commands::Soundbites,
|
|
136
|
+
'soundbite' => Commands::Soundbites,
|
|
137
|
+
'skills' => Commands::Skills,
|
|
138
|
+
'update' => Commands::Update
|
|
139
|
+
}.freeze
|
|
140
|
+
|
|
141
|
+
def initialize(io: $stdout, err: $stderr, stdin: $stdin)
|
|
142
|
+
@io = io
|
|
143
|
+
@err = err
|
|
144
|
+
@stdin = stdin
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def run(argv)
|
|
148
|
+
args = argv.dup
|
|
149
|
+
if args.empty?
|
|
150
|
+
return first_run_or_help
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
if %w[help --help -h].include?(args.first)
|
|
154
|
+
@io.puts HELP
|
|
155
|
+
return 0
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
if %w[version --version -v].include?(args.first)
|
|
159
|
+
@io.puts "buzzsprout-cli #{VERSION}"
|
|
160
|
+
return 0
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
options = Options.parse!(args)
|
|
164
|
+
command = args.shift
|
|
165
|
+
if %w[episode episodes].include?(command) && args.any? { |arg| DOWNLOAD_VERBS.include?(arg) }
|
|
166
|
+
return run_episode_downloads(args, options)
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
klass = ROUTES[command]
|
|
170
|
+
unless klass
|
|
171
|
+
@err.puts "Unknown command: #{command}"
|
|
172
|
+
@io.puts HELP
|
|
173
|
+
return 1
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
status = klass.new(args, options, io: @io, err: @err, stdin: @stdin).run
|
|
177
|
+
status.nil? ? 0 : status
|
|
178
|
+
rescue PodcastPicker::Cancelled => e
|
|
179
|
+
@err.puts Redact.string(e.message)
|
|
180
|
+
1
|
|
181
|
+
rescue Client::Unauthorized => e
|
|
182
|
+
@err.puts "Error: #{Redact.string(e.message)}"
|
|
183
|
+
@err.puts "Run 'buzzsprout login' to save a new token."
|
|
184
|
+
1
|
|
185
|
+
rescue StandardError => e
|
|
186
|
+
@err.puts "Error: #{Redact.string(e.message)}"
|
|
187
|
+
1
|
|
188
|
+
ensure
|
|
189
|
+
Updater.new.notify(io: @err) unless argv.include?('update') || argv.include?('--dry-run')
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
private
|
|
193
|
+
|
|
194
|
+
def run_episode_downloads(args, options)
|
|
195
|
+
unless args[0].to_s.match?(/\A\d+\z/) && DOWNLOAD_VERBS.include?(args[1].to_s)
|
|
196
|
+
return Commands::Downloads.new([], options, io: @io, err: @err, stdin: @stdin).run
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
episode_id = args.shift
|
|
200
|
+
args.shift
|
|
201
|
+
command = Commands::Downloads.new(args, options, io: @io, err: @err, stdin: @stdin)
|
|
202
|
+
command.episode_id = episode_id
|
|
203
|
+
command.run
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
def first_run_or_help
|
|
207
|
+
unless Config.configured?
|
|
208
|
+
return Commands::Login.new([], Options.new, io: @io, err: @err, stdin: @stdin).run
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
@io.puts HELP
|
|
212
|
+
0
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
end
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'net/http'
|
|
5
|
+
require 'openssl'
|
|
6
|
+
require 'uri'
|
|
7
|
+
require_relative 'version'
|
|
8
|
+
require_relative 'redact'
|
|
9
|
+
|
|
10
|
+
module Buzzsprout
|
|
11
|
+
class Client
|
|
12
|
+
USER_AGENT = "BuzzsproutCLI/#{VERSION}"
|
|
13
|
+
MAX_RETRIES = 5
|
|
14
|
+
OPEN_TIMEOUT = 15
|
|
15
|
+
READ_TIMEOUT = 60
|
|
16
|
+
|
|
17
|
+
class Error < StandardError
|
|
18
|
+
attr_reader :status, :body, :error_code, :param, :headers
|
|
19
|
+
|
|
20
|
+
def initialize(message, status: nil, body: nil, error_code: nil, param: nil, headers: nil)
|
|
21
|
+
super(message)
|
|
22
|
+
@status = status
|
|
23
|
+
@body = body
|
|
24
|
+
@error_code = error_code
|
|
25
|
+
@param = param
|
|
26
|
+
@headers = headers || {}
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
class NotModified < Error; end
|
|
31
|
+
class Unauthorized < Error; end
|
|
32
|
+
class RateLimited < Error; end
|
|
33
|
+
|
|
34
|
+
Response = Struct.new(:status, :body, :headers, :parsed, :not_modified, keyword_init: true) do
|
|
35
|
+
def success?
|
|
36
|
+
status.between?(200, 299)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def [](key)
|
|
40
|
+
headers[key.to_s.downcase]
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def initialize(token:, auth_scheme: 'Token', token_provider: nil, base_url: nil, dry_run: false, transport: nil, io: $stderr)
|
|
45
|
+
@token = token
|
|
46
|
+
@auth_scheme = auth_scheme
|
|
47
|
+
@token_provider = token_provider
|
|
48
|
+
@base_url = (base_url || Config.api_base_url).to_s.chomp('/')
|
|
49
|
+
@dry_run = dry_run
|
|
50
|
+
@transport = transport
|
|
51
|
+
@io = io
|
|
52
|
+
@etag_store = {}
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def get(path, query: {}, use_etag: true)
|
|
56
|
+
request(:get, path, query: query, use_etag: use_etag)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def post(path, body: nil, query: {}, headers: {}, multipart: nil)
|
|
60
|
+
request(:post, path, body: body, query: query, headers: headers, multipart: multipart)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def put(path, body: nil, headers: {})
|
|
64
|
+
request(:put, path, body: body, headers: headers)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def patch(path, body: nil, multipart: nil)
|
|
68
|
+
request(:patch, path, body: body, multipart: multipart)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def delete(path)
|
|
72
|
+
request(:delete, path)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def put_bytes(url, bytes, headers: {})
|
|
76
|
+
uri = URI(url)
|
|
77
|
+
request_headers = {
|
|
78
|
+
'Content-Length' => bytes.bytesize.to_s,
|
|
79
|
+
'User-Agent' => USER_AGENT
|
|
80
|
+
}
|
|
81
|
+
request_headers.merge!(headers)
|
|
82
|
+
raw = transport.request(:put, uri, request_headers, bytes)
|
|
83
|
+
raise_for_status(raw) unless raw.status.between?(200, 299)
|
|
84
|
+
raw
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def request(method, path, body: nil, query: {}, headers: {}, use_etag: false, multipart: nil)
|
|
88
|
+
uri = build_uri(path, query)
|
|
89
|
+
|
|
90
|
+
if @dry_run
|
|
91
|
+
print_dry_run(method, uri, body, multipart)
|
|
92
|
+
return nil
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
retries = 0
|
|
96
|
+
retried_rotated_token = false
|
|
97
|
+
loop do
|
|
98
|
+
req_headers = default_headers
|
|
99
|
+
req_headers['Content-Type'] = 'application/json; charset=utf-8' if body && multipart.nil?
|
|
100
|
+
req_headers.merge!(headers)
|
|
101
|
+
|
|
102
|
+
encoded_body = if multipart
|
|
103
|
+
encoded, content_type = encode_multipart(multipart)
|
|
104
|
+
req_headers['Content-Type'] = content_type
|
|
105
|
+
encoded
|
|
106
|
+
elsif body.nil?
|
|
107
|
+
nil
|
|
108
|
+
elsif body.is_a?(String)
|
|
109
|
+
body
|
|
110
|
+
else
|
|
111
|
+
JSON.generate(body)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
if use_etag && method == :get && (etag = @etag_store.dig(uri.to_s, :etag))
|
|
115
|
+
req_headers['If-None-Match'] = etag
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
raw = transport.request(method, uri, req_headers, encoded_body)
|
|
119
|
+
|
|
120
|
+
if raw.status == 401 && @token_provider && !retried_rotated_token
|
|
121
|
+
latest_token = @token_provider.call
|
|
122
|
+
if latest_token != @token
|
|
123
|
+
retried_rotated_token = true
|
|
124
|
+
@token = latest_token
|
|
125
|
+
next
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
if raw.status == 429 && retries < MAX_RETRIES
|
|
130
|
+
retries += 1
|
|
131
|
+
sleep backoff_seconds(raw, retries)
|
|
132
|
+
next
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
if raw.status.between?(500, 599) && retries < MAX_RETRIES && method == :get
|
|
136
|
+
retries += 1
|
|
137
|
+
sleep backoff_seconds(raw, retries)
|
|
138
|
+
next
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
if raw.status == 304
|
|
142
|
+
cached = @etag_store.dig(uri.to_s, :body)
|
|
143
|
+
return cached unless cached.nil?
|
|
144
|
+
|
|
145
|
+
raise NotModified.new('Not modified', status: 304, headers: raw.headers)
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
raise_for_status(raw) unless raw.status.between?(200, 299)
|
|
149
|
+
|
|
150
|
+
parsed = parse_body(raw.body)
|
|
151
|
+
if (etag = raw.headers['etag'])
|
|
152
|
+
@etag_store[uri.to_s] = { etag: etag, body: parsed }
|
|
153
|
+
end
|
|
154
|
+
return parsed
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
private
|
|
159
|
+
|
|
160
|
+
def transport
|
|
161
|
+
@transport || NetTransport.new
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def default_headers
|
|
165
|
+
@token = @token_provider.call if @token_provider
|
|
166
|
+
{
|
|
167
|
+
'Authorization' => @auth_scheme == 'Bearer' ? "Bearer #{@token}" : "Token token=#{@token}",
|
|
168
|
+
'User-Agent' => USER_AGENT,
|
|
169
|
+
'Accept' => 'application/json'
|
|
170
|
+
}
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def build_uri(path, query)
|
|
174
|
+
if path.to_s.start_with?('http://', 'https://')
|
|
175
|
+
uri = URI(path)
|
|
176
|
+
else
|
|
177
|
+
suffix = path.to_s.start_with?('/') ? path : "/#{path}"
|
|
178
|
+
uri = URI("#{@base_url}#{suffix}")
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
unless query.nil? || query.empty?
|
|
182
|
+
existing = URI.decode_www_form(uri.query || '')
|
|
183
|
+
query.each do |key, value|
|
|
184
|
+
next if value.nil? || Redact::QUERY_KEYS.match?(key.to_s)
|
|
185
|
+
|
|
186
|
+
existing << [key.to_s, value.to_s]
|
|
187
|
+
end
|
|
188
|
+
uri.query = URI.encode_www_form(existing) unless existing.empty?
|
|
189
|
+
end
|
|
190
|
+
uri
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def parse_body(body)
|
|
194
|
+
return nil if body.nil? || body.empty?
|
|
195
|
+
|
|
196
|
+
JSON.parse(body)
|
|
197
|
+
rescue JSON::ParserError
|
|
198
|
+
body
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
def raise_for_status(raw)
|
|
202
|
+
parsed = parse_error(raw)
|
|
203
|
+
message = Redact.string(parsed[:message], known: @token)
|
|
204
|
+
klass = case raw.status
|
|
205
|
+
when 401 then Unauthorized
|
|
206
|
+
when 429 then RateLimited
|
|
207
|
+
else Error
|
|
208
|
+
end
|
|
209
|
+
raise klass.new(message, status: raw.status, body: raw.body, error_code: parsed[:code], param: parsed[:param], headers: raw.headers)
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
def parse_error(raw)
|
|
213
|
+
data = parse_body(raw.body)
|
|
214
|
+
if data.is_a?(Hash) && data['error'].is_a?(Hash)
|
|
215
|
+
err = data['error']
|
|
216
|
+
message = err['message'] || 'API error'
|
|
217
|
+
message = "#{message} (#{err['param']})" if err['param']
|
|
218
|
+
return { message: "API error #{raw.status}: #{message}", code: err['code'], param: err['param'] }
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
if data.is_a?(Hash) && data.values.any? { |value| value.is_a?(Array) }
|
|
222
|
+
details = data.map { |key, value| "#{key}: #{Array(value).join(', ')}" }.join('; ')
|
|
223
|
+
return { message: "API error #{raw.status}: #{details}", code: 'invalid', param: nil }
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
snippet = raw.body.to_s.strip
|
|
227
|
+
snippet = snippet[0, 400] unless snippet.empty?
|
|
228
|
+
snippet = raw.headers['status'] || 'request failed' if snippet.empty?
|
|
229
|
+
{ message: "API error #{raw.status}: #{snippet}", code: nil, param: nil }
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
def backoff_seconds(raw, retries)
|
|
233
|
+
retry_after = raw.headers['retry-after']
|
|
234
|
+
return retry_after.to_f if retry_after.to_s =~ /\A\d+(\.\d+)?\z/
|
|
235
|
+
|
|
236
|
+
[2**retries, 16].min
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
def print_dry_run(method, uri, body, multipart)
|
|
240
|
+
@io.puts Redact.string("[dry-run] #{method.to_s.upcase} #{Redact.uri(uri, known: @token)}", known: @token)
|
|
241
|
+
@io.puts "[dry-run] Authorization: #{@auth_scheme == 'Bearer' ? 'Bearer ' : 'Token token='}#{Redact::PLACEHOLDER}"
|
|
242
|
+
@io.puts "[dry-run] User-Agent: #{USER_AGENT}"
|
|
243
|
+
if body
|
|
244
|
+
payload = body.is_a?(String) ? body : JSON.pretty_generate(body)
|
|
245
|
+
@io.puts Redact.string("[dry-run] body: #{payload}", known: @token)
|
|
246
|
+
end
|
|
247
|
+
@io.puts "[dry-run] multipart: #{multipart.keys.join(', ')}" if multipart
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
def encode_multipart(parts)
|
|
251
|
+
boundary = "buzzsprout#{Time.now.to_i}#{rand(1_000_000)}"
|
|
252
|
+
chunks = []
|
|
253
|
+
parts.each do |name, value|
|
|
254
|
+
chunks << "--#{boundary}\r\n"
|
|
255
|
+
if value.is_a?(Hash) && value[:filename]
|
|
256
|
+
chunks << "Content-Disposition: form-data; name=\"#{name}\"; filename=\"#{value[:filename]}\"\r\n"
|
|
257
|
+
chunks << "Content-Type: #{value[:content_type] || 'application/octet-stream'}\r\n\r\n"
|
|
258
|
+
chunks << value[:data]
|
|
259
|
+
chunks << "\r\n"
|
|
260
|
+
else
|
|
261
|
+
chunks << "Content-Disposition: form-data; name=\"#{name}\"\r\n\r\n"
|
|
262
|
+
chunks << value.to_s
|
|
263
|
+
chunks << "\r\n"
|
|
264
|
+
end
|
|
265
|
+
end
|
|
266
|
+
chunks << "--#{boundary}--\r\n"
|
|
267
|
+
[chunks.map(&:b).join, "multipart/form-data; boundary=#{boundary}"]
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
class NetTransport
|
|
271
|
+
Raw = Struct.new(:status, :body, :headers, keyword_init: true)
|
|
272
|
+
|
|
273
|
+
def request(method, uri, headers, body)
|
|
274
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
275
|
+
http.use_ssl = uri.scheme == 'https'
|
|
276
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER if http.use_ssl?
|
|
277
|
+
http.open_timeout = OPEN_TIMEOUT
|
|
278
|
+
http.read_timeout = READ_TIMEOUT
|
|
279
|
+
|
|
280
|
+
request = build_request(method, uri)
|
|
281
|
+
headers.each { |key, value| request[key] = value }
|
|
282
|
+
request.body = body unless body.nil?
|
|
283
|
+
|
|
284
|
+
response = http.request(request)
|
|
285
|
+
header_hash = {}
|
|
286
|
+
response.each_header { |key, value| header_hash[key.downcase] = value }
|
|
287
|
+
Raw.new(status: response.code.to_i, body: response.body, headers: header_hash)
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
private
|
|
291
|
+
|
|
292
|
+
def build_request(method, uri)
|
|
293
|
+
klass = case method
|
|
294
|
+
when :get then Net::HTTP::Get
|
|
295
|
+
when :post then Net::HTTP::Post
|
|
296
|
+
when :put then Net::HTTP::Put
|
|
297
|
+
when :patch then Net::HTTP::Patch
|
|
298
|
+
when :delete then Net::HTTP::Delete
|
|
299
|
+
else
|
|
300
|
+
raise ArgumentError, "Unsupported HTTP method: #{method}"
|
|
301
|
+
end
|
|
302
|
+
klass.new(uri.request_uri)
|
|
303
|
+
end
|
|
304
|
+
end
|
|
305
|
+
end
|
|
306
|
+
end
|