Browse Source

Use an ```ApiResponse``` in a client.

develop
Silvestr Predko 2 years ago
parent
commit
05f33ec1e4
2 changed files with 32 additions and 15 deletions
  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 View File

1
 use common_api::{
1
 use common_api::{
2
-    api::{Data, ExchangeMessage, ParticipantInfo, PublicKeys},
2
+    api::{ApiResponse, Data, ExchangeMessage, ParticipantInfo, PublicKeys},
3
     headers::{SignatureHeader, SIGNATURE_HEADER_NAME},
3
     headers::{SignatureHeader, SIGNATURE_HEADER_NAME},
4
 };
4
 };
5
 
5
 
32
     signer: &Signer,
32
     signer: &Signer,
33
     meet_id: Uuid,
33
     meet_id: Uuid,
34
     participants: HashSet<AccountId>,
34
     participants: HashSet<AccountId>,
35
-) -> Result<Vec<ParticipantInfo>> {
35
+) -> Result<ApiResponse<Vec<ParticipantInfo>>> {
36
     let client = client()?;
36
     let client = client()?;
37
 
37
 
38
     let public_keys = PublicKeys {
38
     let public_keys = PublicKeys {
57
         .and_then(Response::error_for_status)
57
         .and_then(Response::error_for_status)
58
         .map_err(ApiError::from)?;
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
     Ok(infos)
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
     let client = client()?;
73
     let client = client()?;
69
 
74
 
70
     let duration = Duration::from_secs(10);
75
     let duration = Duration::from_secs(10);
85
         .and_then(Response::error_for_status)
90
         .and_then(Response::error_for_status)
86
         .map_err(ApiError::from)?;
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
     Ok(data)
97
     Ok(data)
92
 }
98
 }

+ 18
- 7
web-client/src/lib.rs View File

8
     view_moderator_account,
8
     view_moderator_account,
9
 };
9
 };
10
 
10
 
11
-use common_api::api::{Data, ExchangeMessage};
11
+use common_api::api::{ApiResponse, Data, ExchangeMessage};
12
 use crypto::{decrypt, encrypt, secret};
12
 use crypto::{decrypt, encrypt, secret};
13
 use error::ApiError;
13
 use error::ApiError;
14
 use exchange_client::{exchange, public_keys, receive};
14
 use exchange_client::{exchange, public_keys, receive};
202
             while !participants.is_empty() {
202
             while !participants.is_empty() {
203
                 let infos =
203
                 let infos =
204
                     match public_keys(&handler, &signer, meet_id, participants.clone()).await {
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
                             continue;
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
                 // remove infos that is already processed
218
                 // remove infos that is already processed
258
 
264
 
259
         let get_key = async move {
265
         let get_key = async move {
260
             let meet_id = Uuid::from_str(&meeting_id).map_err(ApiError::from)?;
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
         wasm_bindgen_futures::future_to_promise(async move {
277
         wasm_bindgen_futures::future_to_promise(async move {

Loading…
Cancel
Save