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

Better api responses - Use smart objects as returned objects for the API #159

Merged
merged 10 commits into from
Oct 16, 2024

Conversation

ignacio-chiazzo
Copy link
Owner

@ignacio-chiazzo ignacio-chiazzo commented Oct 15, 2024

Context

Currently, the API returns a complex object Response that contains data and errors like the following example:

response = client.templates.list(business_id: BUSINESS_ID)
response.data&.templates.map { |r| r.template.name })}

This is verbose and complex to understand (it has been raised multiple times by users). Arguably, it lets handling errors gracefully.

Proposed solution

Replace the response with a resource. Here are some examples:

templates = client.templates.list(business_id: BUSINESS_ID)  # -> returns a Pagination object
templates.record -> [ARRAY<Template>]


client = WhatsappSdk::Api::Client.new("<ACCESS TOKEN>") # replace this with a valid access token

client.phone_numbers.register_number(SENDER_ID, 123456) # register the phone number to uplaod media and send message from.

# send a text and a location
client.messages.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,message: "Hey there! it's Whatsapp Ruby SDK")

client.messages.send_location(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, longitude: -75.6898604, latitude: 45.4192206, name: "Ignacio", address: "My house")

# upload a photo and send it
image = client.media.upload(sender_id: SENDER_ID, file_path: "test/fixtures/assets/whatsapp.png", type: "image/png")image = client.media.get(media_id: uploaded_media.id)
client.messages.send_image(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, image_id: image.id)

# upload a sticker and send it
sticker = client.media.upload(sender_id: SENDER_ID, file_path: "test/fixtures/assets/sticker.webp", type: "image/webp")
client.messages.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, sticker_id: sticker.id)

As you can see the API is much simpler and easier to understand than using Responses and specific objects for the APIs.

Solves #157

@ignacio-chiazzo ignacio-chiazzo marked this pull request as ready for review October 15, 2024 23:59
@ignacio-chiazzo ignacio-chiazzo merged commit 32e7919 into main Oct 16, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant