Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add support for multiple tasks situation #38

Merged
merged 9 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ env:
CARGO_TERM_COLOR: always

jobs:
build:
name: Compilation/Style/Tests
build_default:
name: Compilation/Style/Tests (Default)
runs-on: ubuntu-latest

steps:
Expand All @@ -24,3 +24,18 @@ jobs:
run: cargo clippy
- name: Format
run: cargo fmt --check

build_multi:
name: Compilation/Style/Tests (Multi)
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Build
run: cargo build --features multi --verbose
- name: Run All Tests
run: cargo test --features multi --verbose
- name: Clippy
run: cargo clippy --features multi
- name: Format
run: cargo fmt --check
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/target
/examples/**/target
Cargo.lock
/.idea
cookie.txt
11 changes: 8 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "webweg"
version = "0.8.0"
version = "0.9.0"
edition = "2021"
description = "An asynchronous API wrapper for UCSD's WebReg course enrollment system."
readme = "README.md"
Expand All @@ -13,8 +13,13 @@ keywords = ["ucsd", "webreg", "enrollment"]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
reqwest = "0.11.0"
url = "2.2.2"
url = "2.4"
thiserror = "1.0"
parking_lot = { version = "0.12", optional = true }

[dev-dependencies]
tokio = { version = "1.17.0", features = ["macros"] }
tokio = { version = "1.17.0", features = ["macros"] }

[features]
default = []
multi = ["parking_lot"]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ You're also able to do things like:

To see some examples, check out the `examples` folder.

## Crate Features
An optional feature that can be enabled is the `multi` feature. By default, the wrapper will assume an environment with a single thread or task. So, if you want to be able to share an instance of the wrapper across multiple threads or tasks, you would have to use a `Mutex`. The problem with using a `Mutex` is that if a request is taking a long time (which isn't unusual), the entire wrapper is locked until the

## Authentication

Expand Down
10 changes: 10 additions & 0 deletions examples/enroll-in-classes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "enroll-in-classes"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
webweg = { path = "../.." }
tokio = { version = "1.17.0", features = ["macros"] }
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use webweg::wrapper::input_types::{AddType, EnrollWaitAdd, GradeOption};
use webweg::wrapper::wrapper_builder::WebRegWrapperBuilder;
use webweg::wrapper::WebRegWrapper;

#[tokio::main(flavor = "current_thread")]
async fn main() {
let wrapper = WebRegWrapperBuilder::new()
let wrapper = WebRegWrapper::builder()
.with_cookies("my cookies here")
.with_default_term("FA23")
.try_build_wrapper()
.unwrap();

Expand All @@ -19,7 +18,8 @@ async fn main() {
.unwrap();

let enroll_result = wrapper
.default_request()
.req("FA23")
.parsed()
.add_section(AddType::DecideForMe, course_to_enroll, true)
.await;

Expand Down
11 changes: 11 additions & 0 deletions examples/multiple-tasks/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "multiple-tasks"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tokio = { version = "1.17.0", features = ["macros"] }
webweg = { path = "../..", features = ["multi"] }
futures = "0.3"
27 changes: 27 additions & 0 deletions examples/multiple-tasks/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::sync::Arc;
use futures::stream::FuturesUnordered;
use futures::StreamExt;
use webweg::wrapper::WebRegWrapper;

#[tokio::main(flavor = "current_thread")]
async fn main() {
let wrapper = Arc::new(WebRegWrapper::builder()
.with_cookies("my cookies here")
.try_build_wrapper()
.unwrap());

const TERMS: [&str; 3] = ["FA23", "S223", "S323"];
let mut tasks = FuturesUnordered::new();
for term in TERMS {
let cloned = wrapper.clone();
tasks.push(tokio::spawn(async move {
cloned.set_cookies(format!("pretend I have cookies for {}", term));
let data = cloned.req(term).parsed().get_course_info("CSE", "100").await;
println!("{data:?}");
}));
}

while let Some(_) = tasks.next().await {
println!("done!");
}
}
10 changes: 10 additions & 0 deletions examples/raw-requests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "raw-requests"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
webweg = { path = "../.." }
tokio = { version = "1.17.0", features = ["macros"] }
19 changes: 5 additions & 14 deletions examples/raw_requests.rs → examples/raw-requests/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use webweg::wrapper::wrapper_builder::WebRegWrapperBuilder;
use webweg::wrapper::WebRegWrapper;

#[tokio::main(flavor = "current_thread")]
async fn main() {
let wrapper = WebRegWrapperBuilder::new()
let wrapper = WebRegWrapper::builder()
.with_cookies("my cookies here")
.with_default_term("FA23")
.try_build_wrapper()
.unwrap();

Expand All @@ -14,24 +13,16 @@ async fn main() {
// `default_request` will by default give you a "parsed" response. A parsed response
// is one that takes the original WebReg response and "cleans" it and then returns
// the cleaned version.
let my_schedule = wrapper.default_request().get_schedule(None).await;
let my_schedule = wrapper.req("FA23").parsed().get_schedule(None).await;
println!("{my_schedule:?}");

// The above few lines are equivalent to...
let my_schedule = wrapper
.make_request()
.build_term_parser()
.get_schedule(None)
.await;
let my_schedule = wrapper.req("FA23").parsed().get_schedule(None).await;

println!("{my_schedule:?}");

// But let's say you want a raw response (not a cleaned one from WebReg)...
let my_raw_schedule = wrapper
.make_request()
.build_term_raw()
.get_schedule(None)
.await;
let my_raw_schedule = wrapper.req("FA23").raw().get_schedule(None).await;

println!("{my_raw_schedule:?}");
}
11 changes: 11 additions & 0 deletions examples/search-classes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "search-classes"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
webweg = { path = "../.." }
tokio = { version = "1.17.0", features = ["macros"] }

Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use webweg::wrapper::input_types::{CourseLevelFilter, SearchRequestBuilder, SearchType};
use webweg::wrapper::wrapper_builder::WebRegWrapperBuilder;
use webweg::wrapper::WebRegWrapper;

#[tokio::main(flavor = "current_thread")]
async fn main() {
let wrapper = WebRegWrapperBuilder::new()
let wrapper = WebRegWrapper::builder()
.with_cookies("my cookies here")
.with_default_term("FA23")
.try_build_wrapper()
.unwrap();

Expand All @@ -24,7 +23,8 @@ async fn main() {
.only_allow_open();

let search_results = wrapper
.default_request()
.req("FA23")
.parsed()
.search_courses(SearchType::Advanced(search))
.await;

Expand Down
10 changes: 10 additions & 0 deletions examples/switch-quarters/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "switch-quarters"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
webweg = { path = "../.." }
tokio = { version = "1.17.0", features = ["macros"] }
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use webweg::wrapper::wrapper_builder::WebRegWrapperBuilder;
use webweg::wrapper::WebRegWrapper;

#[tokio::main(flavor = "current_thread")]
async fn main() {
let wrapper = WebRegWrapperBuilder::new()
let wrapper = WebRegWrapper::builder()
.with_cookies("my cookies here")
.with_default_term("FA23")
.try_build_wrapper()
.unwrap();

// Registers all active terms so we can switch between active quarters.
_ = wrapper.register_all_terms().await;

// Using `default_request` defaults to the default term (FA23)
// Let's get all CSE 100 courses for FA23
let cse100_fa23 = wrapper
.default_request()
.req("FA23")
.parsed()
.get_course_info("CSE", "100")
.await;

Expand All @@ -30,9 +30,8 @@ async fn main() {

// But we can also switch to another active quarter
let cse100_s223 = wrapper
.make_request()
.override_term("S223")
.build_term_parser()
.req("S223")
.parsed()
.get_course_info("CSE", "100")
.await;

Expand Down
Loading