lib/whatsapp_sdk/api/medias.rb in whatsapp_sdk-0.12.1 vs lib/whatsapp_sdk/api/medias.rb in whatsapp_sdk-0.13.0
- old
+ new
@@ -1,6 +1,5 @@
-# typed: strict
# frozen_string_literal: true
require "faraday"
require "faraday/multipart"
@@ -12,31 +11,23 @@
module WhatsappSdk
module Api
class Medias < Request
class FileNotFoundError < StandardError
- extend T::Sig
-
- sig { returns(String) }
attr_reader :file_path
- sig { params(file_path: String).void }
def initialize(file_path:)
@file_path = file_path
message = "Couldn't find file_path: #{file_path}"
super(message)
end
end
class InvalidMediaTypeError < StandardError
- extend T::Sig
-
- sig { returns(String) }
attr_reader :media_type
- sig { params(media_type: String).void }
def initialize(media_type:)
@media_type = media_type
message = "Invalid Media Type #{media_type}. See the supported types in the official documentation " \
"https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media#supported-media-types."
super(message)
@@ -45,11 +36,10 @@
# Get Media by ID.
#
# @param media_id [String] Media Id.
# @return [Api::Response] Response object.
- sig { params(media_id: String).returns(Api::Response) }
def media(media_id:)
response = send_request(
http_method: "get",
endpoint: "/#{media_id}"
)
@@ -66,11 +56,10 @@
# @param file_path [String] The file_path to download the media e.g. "tmp/downloaded_image.png".
# @param media_type [String] The media type e.g. "audio/mp4". See possible types in the official
# documentation https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media#supported-media-types,
# but note that the API may allow more depending on the client.
# @return [Api::Response] Response object.
- sig { params(url: String, file_path: String, media_type: String).returns(Api::Response) }
def download(url:, file_path:, media_type:)
# Allow download of unsupported media types, since Cloud API may decide to let it through.
# https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/discussions/127
# raise InvalidMediaTypeError.new(media_type: media_type) unless valid_media_type?(media_type)
@@ -95,21 +84,26 @@
# @param file_path [String] Path to the file stored in your local directory. For example: "tmp/whatsapp.png".
# @param type [String] Media type e.g. text/plain, video/3gp, image/jpeg, image/png. For more information,
# see the official documentation https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media#supported-media-types.
#
# @return [Api::Response] Response object.
- sig { params(sender_id: Integer, file_path: String, type: String).returns(Api::Response) }
- def upload(sender_id:, file_path:, type:)
+ def upload(sender_id:, file_path:, type:, headers: {})
raise FileNotFoundError.new(file_path: file_path) unless File.file?(file_path)
params = {
messaging_product: "whatsapp",
file: Faraday::FilePart.new(file_path, type),
type: type
}
- response = send_request(http_method: "post", endpoint: "#{sender_id}/media", params: params, multipart: true)
+ response = send_request(
+ http_method: "post",
+ endpoint: "#{sender_id}/media",
+ params: params,
+ headers: headers,
+ multipart: true
+ )
Api::Response.new(
response: response,
data_class_type: Api::Responses::MediaDataResponse
)
@@ -117,10 +111,9 @@
# Delete a Media by ID.
#
# @param media_id [String] Media Id.
# @return [Api::Response] Response object.
- sig { params(media_id: String).returns(Api::Response) }
def delete(media_id:)
response = send_request(
http_method: "delete",
endpoint: "/#{media_id}"
)