You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

api.rs 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. use borsh::{BorshDeserialize, BorshSerialize};
  2. use near_client::prelude::*;
  3. use serde::{Deserialize, Serialize};
  4. use std::{collections::HashSet, time::Duration};
  5. #[derive(Clone, Debug, Serialize, Deserialize)]
  6. pub struct PublicKeys {
  7. pub timeout: Duration,
  8. pub participants: HashSet<AccountId>,
  9. }
  10. #[derive(Clone, Debug, Serialize, Deserialize)]
  11. pub struct ExchangeMessage {
  12. pub account_id: AccountId,
  13. pub message: Data,
  14. }
  15. #[derive(Clone, Debug, Serialize, Deserialize)]
  16. pub struct Data {
  17. pub moderator_pk: Ed25519PublicKey,
  18. pub data: Vec<u8>,
  19. }
  20. #[derive(Clone, Debug, Serialize, Deserialize)]
  21. pub struct ParticipantInfo {
  22. pub public_key: Ed25519PublicKey,
  23. pub account_id: AccountId,
  24. }
  25. #[derive(Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
  26. pub struct Bandwidth {
  27. pub speed: u32,
  28. pub units: String,
  29. }
  30. #[derive(Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
  31. pub struct NodeInfo {
  32. pub region: String,
  33. pub bandwidth: Bandwidth,
  34. pub account_id: AccountId,
  35. }
  36. #[derive(Debug, Serialize, Deserialize)]
  37. #[serde(tag = "response", content = "body")]
  38. #[serde(rename_all = "snake_case")]
  39. pub enum ApiResponse<T> {
  40. Success(T),
  41. Timeout,
  42. }
  43. #[cfg(test)]
  44. mod tests {
  45. use super::*;
  46. use std::str::FromStr;
  47. #[test]
  48. fn node_info_json() {
  49. let s1 = serde_json::to_value(&NodeInfo {
  50. account_id: AccountId::from_str("account.testnet").unwrap(),
  51. bandwidth: Bandwidth {
  52. speed: 100,
  53. units: "mbps".to_owned(),
  54. },
  55. region: "us".to_owned(),
  56. })
  57. .unwrap();
  58. let s2 = serde_json::json!({
  59. "account_id": "account.testnet",
  60. "region": "us",
  61. "bandwidth": {
  62. "speed": 100,
  63. "units": "mbps"
  64. }
  65. });
  66. assert_eq!(s1, s2);
  67. }
  68. #[test]
  69. fn api_response_parsing() {
  70. let test_data = serde_json::to_value(&ApiResponse::Success(NodeInfo {
  71. account_id: AccountId::from_str("account.testnet").unwrap(),
  72. bandwidth: Bandwidth {
  73. speed: 100,
  74. units: "mbps".to_owned(),
  75. },
  76. region: "us".to_owned(),
  77. }))
  78. .unwrap();
  79. let test_data_match = serde_json::json!({
  80. "response": "success",
  81. "body": {
  82. "account_id": "account.testnet",
  83. "region": "us",
  84. "bandwidth": {
  85. "speed": 100,
  86. "units": "mbps"
  87. }
  88. }
  89. });
  90. assert_eq!(test_data, test_data_match);
  91. let test_data = serde_json::to_value(&ApiResponse::<()>::Timeout).unwrap();
  92. let test_data_match = serde_json::json!({
  93. "response": "timeout",
  94. });
  95. assert_eq!(test_data, test_data_match);
  96. }
  97. }