ergani 0.2.1

Rust SDK for the Greek government Ergani API for employee workflows
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#![allow(unused)]

use core::error;
use std::sync::Arc;

use crate::api_error::{APIError, ErganiError};
use crate::auth::authenticator::{ErganiAuthenticationState, ErganiAuthenticator};
use crate::endpoint::{
    DAILY_SCHEDULE_ENDPOINT, LOOKUP_SUBMISSIONS_ENDPOINT, OVERTIME_ENDPOINT, TRIAL_API_ENDPOINT,
    WEEKLY_SCHEDULE_ENDPOINT, WORK_CARD_ENDPOINT,
};
use crate::ergani_fetch_response::ErganiFetchResponse;
use crate::internal::deserializers::deserialize_datetime;
use crate::models::company::company_daily_schedule::CompanyDailySchedule;
use crate::models::company::company_overtime::CompanyOvertime;
use crate::models::company::company_weekly_schedule::CompanyWeeklySchedule;
use crate::models::company::company_work_card::CompanyWorkCard;
use crate::responses::day_schedule_response::DayScheduleResponseRoot;
use crate::responses::lookup_response::{LookupResponse, LookupRoot};
use crate::responses::overtime_response::OvertimeResponseRoot;
use crate::responses::week_schedule_response::WeekScheduleResponseRoot;
use crate::responses::work_card_response::WorkCardResponseRoot;
use anyhow::{bail, Result};
use bon::Builder;
use chrono::{DateTime, Utc};
use reqwest::header::HeaderValue;
use reqwest::{Method, Request, RequestBuilder, Response, StatusCode};
use serde::de::DeserializeOwned;
use serde::Deserialize;
use serde_json::{json, Value};
use tokio::sync::Mutex;
use tracing::{error, info};

use crate::auth::{self, login_payload};

#[derive(Clone)]
pub struct ErganiClient {
    base_url: String,
    http_client: reqwest::Client,
}

/// Represents a submission response from the Ergani API
/// * - `submission_id` - The unique identifier of the submission
/// * - `protocol` - The protocol associated with the submission
/// * - `submission_date` - The datetime of the submission
#[derive(Deserialize, Debug)]
pub struct SubmissionResponse {
    id: String,
    protocol: String,
    #[serde(rename = "submitDate", deserialize_with = "deserialize_datetime")]
    submit_date: DateTime<Utc>,
}

#[derive(Builder)]
struct ErganiRequestPayload {
    method: Method,
    endpoint: String,
    body: Option<Value>,
}

#[derive(Builder)]
struct ErganiRequestResponse {
    response: Option<Response>,
    auth_state: ErganiAuthenticationState,
}

impl ErganiClient {
    /// A client for interacting with the Ergani API
    /// * - `base_url` - The base URL of the Ergani API. Defaults to <https://trialeservices.yeka.gr/WebServicesAPI/api>.
    pub fn init(base_url: String) -> ErganiClient {
        let client = reqwest::Client::builder()
            .user_agent("Ergani Rust Client")
            .build()
            .unwrap();

        ErganiClient {
            http_client: client,
            base_url,
        }
    }

    /// Submits work card records (check-in, check-out) for employees to the Ergani API
    ///
    /// # Arguments:
    /// * - `company_work_cards[Vec<CompanyWorkCard>]` - A Vec of CompanyWorkCard instances to be submitted
    /// * - `auth_state` - The authentication state of the Ergani API
    ///
    /// # Returns:
    /// * - `[Vec<SubmissionResponse>]` - A list of SubmissionResponse that were parsed from the API response
    ///
    /// # Errors:
    /// * - `[APIError::General]` - An error occurred while communicating with the Ergani API
    /// * - `[APIError::AuthenticationError]` - Raised if there is an authentication error with the Ergani API
    pub async fn submit_work_card(
        &self,
        company_work_cards: Vec<CompanyWorkCard>,
        auth_state: ErganiAuthenticationState,
    ) -> Result<Vec<SubmissionResponse>> {
        let params = serde_json::to_value(company_work_cards)?;

        let request_payload = json!({
            "Cards": {
                "Card": params
            }
        });

        let payload = ErganiRequestPayload::builder()
            .method(Method::POST)
            .endpoint(WORK_CARD_ENDPOINT.to_string())
            .body(request_payload)
            .build();
        let response = self._request(&payload, &auth_state).await?;

        self._extract_submission_result(response.response, auth_state)
            .await
    }

    /// Submits overtime records for employees to the Ergani API
    ///
    /// # Arguments:
    /// * - `company_overtimes[Vec<CompanyOvertime>]` - A Vec of CompanyOvertime instances to be submitted
    /// * - `auth_state` - The authentication state of the Ergani API
    ///
    /// # Returns:
    /// * - `[Vec<SubmissionResponse>]` - A list of SubmissionResponse that were parsed from the API response
    ///
    /// # Errors:
    /// * - `[APIError::General]` - An error occurred while communicating with the Ergani API
    /// * - `[APIError::AuthenticationError]` - Raised if there is an authentication error with the Ergani API
    pub async fn submit_overtime(
        &self,
        company_overtimes: Vec<CompanyOvertime>,
        auth_state: ErganiAuthenticationState,
    ) -> Result<Vec<SubmissionResponse>> {
        let params = serde_json::to_value(company_overtimes)?;
        let request_payload = json!({
            "Overtimes": {
                "Overtime": params
            }
        });

        let payload = ErganiRequestPayload::builder()
            .method(Method::POST)
            .endpoint(OVERTIME_ENDPOINT.to_string())
            .body(request_payload)
            .build();
        let response = self._request(&payload, &auth_state).await?;

        self._extract_submission_result(response.response, auth_state)
            .await
    }

    /// Submits schedule records that are updated on a daily basis for employees to the Ergani API
    ///
    /// # Arguments:
    /// * - `company_daily_schedules[Vec<CompanyDailySchedule>]` - A Vec of CompanyDailySchedule instances to be submitted
    /// * - `auth_state` - The authentication state of the Ergani API
    /// # Returns:
    /// * - `[Vec<SubmissionResponse>]` - A list of SubmissionResponse that were parsed from the API response
    ///
    /// # Errors:
    /// * - `[APIError::General]` - An error occurred while communicating with the Ergani API
    /// * - `[APIError::AuthenticationError]` - Raised if there is an authentication error with the Ergani API
    pub async fn submit_daily_schedule(
        &self,
        company_daily_schedules: Vec<CompanyDailySchedule>,
        auth_state: ErganiAuthenticationState,
    ) -> Result<Vec<SubmissionResponse>> {
        let params = serde_json::to_value(company_daily_schedules)?;
        let request_payload = json!({
            "WTOS": {
                "WTO": params
            }
        });

        let payload = ErganiRequestPayload::builder()
            .method(Method::POST)
            .endpoint(DAILY_SCHEDULE_ENDPOINT.to_string())
            .body(request_payload)
            .build();

        let response = self._request(&payload, &auth_state).await?;

        self._extract_submission_result(response.response, auth_state)
            .await
    }

    /// Submits weekly schedule records for employees to the Ergani API
    ///
    /// # Arguments:
    /// * - `company_weekly_schedules[Vec<CompanyWeeklySchedule>]` - A Vec of CompanyWeeklySchedule instances to be submitted
    /// * - `auth_state` - The authentication state of the Ergani API
    /// # Returns:
    /// * - `[Vec<SubmissionResponse>]` - A list of SubmissionResponse that were parsed from the API response
    ///
    /// # Errors:
    /// * - `[APIError::General]` - An error occurred while communicating with the Ergani API
    /// * - `[APIError::AuthenticationError]` - Raised if there is an authentication error with the Ergani API
    pub async fn submit_weekly_schedule(
        &self,
        company_weekly_schedules: Vec<CompanyWeeklySchedule>,
        auth_state: ErganiAuthenticationState,
    ) -> Result<Vec<SubmissionResponse>> {
        let params = serde_json::to_value(company_weekly_schedules)?;
        let request_payload = json!({
            "WTOS": {
                "WTO": params
            }
        });

        let payload = ErganiRequestPayload::builder()
            .method(Method::POST)
            .endpoint(WEEKLY_SCHEDULE_ENDPOINT.to_string())
            .body(request_payload)
            .build();

        let response = self._request(&payload, &auth_state).await?;

        self._extract_submission_result(response.response, auth_state)
            .await
    }

    /// Fetches the submissions from the Ergani API
    ///
    /// # Arguments:
    /// * - `auth_state` - The authentication state of the Ergani API
    ///
    /// # Returns:
    /// * - `LookupRoot` - The submissions from the Ergani API
    pub async fn fetch_submissions(
        &self,
        auth_state: ErganiAuthenticationState,
    ) -> Result<ErganiFetchResponse<LookupRoot>> {
        let payload = ErganiRequestPayload::builder()
            .method(Method::GET)
            .endpoint(LOOKUP_SUBMISSIONS_ENDPOINT.to_string())
            .maybe_body(None)
            .build();

        let response = self._request(&payload, &auth_state).await?;

        self._extract_fetch_result(response.response, auth_state)
            .await
    }

    /// Fetches the weekly schedule from the Ergani API
    ///
    /// # Arguments:
    /// * - `auth_state` - The authentication state of the Ergani API
    ///
    /// # Returns:
    /// * - `WeekScheduleResponseRoot` - The weekly schedule from the Ergani API
    pub async fn fetch_weekly_schedule(
        &self,
        auth_state: ErganiAuthenticationState,
    ) -> Result<ErganiFetchResponse<WeekScheduleResponseRoot>> {
        let payload = ErganiRequestPayload::builder()
            .method(Method::GET)
            .endpoint(WEEKLY_SCHEDULE_ENDPOINT.to_string())
            .maybe_body(None)
            .build();
        let response = self._request(&payload, &auth_state).await?;

        self._extract_fetch_result(response.response, auth_state)
            .await
    }

    /// Fetches the daily schedule from the Ergani API
    ///
    /// # Arguments:
    /// * - `auth_state` - The authentication state of the Ergani API
    ///
    /// # Returns:
    /// * - `DayScheduleResponseRoot` - The daily schedule from the Ergani API
    pub async fn fetch_daily_schedule(
        &self,
        auth_state: ErganiAuthenticationState,
    ) -> Result<ErganiFetchResponse<DayScheduleResponseRoot>> {
        let payload = ErganiRequestPayload::builder()
            .method(Method::GET)
            .endpoint(DAILY_SCHEDULE_ENDPOINT.to_string())
            .maybe_body(None)
            .build();
        let response = self._request(&payload, &auth_state).await?;

        self._extract_fetch_result(response.response, auth_state)
            .await
    }

    /// Fetches the work cards from the Ergani API
    ///
    /// # Arguments:
    /// * - `auth_state` - The authentication state of the Ergani API
    ///
    /// # Returns:
    /// * - `WorkCardResponseRoot` - The work cards from the Ergani API
    pub async fn fetch_work_cards(
        &self,
        auth_state: ErganiAuthenticationState,
    ) -> Result<ErganiFetchResponse<WorkCardResponseRoot>> {
        let payload = ErganiRequestPayload::builder()
            .method(Method::GET)
            .endpoint(WORK_CARD_ENDPOINT.to_string())
            .maybe_body(None)
            .build();
        let response = self._request(&payload, &auth_state).await?;

        self._extract_fetch_result(response.response, auth_state)
            .await
    }

    /// Fetches the overtime records from the Ergani API
    ///
    /// # Arguments:
    /// * - `auth_state` - The authentication state of the Ergani API
    ///
    /// # Returns:
    /// * - `OvertimeResponseRoot` - The overtime records from the Ergani API
    pub async fn fetch_overtimes(
        &self,
        auth_state: ErganiAuthenticationState,
    ) -> Result<ErganiFetchResponse<OvertimeResponseRoot>> {
        let payload = ErganiRequestPayload::builder()
            .method(Method::GET)
            .endpoint(OVERTIME_ENDPOINT.to_string())
            .maybe_body(None)
            .build();
        let response = self._request(&payload, &auth_state).await?;

        self._extract_fetch_result(response.response, auth_state)
            .await
    }

    /// Sends a request to the specified endpoint using the given HTTP method and payload
    ///
    /// # Arguments
    /// * - `payload` - The JSON value to be sent as the request payload
    /// * - `auth_state` - The authentication state of the Ergani API
    ///
    /// # Returns
    /// * - `ErganiRequestResponse` - The response from the Ergani API
    ///
    /// # Errors
    /// * - `ApiError` - errors may occur for network-related errors
    async fn _request(
        &self,
        payload: &ErganiRequestPayload,
        auth_state: &ErganiAuthenticationState,
    ) -> Result<ErganiRequestResponse> {
        let url = format!("{}{}", self.base_url, payload.endpoint);

        let mut request_builder = self
            .http_client
            .request(payload.method.clone(), url)
            .bearer_auth(auth_state.access_token());

        if let Some(body) = payload.body.clone() {
            request_builder = request_builder.json(&body);
        }

        let response = request_builder.send().await?;

        self._handle_response(payload, response, auth_state).await
    }

    /// Handles the HTTP response, raising exceptions for error status codes and returning the response for successful ones
    /// # Arguments
    /// * - `payload` - The payload of the request
    /// * - `response` - The response object to handle
    /// * - `auth_state` - The authentication state of the Ergani API
    ///
    /// # Returns:
    /// * - `ErganiRequestResponse` - The response from the Ergani API
    ///
    /// # Errors:
    /// * - `[APIError::General]` - An error occurred while communicating with the Ergani API
    /// * - `[APIError::NotFound]` - Raised if the requested resource was not found
    /// * - `[APIError::AuthenticationError]` - Raised if there is an authentication error with the Ergani API
    async fn _handle_response(
        &self,
        payload: &ErganiRequestPayload,
        response: Response,
        auth_state: &ErganiAuthenticationState,
    ) -> Result<ErganiRequestResponse> {
        let status = response.status();

        /// Refresh the authentication token if the response is a 401 Unauthorized
        if status == StatusCode::UNAUTHORIZED {
            info!("Refreshing authentication token");

            let ergani_authenticator = ErganiAuthenticator::builder()
                .base_url(self.base_url.clone())
                .build();

            let refreshed_auth_state = ergani_authenticator.refresh(auth_state).await;

            if refreshed_auth_state.is_err() {
                error!("Failed to refresh authentication token");
                bail!(refreshed_auth_state.err().unwrap())
            }

            let url = format!("{}{}", self.base_url, payload.endpoint);

            let refreshed_auth_state = refreshed_auth_state.unwrap();

            let mut request_builder = self
                .http_client
                .request(payload.method.clone(), url)
                .bearer_auth(refreshed_auth_state.access_token());

            if let Some(body) = payload.body.clone() {
                request_builder = request_builder.json(&body);
            }

            let response = request_builder.send().await?;

            if response.status().is_success() {
                let auth_state = ErganiAuthenticationState::builder()
                    .access_token(refreshed_auth_state.access_token().to_string())
                    .access_token_expired(refreshed_auth_state.access_token_expired())
                    .refresh_token(refreshed_auth_state.refresh_token().to_string())
                    .refresh_token_expired(refreshed_auth_state.refresh_token_expired().to_utc())
                    .build();

                /// Return the response from the Ergani API, with the new authentication state
                let request_response = ErganiRequestResponse::builder()
                    .maybe_response(Some(response))
                    .auth_state(auth_state)
                    .build();

                return Ok(request_response);
            }

            let original_error = response.error_for_status_ref().unwrap_err();
            let error_text = response.text().await?.to_string();
            let ergani_error = ErganiError {
                message: error_text,
            };
            bail!(APIError::AuthenticationFailed(original_error, ergani_error))
        }

        if status == StatusCode::NOT_FOUND {
            let original_error = response.error_for_status_ref().unwrap_err();
            let ergani_error = response.json::<ErganiError>().await?;
            bail!(APIError::NotFound(original_error, ergani_error))
        }

        if status == StatusCode::NO_CONTENT {
            let request_response = ErganiRequestResponse::builder()
                .maybe_response(None)
                .auth_state(auth_state.clone())
                .build();

            return Ok(request_response);
        }

        if status.is_success() {
            let request_response = ErganiRequestResponse::builder()
                .maybe_response(Some(response))
                .auth_state(auth_state.clone())
                .build();

            Ok(request_response)
        } else {
            let original_error = response.error_for_status_ref().unwrap_err();
            let ergani_error = response.json::<ErganiError>().await?;
            bail!(APIError::General(original_error, ergani_error))
        }
    }

    /// Extracts the submission result from the Ergani API response
    /// # Arguments:
    /// * - `response` - The response object from the Ergani API
    ///
    /// # Returns:
    ///  * - `Vec<SubmissionResponse>` - A Vec of submission responses parsed from the API response
    async fn _extract_submission_result(
        &self,
        response: Option<Response>,
        auth_state: ErganiAuthenticationState,
    ) -> Result<Vec<SubmissionResponse>> {
        if response.is_none() {
            return Ok(vec![]);
        }

        let response: Vec<SubmissionResponse> = response.unwrap().json().await?;

        Ok(response)
    }

    /// Extracts the lookup result from the Ergani API response
    /// # Arguments:
    /// * - `response` - The response object from the Ergani API
    ///
    /// # Returns:
    ///  * - `T` - A Vec of T responses parsed from the API response
    async fn _extract_fetch_result<T: DeserializeOwned>(
        &self,
        response: Option<Response>,
        auth_state: ErganiAuthenticationState,
    ) -> Result<ErganiFetchResponse<T>> {
        let response: T = response.unwrap().json().await?;

        Ok(ErganiFetchResponse::builder()
            .response(response)
            .auth_state(auth_state)
            .build())
    }
}