# Driving Summary

The SDK allows for the host application to retrieve driving summary data.

### Fetch Drive Summary

To fetch driving summary data, the following API can be used.

### Concrete example

{% tabs %}
{% tab title="Swift" %}

```swift
let service = DrivingSummaryService()
let startDate = Date() // customize this to your convenience
let endDate = Date() // customize this to your convenience

service.fetch(filters: [.date(start: startDate, end: endDate)], then: { result in 
    guard !result.value.isEmpty else {
        // failure
        return
    }
    completionHandler(.success(result.value))
}
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val drivingSummaryService = DrivingSummaryService()
val filters: MutableSet<DrivingSummaryService.Filter> = HashSet()
filters.add(DrivingSummaryService.Filter.Date(summaryStartDate, summaryEndDate))

drivingSummaryService.fetchDrivingSummaryAggregate {filters)
            { result: Result<Content?>? ->
                if (result?.value == null || result.throwable != null) {
                    // failure
                    } else {
                        // Success
                        val content = result.value
                }
})
```

{% endtab %}
{% endtabs %}

### Fetch Drive Summary By Policy

To fetch driving summary for a specific policy within the given start and end date, the following API can be used.

#### Parameters:

* **`policyId`**: The policy ID
* **`startDate`**: Start date (optional)
* **`endDate`**: End date (optional)
* **`verificationCutOffDate`**: Verification cutoff date (optional)

### Concrete example

{% tabs %}
{% tab title="Swift" %}

```swift
let service = DrivingSummaryService()
service.fetchByPolicy(policyId: policyId,
                       startDate: startDate,
                       endDate: endDate,
                       verificationCutOffDate: verificationCutOffDate) { result in
    switch result {
    case .success(let policyDrivingSummary):
        // Success - use policyDrivingSummary
    case .failure(let error):
        // Handle error
    }
}
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val service = DrivingSummaryService()
service.fetchByPolicy(policyId,
                       startDate,
                       endDate,
                       verificationCutOffDate) { result: Result<PolicyDrivingSummary>? ->
    if (result?.value == null || result.throwable != null) {
        // failure
    } else {
        // Success
        val policyDrivingSummary = result.value
    }
```

{% endtab %}
{% endtabs %}

### Fetch Drive Summary For User By Policy

To fetch driving summary for a specific user and policy within the given start and end date, the following API can be used.

#### Parameters:

* **`userId`**: The user ID
* **`policyId`**: The policy ID
* **`startDate`**: Start date (optional)
* **`endDate`**: End date (optional)
* **`verificationCutOffDate`**: Verification cutoff date (optional)

### Concrete example

{% tabs %}
{% tab title="Swift" %}

```swift
let service = DrivingSummaryService()
service.fetchForUserByPolicy(userId: userId,
                              policyId: policyId,
                              startDate: startDate,
                              endDate: endDate,
                              verificationCutOffDate: verificationCutOffDate) { result in
    switch result {
    case .success(let userDrivingSummary):
        // Success - use userDrivingSummary
    case .failure(let error):
        // Handle error
    }
}
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val service = DrivingSummaryService()
service.fetchForUserByPolicy(userId,
                              policyId,
                              startDate,
                              endDate,
                              verificationCutOffDate) { result: Result<UserDrivingSummary>? ->
    if (result?.value == null || result.throwable != null) {
        // failure
    } else {
        // Success
        val userDrivingSummary = result.value
    }
}
```

{% endtab %}
{% endtabs %}
