From d9b85a815811c1b7dd7834c0496454b5e50e5d18 Mon Sep 17 00:00:00 2001 From: kaylezang Date: Thu, 4 Jan 2024 10:47:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20url=E7=9A=84/=E6=A3=80=E6=9F=A5?= =?UTF-8?q?=E5=92=8C=E6=A0=BC=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/lib.rs | 4 ++-- src/utils.rs | 55 +++++++++++++++++++++++++++++++++++++++++++--------- 4 files changed, 50 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b9cfad7..1ea0139 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -951,7 +951,7 @@ dependencies = [ [[package]] name = "teemo" -version = "0.2.1" +version = "0.2.2" dependencies = [ "async-recursion", "base64", diff --git a/Cargo.toml b/Cargo.toml index 3440e81..6d49688 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "teemo" -version = "0.2.1" +version = "0.2.2" edition = "2021" authors = ["urnotzane "] description = "A League of Legends API wrapper for Rust" diff --git a/src/lib.rs b/src/lib.rs index 1a01b3e..0cfd402 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -109,7 +109,7 @@ impl Teemo { url: &str, data: Option>, ) -> HashMap { - let full_url = format!("{}{}", self.url, url); + let full_url = utils::transform_full_url(self.url.clone(), url); request::send(method, &full_url, data).await } @@ -123,7 +123,7 @@ impl Teemo { data: Option>, ) -> HashMap { let live_url = "https://127.0.0.1:2999/"; - let full_url = format!("{}{}", live_url, url); + let full_url = utils::transform_full_url(Url::parse(live_url).unwrap(), url); request::send(method, &full_url, data).await } diff --git a/src/utils.rs b/src/utils.rs index e98e40d..eb88e33 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,4 @@ -use crate::{EventBody, EventCallback, EventType, EventTasks}; +use crate::{EventBody, EventCallback, EventTasks, EventType}; use futures_util::stream::{SplitSink, SplitStream}; use futures_util::{SinkExt, StreamExt}; use serde_json::Value; @@ -8,6 +8,7 @@ use std::{collections::HashMap, process::Command}; use tokio::net::TcpStream; use tokio::sync::mpsc::Receiver; use tokio_tungstenite::{tungstenite::Message, MaybeTlsStream, WebSocketStream}; +use url::Url; #[test] fn it_works() { @@ -24,12 +25,35 @@ fn it_works() { assert_res ); // For `fn format_event_type` - assert_eq!(format_event_type("/lol-summoner", EventType::Subscribe), "[5,\"OnJsonApiEvent_lol-summoner\"]"); - assert_eq!(format_event_type("Exit", EventType::Subscribe), "[5,\"Exit\"]"); + assert_eq!( + format_event_type("/lol-summoner", EventType::Subscribe), + "[5,\"OnJsonApiEvent_lol-summoner\"]" + ); + assert_eq!( + format_event_type("Exit", EventType::Subscribe), + "[5,\"Exit\"]" + ); // For `fn revert_event_type` - assert_eq!(revert_event_type("OnJsonApiEvent_lol-lobby_v2_lobby".to_string()), "/lol-lobby/v2/lobby".to_string()); - assert_eq!(revert_event_type("OnJsonApiEvent".to_string()), "OnJsonApiEvent".to_string()); - assert_eq!(revert_event_type("OnServiceProxyAsyncEvent".to_string()), "OnServiceProxyAsyncEvent".to_string()); + assert_eq!( + revert_event_type("OnJsonApiEvent_lol-lobby_v2_lobby".to_string()), + "/lol-lobby/v2/lobby".to_string() + ); + assert_eq!( + revert_event_type("OnJsonApiEvent".to_string()), + "OnJsonApiEvent".to_string() + ); + assert_eq!( + revert_event_type("OnServiceProxyAsyncEvent".to_string()), + "OnServiceProxyAsyncEvent".to_string() + ); + assert_eq!( + transform_full_url(Url::parse("https://teemo.com").unwrap(), "get_name"), + String::from("https://teemo.com/get_name") + ); + assert_eq!( + transform_full_url(Url::parse("https://teemo.com").unwrap(), "/get_name"), + String::from("https://teemo.com/get_name") + ); } #[cfg(windows)] @@ -90,14 +114,27 @@ pub fn format_event_type(event: &str, event_type: EventType) -> String { pub fn revert_event_type(event: String) -> String { let delimiter_count = event.matches("_").count(); - let event_uri = event.replacen("OnJsonApiEvent", "", 1) + let event_uri = event + .replacen("OnJsonApiEvent", "", 1) .replacen("_", "/", delimiter_count); - + if event_uri.len() < 1 { return event; } event_uri } +/// To check if the incoming URL has a / and remove it if present. +pub fn transform_full_url(base_url: Url, url: &str) -> String { + let url_string = url.to_string(); + let mut url_chars = url_string.chars(); + let first_char = url_chars.next().unwrap(); + let mut res_url = url_string; + + if first_char.to_string().as_str() == "/" { + res_url.remove(0); + } + format!("{}{}", base_url, res_url) +} pub(crate) async fn lcu_ws_sender( mut writer: SplitSink>, Message>, @@ -145,7 +182,7 @@ pub(crate) async fn lcu_ws_receiver( Err(err) => { println!("LCU websocket connection failed: {:?}", err); break; - }, + } }; if msg.is_text() || msg.is_binary() { // msg为空时表示订阅LCU事件成功