infobip_sdk

Crates.ioinfobip_sdk
lib.rsinfobip_sdk
version0.6.1
sourcesrc
created_at2022-08-16 21:02:17.025393
updated_at2024-04-19 13:37:42.975167
descriptionClient SDK to use the Infobip API with pure Rust.
homepagehttps://www.infobip.com/
repositoryhttps://github.com/infobip-community/infobip-api-rust-sdk
max_upload_size
id646972
size508,378
Infobip DevRel (infobipdev)

documentation

https://www.infobip.com/docs/api

README

Infobip API Rust SDK

Workflow Crates.io Crates.io Downloads Crates.io Minimum Rust Version

Client SDK to use the Infobip API with pure Rust.

This crate enables you to use multiple Infobip communication channels, like SMS, 2FA, WhatsApp, Email, etc. It abstracts the needed HTTP calls, models and validates payloads and models errors. The module structure is divided by communication channel.


๐Ÿ“ก Supported Channels

Currently, we support the following channels:

More channels to be added in the near future!

๐Ÿ” Authentication

To use the library, you'll need to set up an Infobip account. Then you can use your API Key and custom base URL to call the endpoints. You can use the Configuration::from_env_api_key() method to load the configuration from the environment. To do that, set the IB_API_KEY and IB_BASE_URL variables.

๐Ÿ“ฆ Installation

To install the library, run the following command under your project's root directory:

cargo add infobip_sdk

Alternatively, you can add the dependency to your project's Cargo.toml

[dependencies]
infobip_sdk = "<version>"

Replace <version> with the latest (or desired) release of the library. For example 0.5.0.

๐Ÿš€ Usage

To use the library, import the client and channel-specific models. Then create a client and call the associated functions. For example, to send an SMS, you can do this:

use infobip_sdk::model::sms::{Destination, Message, SendRequestBody};
use infobip_sdk::api::sms::SmsClient;
use infobip_sdk::configuration::Configuration;

#[tokio::main]
async fn main() {
    // Build SMS client with configuration from the environment.
    let sms_client = SmsClient::with_configuration(
        // Load IB_API_KEY and IB_BASE_URL environment variables.
        Configuration::from_env_api_key().unwrap()
    );

    // Create a message.
    let message = Message{
        destinations: Some(vec![Destination::new("123456789012")]),
        text: Some("Your message text".to_string()),
        ..Default::default()
    };

    // Create the SendRequestBody instance.
    let request_body = SendRequestBody::new(vec![message]);

    // Send the SMS.
    let response = sms_client.send(request_body).await.unwrap();

    // Do what you want with the response.
    assert_eq!(response.status, reqwest::StatusCode::OK);
    println!("Response body:\n{}", serde_json::to_string(&response.body).unwrap());
}

๐Ÿ‘€ Examples

The best way to learn how to use the library is to look at the official docs.rs documentation, which has simple examples on how to use every endpoint. You can also look at integration tests under the tests directory, which work similarly to how you would use them in a real scenario.

๐Ÿ—’ Notes

Building Payload Models

Structs that represent the models have public fields, so you can either build them with the provided new() functions, with serde_json::from_str(), or with the true constructor. For example, to build a Message instance, you can do this:

let message = Message{
    destinations: Some(vec![Destination::new("123456789012")]),
    text: Some("Your message text".to_string()),
    ..Default::default()
}

or this:

let message: Message = serde_json::from_str(
    r#"
        {
          "destinations": [
            {
              "to": "123456789012"
            }
          ],
          "text": "Your message text"
        }
    "#,
)
.unwrap();

or this:

let destination = Destination {
    message_id: None,
    to: "41793026727".to_string()
};

let message = Message {
    destinations: Some(vec![destination]),
    ..Default::default()
};

Model Validation

Some models have mandatory fields. Optional fields are wrapped in Option Enums. Models also have additional checks to make sure that fields have valid values, when possible. Validation is done automatically when calling an endpoint, or you can call the .validate() method of the model.

Using Features

You can speed up compile time by turning only the needed channels as library features. For example, to only build SMS, add the dependency like this:

infobip_sdk = { version = "0.5", features = ["sms"] }

You can see the complete list of features in the Cargo.toml of the project. Feature names follow channel names.

๐Ÿงก Contributing

If you would like to help this project improve, please check our contributing guide and code of conduct.

โš–๏ธ License

This project is licensed under either of

at your option.

Commit count: 127

cargo fmt