7 unstable releases

0.6.1 Apr 19, 2024
0.5.2 Aug 25, 2023
0.5.1 Jun 15, 2023
0.4.0 Dec 19, 2022
0.3.0 Oct 6, 2022

#37 in HTTP client

26 downloads per month

Apache-2.0 OR MIT

425KB
9K SLoC

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.

Dependencies

~9โ€“27MB
~401K SLoC