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

All Trades endpoint #71

Merged
merged 6 commits into from
May 29, 2024
Merged

All Trades endpoint #71

merged 6 commits into from
May 29, 2024

Conversation

kevin-pease
Copy link
Contributor

✨ What kind of change does this PR introduce? (Bug fix, feature, docs update...)

Begins the implementation of the Trades endpoint group, starting with the All Trades endpoint.
Includes some minor test value updates in the offers endpoint group, in order for the tests to succeed.

💥 Does this PR introduce a breaking change?

No.

🤔 Checklist before submitting

  • All projects build
  • Follows style guide lines
  • Relevant documentation was updated
  • Rebased onto current main

@tluijken tluijken self-requested a review May 14, 2024 13:28
Comment on lines 133 to 162
if let Some(base_asset) = &self.base_asset {
match &base_asset.0 {
AssetType::Native => {
query.push(format!("base_asset_type=native"));
}
AssetType::Alphanumeric4(asset) => {
query.push(format!("base_asset_type=credit_alphanum4"));
query.push(format!("&base_asset_code={}", asset.asset_code));
query.push(format!("&base_asset_issuer={}", asset.asset_issuer));
}
AssetType::Alphanumeric12(asset) => {
query.push(format!("base_asset_type=credit_alphanum12"));
query.push(format!("&base_asset_code={}", asset.asset_code));
query.push(format!("&base_asset_issuer={}", asset.asset_issuer));
}
}
}

if let Some(base_asset) = &self.base_asset {
match &base_asset.0 {
AssetType::Native => {
query.push(format!("&counter_asset_type=native"));
}
AssetType::Alphanumeric4(asset) => {
query.push(format!("&counter_asset_type=credit_alphanum4"));
query.push(format!("&counter_asset_code={}", asset.asset_code));
query.push(format!("&counter_asset_issuer={}", asset.asset_issuer));
}
AssetType::Alphanumeric12(asset) => {
query.push(format!("&counter_asset_type=credit_alphanum12"));
query.push(format!("&counter_asset_code={}", asset.asset_code));
query.push(format!("&counter_asset_issuer={}", asset.asset_issuer));
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is deze code nu duplicaat? De check lijkt hetzelfde, maar de implementatie verschilt licht. Wellicht bedoelde je bij de 2e check geen base_asset maar een counter_asset?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, ja, klopt helemaal wat je zegt.

Comment on lines 129 to 175
impl Request for AllTradesRequest {
fn get_query_parameters(&self) -> String {
let mut query: Vec<String> = Vec::new();

if let Some(base_asset) = &self.base_asset {
match &base_asset.0 {
AssetType::Native => {
query.push(format!("base_asset_type=native"));
}
AssetType::Alphanumeric4(asset) => {
query.push(format!("base_asset_type=credit_alphanum4"));
query.push(format!("&base_asset_code={}", asset.asset_code));
query.push(format!("&base_asset_issuer={}", asset.asset_issuer));
}
AssetType::Alphanumeric12(asset) => {
query.push(format!("base_asset_type=credit_alphanum12"));
query.push(format!("&base_asset_code={}", asset.asset_code));
query.push(format!("&base_asset_issuer={}", asset.asset_issuer));
}
}
}

if let Some(base_asset) = &self.base_asset {
match &base_asset.0 {
AssetType::Native => {
query.push(format!("&counter_asset_type=native"));
}
AssetType::Alphanumeric4(asset) => {
query.push(format!("&counter_asset_type=credit_alphanum4"));
query.push(format!("&counter_asset_code={}", asset.asset_code));
query.push(format!("&counter_asset_issuer={}", asset.asset_issuer));
}
AssetType::Alphanumeric12(asset) => {
query.push(format!("&counter_asset_type=credit_alphanum12"));
query.push(format!("&counter_asset_code={}", asset.asset_code));
query.push(format!("&counter_asset_issuer={}", asset.asset_issuer));
}
}
}
query.join("")
}

fn build_url(&self, base_url: &str) -> String {
format!(
"{}/{}?{}",
base_url,
super::TRADES_PATH,
self.get_query_parameters()
)
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

De code voor het ophalen van de QueryString parameters is redelijk duplicaat, met uitzondering can de counter/base prefix. Dit kun je weer samenvoegen in 1 functie. Bijvoorbeeld zoiets:

impl Request for AllTradesRequest {
    fn add_asset_query(&mut self, query: &mut Vec<String>, asset: &Asset, asset_prefix: &str) {
        let asset_type = match &asset.0 {
            AssetType::Native => "native".to_string(),
            AssetType::Alphanumeric4(asset_data) | AssetType::Alphanumeric12(asset_data) => {
                let asset_type_label = if matches!(asset.0, AssetType::Alphanumeric4(_)) {
                    "credit_alphanum4"
                } else {
                    "credit_alphanum12"
                };
                query.push(format!("&{}_asset_code={}", asset_prefix, asset_data.asset_code));
                query.push(format!("&{}_asset_issuer={}", asset_prefix, asset_data.asset_issuer));
                asset_type_label.to_string()
            }
        };
        query.push(format!("{}_asset_type={}", asset_prefix, asset_type));
    }

    fn get_query_parameters(&self) -> String {
        let mut query = Vec::new();
        if let Some(base_asset) = &self.base_asset {
            self.add_asset_query(&mut query, base_asset, "base");
        }
        if let Some(counter_asset) = &self.counter_asset {
            self.add_asset_query(&mut query, counter_asset, "counter");
        }
        query.join("&").trim_start_matches('&').to_string()
    }

    fn build_url(&self, base_url: &str) -> String {
        format!("{}/{}?{}", base_url, super::TRADES_PATH, self.get_query_parameters())
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dit had ik inderdaad ook gezien. Het gebeurt ook elders in de code, dus misschien kunnen we een stapje verder gaan door er een algemeen iets van te maken?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dat klinkt als een goed idee :D

Comment on lines 32 to 38
pub struct Links {
#[serde(rename = "self")]
self_link: Link,
base: Link,
counter: Link,
operation: Link,
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gaat dit goed met de andere preludes?
Neem bijv AccountResponseLinks. Door een dedicated naam te gebruiken weten we zeker dat we niet meerdere public structs hebben met dezelfde naam (wat via een prelude voor conflicten kan zorgen).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tot nu toe gaat het goed, maar ik kan me voorstellen dat het inderdaad problemen gaat opleveren; ik ga het aanpassen. Misschien is het goed om te kijken of de andere endpoints ook van deze conventie gebuik maken. 🙂

Copy link
Collaborator

@tluijken tluijken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wellicht nog wel mooi om je voorstel mee te nemen om die ene match lus voor base_asset en counter_asset generiek te maken als die elders gebruikt wordt. Mag ook buiten deze PR.

@tluijken
Copy link
Collaborator

Vergeet niet deze branch te rebasen op develop en de conflicten even te fixen.

@tluijken tluijken merged commit 77f5a50 into develop May 29, 2024
2 checks passed
@kevin-pease kevin-pease deleted the 56-trades-endpoint branch June 3, 2024 07:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants