Git Product home page Git Product logo

lta-rs's Introduction

lta-rs

๐Ÿš Singapore LTA Datamall async first Rust client. lta-rs is used to interact with lta-datamall

lta-rs in action

Cargo.toml setup

[dependencies]
# extra features available: blocking
lta = { version = "0.5.1" }

API key setup

You can get your API key from here

use lta::{LTAResult, LTAClient, Client, Traffic, TrafficRequests};

#[tokio::main]
async fn main() -> LTAResult<()> {
    let api_key = std::env::var("API_KEY").expect("API_KEY not found!");
    let client = LTAClient::with_api_key(api_key)?;
    let erp_rates = Traffic::get_erp_rates(&client, None).await?;
    println!("{:?}", erp_rates);
    Ok(())
}

Examples

Getting bus timings
use lta::{LTAResult, LTAClient, Client, Bus, BusRequests};

fn get_bus_arrival() -> LTAResult<()> {
    let api_key = std::env::var("API_KEY").expect("API_KEY not found!");
    let client = LTAClient::with_api_key(api_key);
    let arrivals = Bus::get_arrival(&client, 83139, None)?;
    println!("{:?}", arrivals);
    Ok(())
}
Getting other data
// All the APIs in this library are designed to be used like this
// `lta::RequestType::get_something`
// All of them return lta::utils::LTAResult<T>
// The example below is Bus::get_bus_services()
// and Traffic::get_erp_rates()
// Do note that the API calling convention is similar across all the APIs except for
// bus::get_arrival
// Most of the APIs returns only 500 record
// If you want to get records 501 - 1000 take a look at get_erp() example
use lta::{LTAResult, LTAClient, Client, Bus, Traffic, BusRequests, TrafficRequests};

async fn bus_services() -> LTAResult<()> {
    let api_key = std::env::var("API_KEY").expect("API_KEY not found!");
    let client = LTAClient::with_api_key(api_key)?;
    let bus_services= Bus::get_bus_services(&client, None)?;
    println!("{:?}", bus_services);
    Ok(())
}

async fn get_erp() -> LTAResult<()> {
    let api_key = std::env::var("API_KEY").expect("API_KEY not found!");
    let client = LTAClient::with_api_key(api_key)?;
    let erp_rates = Traffic:: get_erp_rates(&client, Some(500))?;
    println!("{:?}", erp_rates);
    Ok(())
}

Custom Client

There are some instances where you might need to customise the reqwest client due to certain limitations.
use lta::r#async::client::LTAClient;
use lta::reqwest::ClientBuilder;
use std::time::Duration;
use lta::Client;

fn my_custom_client() -> LTAClient {
    let client = ClientBuilder::new()
        .no_gzip()
        .connect_timeout(Duration::new(420, 0))
        .build()
        .unwrap();

    LTAClient::new("API_KEY", client)
}

General advice

  • Reuse LTAClient as it holds a connection pool internally
  • Reduce the number of times you call the API, take a look at Update Freq in the documentation and prevent yourself from getting blacklisted. Use a caching mechanism.

Getting help

  • You can get help via GitHub issues. I will try my best to respond to your queries ๐Ÿ˜„

Design decisions

  • Made sure that Rust structs are as close to the original response as possible so that people can reference the original docs if there are any issues
  • Simple and no additional baggage. Only the client is included. E.g If anyone wants to add concurrency, they have to do it on their own
  • Predictable API usage

Changelog

Changelog can be found here

Requirements

  • Rust compiler 1.46

License

lta-rs is licensed under MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)

Frequently Asked Questions

Is this library being actively developed?

Yes. However, development will slow down from mid August 2019 onwards due to my NS commitments.

What are the APIs available?

Take a look at the official LTA docs.

Where do I get the official docs from lta?

You can get them here

lta-rs's People

Contributors

euwbah avatar zeon256 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.