Rust Client SDK
The OneSignal Rust client is a server OneSignal SDK for Rust. Integrate OneSignal with your backend events, data, and more.
Overview
GitHub repository https://github.com/onesignal/onesignal-cpp-api
Installation
Put the package under your project folder in a directory named onesignal
and add the following to Cargo.toml
under [dependencies]
:
onesignal = { path = "./onesignal" }
Examples
Use the following dependency:
use onesignal::*;
use onesignal::apis::configuration::Configuration;
use onesignal::models::filter_expressions::RelationType;
use onesignal::models::{App, FilterExpressions, Notification, Player, Segment, StringMap, UpdatePlayerTagsRequestBody};
Define the constants. You can find all the values on the dashboard page of your app.
const APP_ID: &str = "<YOUR_APP_ID>";
const APP_KEY_TOKEN: &str = "<YOUR_APP_KEY_TOKEN>";
const USER_KEY_TOKEN: &str = "<YOUR_USER_KEY_TOKEN>";
Creating a configuration object:
fn create_configuration() -> Box<Configuration> {
let mut configuration = apis::configuration::Configuration::new();
configuration.app_key_token = Some(String::from(APP_KEY_TOKEN));
configuration.user_key_token = Some(String::from(USER_KEY_TOKEN));
Box::new(configuration)
}
Creating a notification object:
fn create_notification() -> Box<Notification> {
let mut notification = Notification::new(String::from(APP_ID));
let mut string_map = StringMap::new();
string_map.en = Some(String::from("Rust test notification"));
notification.contents = Some(Box::new(string_map));
notification.is_chrome_web = Some(true);
notification.is_any_web = Some(true);
notification.included_segments = Some(vec![String::from("Subscribed Users")]);
Box::new(notification)
}
Sending a notification:
async fn send_notification() {
// Prepare configuration and the notification objects
let configuration = create_configuration();
let notification = create_notification();
// Send notification to the server
let create_notification_response = apis::default_api::create_notification(&configuration, *notification).await;
// Check the result
if let Ok(ref created_notification) = create_notification_response {
println!("Created notification id: {}", created_notification.id);
}
if let Err(ref created_notification_error) = create_notification_response {
println!("Created notification error: {}", created_notification_error.to_string());
}
}
Canceling a scheduled notification
async fn cancel_scheduled_notification() {
let configuration = create_configuration();
let mut notification = create_notification();
let send_after = Utc::now().checked_add_signed(Duration::seconds(30));
notification.send_after = Some(String::from(send_after.unwrap().to_rfc2822()));
let create_notification_response =
apis::default_api::create_notification(&configuration, *notification.clone()).await;
let cancel_notification_response =
apis::default_api::cancel_notification(
&configuration, APP_ID, &create_notification_response.unwrap().id).await;
assert_ok!(&cancel_notification_response);
assert!(cancel_notification_response.unwrap().success.unwrap());
}
Getting a notification
async fn get_notification() {
let configuration = create_configuration();
let notification = create_notification();
let create_notification_response =
apis::default_api::create_notification(&configuration, *notification.clone()).await;
let get_notification_response =
apis::default_api::get_notification(
&configuration, APP_ID, &create_notification_response.unwrap().id).await;
assert_ok!(&get_notification_response);
assert!(get_notification_response.unwrap().id.unwrap().len() > 0);
}
Getting a list of notifications
async fn get_multiple_notifications() {
let configuration = create_configuration();
// Limit: 100
// Offset: 0
// Kind: 1
let get_notifications_response =
apis::default_api::get_notifications(
&configuration, APP_ID, Some(100), Some(0), Some(1)).await;
assert_ok!(&get_notifications_response);
assert!(get_notifications_response.unwrap().notifications.unwrap().len() > 0);
}
Creating a brand new player model
fn create_player() -> Box<Player> {
let mut player = Box::new(Player::new("Rust Test Player".to_string(), 1));
player.app_id = Some(APP_ID.to_string());
player
}
Creating and deleteing a segment on the server
async fn create_and_get_player() {
let configuration = create_configuration();
let player = create_player();
let create_player_response =
apis::default_api::create_player(&configuration, *player).await;
let get_player_response =
apis::default_api::get_player(
&configuration, APP_ID, &create_player_response.unwrap().id.unwrap(), None).await;
assert_ok!(&get_player_response);
assert!(get_player_response.unwrap().id.len() > 0);
}
Creating and updating a player on the server
async fn update_player() {
let configuration = create_configuration();
let player = create_player();
let mut updated_player = create_player();
updated_player.external_user_id = Some(String::from("test_user"));
let create_player_response =
apis::default_api::create_player(&configuration, *player).await;
let update_player_response =
apis::default_api::update_player(
&configuration, &create_player_response.unwrap().id.unwrap(), *updated_player).await;
assert_ok!(&update_player_response);
assert!(update_player_response.unwrap().success.unwrap());
}
Upfating player tags
async fn update_player_tags() {
let configuration = create_configuration();
let mut updated_player_tags_request_body= UpdatePlayerTagsRequestBody::new();
let tag_value = json!({"test_tag": 1});
updated_player_tags_request_body.tags = Some(tag_value);
let update_player_tags_response =
apis::default_api::update_player_tags(
&configuration,
APP_ID,
"test_user",
Some(updated_player_tags_request_body)).await;
assert_ok!(&update_player_tags_response);
assert!(update_player_tags_response.unwrap().success.unwrap());
}
Getting a list of players
async fn get_multiple_players() {
let configuration = create_configuration();
// Limit: 10
// Offset: 0
let get_players_response =
apis::default_api::get_players(&configuration, APP_ID, Some(10), None).await;
assert_ok!(&get_players_response);
assert!(get_players_response.unwrap().players.unwrap().len() > 0);
}
Creating a segment model
fn create_segment(name: String) -> Box<Segment> {
let mut filter_expressions = FilterExpressions::new(
"session_count".to_string(),
RelationType::Greater_Than);
filter_expressions.value = Some("1".to_string());
let segment = Segment::new(name, vec![filter_expressions]);
Box::new(segment)
}
Creating and deleting a segment on the server
async fn create_and_delete_segment() {
let configuration = create_configuration();
let segment = create_segment("test_segment");
let create_segment_response =
apis::default_api::create_segments(&configuration, APP_ID, Some(*segment)).await;
let delete_segment_response =
apis::default_api::delete_segments(
&configuration, APP_ID, &create_segment_response.unwrap().id.unwrap()).await;
assert_ok!(&delete_segment_response);
assert!(delete_segment_response.unwrap().success.unwrap());
}
Getting an app
async fn get_app() {
let configuration = create_configuration();
let get_app_response =
apis::default_api::get_app(&configuration, APP_ID).await;
assert_ok!(&get_app_response);
assert!(get_app_response.unwrap().id.len() > 0);
}
Updating an app
async fn update_app() {
let configuration = create_configuration();
let mut app = App::new(APP_ID.to_string());
app.site_name = Some("rust_test_changed_name".to_string());
let update_app_response =
apis::default_api::update_app(&configuration, APP_ID, app).await;
assert_ok!(&update_app_response);
assert!(update_app_response.unwrap().id.len() > 0);
}
Getting a list of apps
async fn get_multiple_apps() {
let configuration = create_configuration();
let get_apps_response =
apis::default_api::get_apps(&configuration).await;
assert_ok!(&get_apps_response);
assert!(get_apps_response.unwrap().len() > 0);
}
Getting outcomes
async fn get_outcomes() {
let configuration = create_configuration();
let outcome_names = "os__session_duration.count,os__click.count";
let outcome_time_range = "1d";
let outcome_platforms = "5";
let outcome_attribution = "direct";
let get_outcomes_response =
apis::default_api::get_outcomes(
&configuration,
APP_ID,
outcome_names,
None,
Some(outcome_time_range),
Some(outcome_platforms),
Some(outcome_attribution)).await;
assert_ok!(&get_outcomes_response);
assert!(get_outcomes_response.unwrap().outcomes.unwrap().len() > 0);
}
Documentation for API Endpoints
All URIs are relative to https://onesignal.com/api/v1
Class | Method | HTTP request | Description |
---|---|---|---|
DefaultApi | cancel_notification | DELETE /notifications/{notification_id} | Stop a scheduled or currently outgoing notification |
DefaultApi | create_app | POST /apps | Create an app |
DefaultApi | create_notification | POST /notifications | Create notification |
DefaultApi | create_player | POST /players | Add a device |
DefaultApi | create_segments | POST /apps/{app_id}/segments | Create Segments |
DefaultApi | delete_player | DELETE /players/{player_id} | Delete a user record |
DefaultApi | delete_segments | DELETE /apps/{app_id}/segments/{segment_id} | Delete Segments |
DefaultApi | export_players | POST /players/csv_export?app_id={app_id} | CSV export |
DefaultApi | get_app | GET /apps/{app_id} | View an app |
DefaultApi | get_apps | GET /apps | View apps |
DefaultApi | get_notification | GET /notifications/{notification_id} | View notification |
DefaultApi | get_notification_history | POST /notifications/{notification_id}/history | Notification History |
DefaultApi | get_notifications | GET /notifications | View notifications |
DefaultApi | get_outcomes | GET /apps/{app_id}/outcomes | View Outcomes |
DefaultApi | get_player | GET /players/{player_id} | View device |
DefaultApi | get_players | GET /players | View devices |
DefaultApi | update_app | PUT /apps/{app_id} | Update an app |
DefaultApi | update_player | PUT /players/{player_id} | Edit device |
DefaultApi | update_player_tags | PUT /apps/{app_id}/users/{external_user_id} | Edit tags with external user id |
Documentation For Models
- App
- Button
- DeliveryData
- ExportPlayersRequestBody
- Filter
- FilterExpressions
- FilterNotificationTarget
- GetNotificationRequestBody
- InlineResponse200
- InlineResponse2001
- InlineResponse2002
- InlineResponse2005
- InlineResponse2007
- InlineResponse2008
- InlineResponse201
- InlineResponse400
- InlineResponse4001
- InlineResponse4002
- InlineResponse4003
- InvalidIdentifierError
- Notification
- Notification200Errors
- NotificationAllOf
- NotificationAllOfAndroidBackgroundLayout
- NotificationSlice
- NotificationTarget
- NotificationWithMeta
- NotificationWithMetaAllOf
- Operator
- OutcomeData
- OutcomesData
- PlatformDeliveryData
- Player
- PlayerNotificationTarget
- PlayerSlice
- Purchase
- Segment
- SegmentNotificationTarget
- StringMap
- UpdatePlayerTagsRequestBody
To get access to the crate's generated documentation, use:
cargo doc --open
Updated over 2 years ago