Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STATUS commandを叩く際はGET reqを投げないといけないのにそうなっていなかった。 #15

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions lib/simple_tweet/v2_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ def request(req)

def request_with_retry(req:, expected_status_code:, error_kind_message:, retry_count: 3)
res = request(req)
return res if res.code == expected_status_code

return res if expected_status_code === res.code # rubocop:disable Style/CaseEquality
raise UploadMediaError.new(error_kind_message, response: res) unless retry_count.positive?

@client = nil # reset client
sleep 1 << (3 - retry_count)
request_with_retry(
req: req,
Expand Down Expand Up @@ -125,24 +125,24 @@ def finalize(media_id:)
media_id: media_id
)
# finalizeは201が帰ってきてても、processing_infoにretry_afterが入っている場合がある(upload_video中で処理)。
res = request_with_retry(req: req, expected_status_code: "201", error_kind_message: "finalize failed")
res = request_with_retry(req: req, expected_status_code: /^20\d$/, error_kind_message: "finalize failed")
::JSON.parse(res.body)
end

def status(media_id:)
req = ::Net::HTTP::Post::Multipart.new(
TW_MEDIA_UPLOAD_PATH,
command: "STATUS",
media_id: media_id
)
# https://developer.twitter.com/en/docs/twitter-api/v1/media/upload-media/api-reference/get-media-upload-status
# これはGET
uri = ::URI.parse(TW_UPLOAD_ORIGIN + TW_MEDIA_UPLOAD_PATH)
uri.query = ::URI.encode_www_form(command: "STATUS", media_id: media_id)
req = ::Net::HTTP::Get.new(uri)
res = request_with_retry(req: req, expected_status_code: "200", error_kind_message: "status failed")
::JSON.parse(res.body)
end

# https://developer.twitter.com/en/docs/twitter-api/v1/media/upload-media/api-reference/post-media-upload-init
def upload_video(video:)
init_res = init(video: video)
media_id = init_res["media_id_string"]
media_id = init_res["media_id_string"] # : String

chunks_needed = (video.size - 1) / APPEND_PER + 1
chunks_needed.times do |i|
Expand Down
2 changes: 1 addition & 1 deletion sig/simple_tweet.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module SimpleTweet
private
def access_token: (?site: String) -> untyped
def request: (Net::HTTPRequest req) -> Net::HTTPResponse
def request_with_retry: (req: Net::HTTPRequest, expected_status_code: String, error_kind_message: String, ?retry_count: Integer) -> Net::HTTPResponse
def request_with_retry: (req: Net::HTTPRequest, expected_status_code: String | Regexp, error_kind_message: String, ?retry_count: Integer) -> Net::HTTPResponse
def upload_media: (media_type: String, media: untyped) -> [String]
def init: (video: untyped) -> Hash[String, untyped]
def append: (video: untyped, media_id: String, index: untyped) -> Net::HTTPResponse
Expand Down