Then, an iterator is instantiated to iterate over the available usage records. First, a header indicating the information coming (“Twilio Account Usage”) is printed. However, if the body was retrieved, Serde JSON parses it into an AccountUsage struct: doing so makes the JSON body easier to work with programmatically.įollowing that, the get_total_usage_cost function is called to calculate the total cost of the retrieved usage records.Īt this point, there’s not all that much left, but to print out the retrieved and calculated information to the terminal. If it can’t do that, the code exits, printing the reason why the body could not be extracted to the terminal. Otherwise, the code next attempts to retrieve the response’s body as text. Next up, if the status code of the response was not HTTP 200 (OK), the code panics and exits, printing the error code to the terminal. As requests to Twilio’s API require the password to be supplied, auth_token is passed in as the Some variant of Option. Because of that, the password ( auth_token) needs to be supplied as an Option type. It uses Client’s basic_auth() function to authenticate the request, passing account_sid as the username and auth_token as the password.Īs the second parameter ( password) to basic_auth is an Option type, it allows for authentication both with and without a password. In addition, there are several other paths available these are:Īfter that, a new reqwest::blocking::Client object, client, is initialised and used to make a GET request to the URI defined in request_url. html, the information returned would instead be formatted as CSV or HTML respectively. The page size limits the maximum number of records returned to 20.Īs the URI ends with LastMonth.json, any results returned will be limited to those in the last month and returned in JSON format. request_uri is initialised to store the URI for the usage records endpoint, using captured identifiers to set the Account SID and page size.page_size stores the number of records to be retrieved, or the size of a “single page” of records.If both variables were defined and retrieved, however, then two further variables are defined: page_size and request_uri. If either of them cannot be retrieved, an appropriate error message is printed to the terminal and the application exits. env into the application’s list of environment variables.Īfter that, it attempts to initialise account_sid with your Twilio Account SID and auth_token with your Twilio Auth Token from the environment variables TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN respectively. It uses the dotenv crate to load the variables defined earlier in. print_usage_report(): this iterates over the UsageRecord objects stored in usage_records, printing their start and end date, category, and price, comma-separated.get_total_usage_cost(): this calculates and returns the total account usage cost, by iterating over the UsageRecord objects stored in usage_records and summing the price field.The AccountUsage struct defines two methods: Use reqwest::", account_usage.page_size) The first thing to do is to scaffold a new Rust project using Cargo and change in to the new project directory, by running the commands below. That said, I’m thinking of creating them. The code will make direct API calls and be more verbose than you would likely expect as, at least for the time being, there are no official Twilio Rust crates. You’ll learn how to retrieve your Twilio account usage for the last month, sum up the total cost, and print that information to the terminal. This won’t be a big Rust tutorial, but rather one that’s short, sweet, and mostly straight to the point. A mobile phone number that can receive SMS. If you are new to Twilio, click here to create a free account Rust and Cargo, along with some prior experience with both.To follow along with this tutorial, you will need the following: What’s more, it’s a very safe language with speed approaching that of C. Learning them can help you better appreciate the languages that you already know. This time, I’m going to show you how to do it using Rust.īut why Rust? After all, it can be harder to learn than a number of other languages at least in my experience.īecause I feel that many of Rust’s features – especially borrowing and scoped resource management functionality – make it an extremely compelling language to learn. Recently, I’ve stepped through how to view your Twilio account usage information using Go.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |