xpx_chain_sdk/api/
query_params.rs

1/*
2 * Copyright 2018 ProximaX Limited. All rights reserved.
3 * Use of this source code is governed by the Apache 2.0
4 * license that can be found in the LICENSE file.
5 */
6
7use serde_json::Value;
8
9#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
10pub enum Order {
11    ASC,
12    DESC,
13}
14
15impl Order {
16    pub fn as_str(self) -> &'static str {
17        match self {
18            Order::ASC => "id",
19            Order::DESC => "-id",
20        }
21    }
22}
23
24#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize)]
25pub enum OrderV2 {
26    ASC,
27    DESC,
28}
29
30impl OrderV2 {
31    pub fn as_str(self) -> &'static str {
32        match self {
33            OrderV2::ASC => "asc",
34            OrderV2::DESC => "desc",
35        }
36    }
37}
38
39#[derive(Clone, Debug, Serialize)]
40pub struct QueryParam {
41    pub name: String,
42    pub value: Value,
43}
44
45/// The `QueryParams` structure describes pagination params for requests.
46#[derive(Clone, Debug, Serialize)]
47pub struct QueryParams {
48    #[serde(skip_serializing_if = "Option::is_none")]
49    pub id: Option<String>,
50    pub page_size: u16,
51    /// Order of transactions.
52    /// DESC. Newer to older.
53    ///  ASC. Older to newer.
54    pub order: Order,
55}
56
57impl QueryParams {
58    pub fn to_query_params(&self) -> Vec<QueryParam> {
59        let binding = serde_json::to_value(&self).unwrap();
60        let object = binding.as_object().unwrap();
61
62        object
63            .into_iter()
64            .map(|(key, value)| QueryParam { name: key.to_string(), value: value.clone() })
65            .collect::<Vec<_>>()
66    }
67}