Skip to content

Commit

Permalink
Merge pull request #1 from euanwm/main
Browse files Browse the repository at this point in the history
Passing Tests & Minor Tweaks
  • Loading branch information
ishantanu authored Jul 6, 2023
2 parents 2253b82 + 7ee7a8a commit 7d18f63
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Rust

on:
push:
branches: '**'
pull_request:
branches: '**'

env:
CARGO_TERM_COLOR: always
APP_KEY: ${{ secrets.APP_KEY }}

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Build
run: cargo build
- name: Run tests
run: cargo test
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# TFL API Wrapper

A rust crate for using the [Transport for London (TFL) API](https://api.tfl.gov.uk).

[Cargo.toml](Cargo.toml)
*Note: This is currently WIP and not ready for use.*

## Installation

Using `cargo`, add this to your project's `Cargo.toml`:
```toml
[dependencies]
tfl-api-wrapper = "0.1.1"
tfl-api-wrapper = "0.1.2"
```

## Implemented APIs
Expand All @@ -30,15 +30,19 @@ Instantiate the `Client` using:

```rust
use tfl_api_wrapper::{Client, RequestBuilder};
let client = Client::new(env::var("APP_KEY").unwrap().into());
let client = Client::new(std::env::var("APP_KEY").unwrap());
```
Here `APP_KEY` could be either `Primary key` or `Secondary key`.

## Example

Get the API version:
```rust
let ver = client.api_version().fetch().await.unwrap();
async fn it_is_version_1() {
use tfl_api_wrapper::{Client, RequestBuilder};
let client = Client::new(std::env::var("APP_KEY").unwrap());
let ver = client.api_version().fetch().await.unwrap();
}
```

## Tests
Expand All @@ -49,7 +53,7 @@ APP_KEY=hjdhajsdas cargo test

## References/Credits
1. Existing tfl wrappers
1. [tfl-api-wrapper](https://github.com/ZackaryH8/tfl-api-wrapper) - NodeJS wrapper for TFL API, made with TypeScript.
2. [tfl-api-wrapper-py](https://github.com/ZackaryH8/tfl-api-wrapper-py) - Python wrapper for TFL API
3. [go-tfl](https://github.com/ZackaryH8/go-tfl) - Go client for TFL API
1. [tfl-api-wrapper](https://github.com/ZackaryH8/tfl-api-wrapper) - NodeJS wrapper for TFL API, made with TypeScript.
2. [tfl-api-wrapper-py](https://github.com/ZackaryH8/tfl-api-wrapper-py) - Python wrapper for TFL API
3. [go-tfl](https://github.com/ZackaryH8/go-tfl) - Go client for TFL API
2. [Adzuna-rs](https://github.com/kamui-fin/adzuna-rs) - For existing wrapper implementation for Adzuna API.
11 changes: 6 additions & 5 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ pub trait RequestBuilder {
.map_err(|e| Error::from_status(e.status().unwrap_or(StatusCode::BAD_REQUEST)))?;

let status = response.status();
println!("{:?}", response);
// todo: implement an appropriate logging solution
// println!("{:?}", response);
if status != StatusCode::OK {
return Err(Error::new(
response.json::<models::ApiException>().await.ok(),
Expand Down Expand Up @@ -80,11 +81,11 @@ create_endpoint!(VersionRequest);
impl RequestBuilder for VersionRequest<'_> {
type Response = models::Version;

fn get_client(&self) -> &Client {
self.client
}

fn get_request_url(&self) -> String {
"/version".into()
}

fn get_client(&self) -> &Client {
self.client
}
}
10 changes: 5 additions & 5 deletions tests/tfl_version.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#[cfg(test)]
mod tests {
use std::env;

use tfl_api_wrapper::{Client, RequestBuilder};

fn get_client() -> Client {
Client::new(env::var("APP_KEY").unwrap())
Client::new(
std::env::var("APP_KEY").unwrap()
)
}

#[tokio::test]
async fn it_is_version_1() {
const VERSION: &str = "master.5754\r\n";
let client = get_client();
let ver = client.api_version().fetch().await.unwrap();
println!("aaa {:?}", ver);
assert_eq!(ver.version, "master.5753\r\n");
assert_eq!(ver.version, VERSION, "Version is not {}", VERSION);
}
}

0 comments on commit 7d18f63

Please sign in to comment.