Procházet zdrojové kódy

Merge pull request #102 from Relayz-io/silvestr/use-timeout-resp

Use an ```ApiResponse``` in a client.
develop
Predko Silvestr před 2 roky
rodič
revize
518bc4170f
Žádný účet není propojen s e-mailovou adresou tvůrce revize
2 změnil soubory, kde provedl 32 přidání a 15 odebrání
  1. 14
    8
      web-client/src/exchange_client.rs
  2. 18
    7
      web-client/src/lib.rs

+ 14
- 8
web-client/src/exchange_client.rs Zobrazit soubor

@@ -1,5 +1,5 @@
1 1
 use common_api::{
2
-    api::{Data, ExchangeMessage, ParticipantInfo, PublicKeys},
2
+    api::{ApiResponse, Data, ExchangeMessage, ParticipantInfo, PublicKeys},
3 3
     headers::{SignatureHeader, SIGNATURE_HEADER_NAME},
4 4
 };
5 5
 
@@ -32,7 +32,7 @@ pub async fn public_keys(
32 32
     signer: &Signer,
33 33
     meet_id: Uuid,
34 34
     participants: HashSet<AccountId>,
35
-) -> Result<Vec<ParticipantInfo>> {
35
+) -> Result<ApiResponse<Vec<ParticipantInfo>>> {
36 36
     let client = client()?;
37 37
 
38 38
     let public_keys = PublicKeys {
@@ -57,14 +57,19 @@ pub async fn public_keys(
57 57
         .and_then(Response::error_for_status)
58 58
         .map_err(ApiError::from)?;
59 59
 
60
-    let infos =
61
-        serde_json::from_slice::<Vec<ParticipantInfo>>(&verify_response(handler, response).await?)
62
-            .map_err(ApiError::from)?;
60
+    let infos = serde_json::from_slice::<ApiResponse<Vec<ParticipantInfo>>>(
61
+        &verify_response(handler, response).await?,
62
+    )
63
+    .map_err(ApiError::from)?;
63 64
 
64 65
     Ok(infos)
65 66
 }
66 67
 
67
-pub async fn receive(handler: &Handler, signer: &Signer, meet_id: Uuid) -> Result<Data> {
68
+pub async fn receive(
69
+    handler: &Handler,
70
+    signer: &Signer,
71
+    meet_id: Uuid,
72
+) -> Result<ApiResponse<Data>> {
68 73
     let client = client()?;
69 74
 
70 75
     let duration = Duration::from_secs(10);
@@ -85,8 +90,9 @@ pub async fn receive(handler: &Handler, signer: &Signer, meet_id: Uuid) -> Resul
85 90
         .and_then(Response::error_for_status)
86 91
         .map_err(ApiError::from)?;
87 92
 
88
-    let data = serde_json::from_slice::<Data>(&verify_response(handler, response).await?)
89
-        .map_err(ApiError::from)?;
93
+    let data =
94
+        serde_json::from_slice::<ApiResponse<Data>>(&verify_response(handler, response).await?)
95
+            .map_err(ApiError::from)?;
90 96
 
91 97
     Ok(data)
92 98
 }

+ 18
- 7
web-client/src/lib.rs Zobrazit soubor

@@ -8,7 +8,7 @@ pub use contract::{
8 8
     view_moderator_account,
9 9
 };
10 10
 
11
-use common_api::api::{Data, ExchangeMessage};
11
+use common_api::api::{ApiResponse, Data, ExchangeMessage};
12 12
 use crypto::{decrypt, encrypt, secret};
13 13
 use error::ApiError;
14 14
 use exchange_client::{exchange, public_keys, receive};
@@ -202,11 +202,17 @@ impl KeyProvisioner {
202 202
             while !participants.is_empty() {
203 203
                 let infos =
204 204
                     match public_keys(&handler, &signer, meet_id, participants.clone()).await {
205
-                        Ok(infos) => infos,
206
-                        Err(err) => {
207
-                            warn!("Failed to fetch a public keys, cause {err:?}");
205
+                        Ok(ApiResponse::Success(infos)) => infos,
206
+                        Ok(ApiResponse::Timeout) => {
207
+                            warn!("Timeout happens on a server-side, subscribed one more time");
208 208
                             continue;
209 209
                         }
210
+                        Err(err) => {
211
+                            return Err(ApiError::KeyExchange(format!(
212
+                                "The send keys operation has been failed, cause {err:?}"
213
+                            ))
214
+                            .into());
215
+                        }
210 216
                     };
211 217
 
212 218
                 // remove infos that is already processed
@@ -258,9 +264,14 @@ impl KeyProvisioner {
258 264
 
259 265
         let get_key = async move {
260 266
             let meet_id = Uuid::from_str(&meeting_id).map_err(ApiError::from)?;
261
-            let data = receive(&handler, &signer, meet_id).await?;
262
-            let secret = decrypt(signer.secret_key(), data.moderator_pk, meet_id, data.data)?;
263
-            Ok(JsValue::from_str(&base64::encode(secret)))
267
+
268
+            loop {
269
+                if let ApiResponse::Success(data) = receive(&handler, &signer, meet_id).await? {
270
+                    let secret =
271
+                        decrypt(signer.secret_key(), data.moderator_pk, meet_id, data.data)?;
272
+                    return Ok(JsValue::from_str(&base64::encode(secret)));
273
+                }
274
+            }
264 275
         };
265 276
 
266 277
         wasm_bindgen_futures::future_to_promise(async move {

Načítá se…
Zrušit
Uložit