Skip to content

Commit

Permalink
Merge pull request #123 from ignacio-chiazzo/use-multipart-only-when-…
Browse files Browse the repository at this point in the history
…needed

Send multipart Faraday only when needed
  • Loading branch information
ignacio-chiazzo authored Feb 11, 2024
2 parents 9a92214 + 7855efa commit 081ff54
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 15 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Unreleased
- Validate Vertical on BusinessProfile update API. @ignacio-chiazzo https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/120
- Added ability to specify fields param in the busines profile API. @ignacio-chiazzo https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/119
- Use http multipart only when is needed. @ignacio-chiazzo [#123](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/123)
- Validate Vertical on BusinessProfile update API. @ignacio-chiazzo [#120](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/120)
- Added ability to specify fields param in the busines profile API. @ignacio-chiazzo [#119](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/119)

# v 0.11.0
- Bumped API version to v19. @paulomcnally https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/116
Expand Down
17 changes: 9 additions & 8 deletions lib/whatsapp_sdk/api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ def initialize(access_token)
full_url: T.nilable(String),
http_method: String,
params: T::Hash[T.untyped, T.untyped],
headers: T::Hash[T.untyped, T.untyped]
headers: T::Hash[T.untyped, T.untyped],
multipart: T::Boolean
).returns(T.nilable(T::Hash[T.untyped, T.untyped]))
end
def send_request(endpoint: "", full_url: nil, http_method: "post", params: {}, headers: {})
def send_request(endpoint: "", full_url: nil, http_method: "post", params: {}, headers: {}, multipart: false)
url = full_url || ApiConfiguration::API_URL

faraday_request = T.unsafe(faraday(url))
faraday_request = T.unsafe(faraday(url: url, multipart: multipart))

response = faraday_request.public_send(http_method, endpoint, request_params(params, headers), headers)

Expand Down Expand Up @@ -69,12 +70,12 @@ def request_params(params, headers)
params
end

sig { params(url: String).returns(Faraday::Connection) }
def faraday(url)
sig { params(url: String, multipart: T::Boolean).returns(Faraday::Connection) }
def faraday(url:, multipart: false)
::Faraday.new(url) do |client|
client.request :multipart
client.request :url_encoded
client.adapter ::Faraday.default_adapter
client.request(:multipart) if multipart
client.request(:url_encoded)
client.adapter(::Faraday.default_adapter)
client.headers['Authorization'] = "Bearer #{@access_token}" unless @access_token.nil?
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/whatsapp_sdk/api/medias.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def upload(sender_id:, file_path:, type:)
type: type
}

response = send_request(http_method: "post", endpoint: "#{sender_id}/media", params: params)
response = send_request(http_method: "post", endpoint: "#{sender_id}/media", params: params, multipart: true)

Api::Response.new(
response: response,
Expand Down
3 changes: 2 additions & 1 deletion lib/whatsapp_sdk/api/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ def send_audio(sender_id:, recipient_number:, audio_id: nil, link: nil, message_
response = send_request(
endpoint: endpoint(sender_id),
params: params,
headers: DEFAULT_HEADERS
headers: DEFAULT_HEADERS,
multipart: true
)

Api::Response.new(
Expand Down
9 changes: 7 additions & 2 deletions lib/whatsapp_sdk/api/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ def download_file(url:, content_type_header:, file_path: nil)
@client.download_file(url: url, content_type_header: content_type_header, file_path: file_path)
end

def send_request(endpoint: nil, full_url: nil, http_method: "post", params: {}, headers: {})
def send_request(endpoint: nil, full_url: nil, http_method: "post", params: {}, headers: {}, multipart: false)
@client.send_request(
http_method: http_method, full_url: full_url, endpoint: endpoint, params: params, headers: headers
http_method: http_method,
full_url: full_url,
endpoint: endpoint,
params: params,
headers: headers,
multipart: multipart
)
end
end
Expand Down
1 change: 1 addition & 0 deletions test/whatsapp/api/medias_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def test_upload_media_sends_valid_params
@medias_api.expects(:send_request).with(
http_method: "post",
endpoint: "123/media",
multipart: true,
params: {
messaging_product: "whatsapp",
file: file_part,
Expand Down
4 changes: 3 additions & 1 deletion test/whatsapp/api/messages_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ def test_send_audio_message_with_a_link
type: "audio",
audio: { link: audio_link }
},
multipart: true,
headers: { "Content-Type" => "application/json" }
).returns(valid_response(valid_contacts, valid_messages))

Expand All @@ -245,6 +246,7 @@ def test_send_audio_message_with_an_audio_id
type: "audio",
audio: { id: audio_id }
},
multipart: true,
headers: { "Content-Type" => "application/json" }
).returns(valid_response(valid_contacts, valid_messages))

Expand Down Expand Up @@ -355,7 +357,7 @@ def test_send_document_message_with_a_link
assert_predicate(message_response, :ok?)
end

def test_send_document_message_with_an_document_id
def test_send_document_message_with_a_document_id
document_id = "12_345"
@messages_api.expects(:send_request).with(
endpoint: "123123/messages",
Expand Down

0 comments on commit 081ff54

Please sign in to comment.