Get quotes

Mobile Network SDK

  1. Getting started
  2. Authentication & Configuration
  3. Plan a Trip
  4. Get Quotes
  5. Book a Trip
  6. Track a Trip
  7. Testing

In this step, we need to retrieve available quotes for the selected pickup and destination locations. The KarhooApi.quotesService can be used to get quotes from fleet operators for a specified trip between two locations.

Get Quotes

The KarhooApi.quotesService.quotes requires a QuoteSearch which specifies the location of origin and destination of the trip. You can even specify the pickup time if the user would like to be picked up later.

let quoteService = Karhoo.getQuotesService()

// create request
let quoteSearch = QuoteSearch(origin: originLocationDetails,
                              destination: destinationLocationDetails, 
                              dateScheduled: scheduledDate)

// create observer
let quoteSearchObserver = Observer<Quotes> { [weak self] result in
    switch result {
        case .success(let quotes):
            print("Quotes: \(quotes.all)")
        case .failure(let error):
            // handle error (KarhooError)
    }
}

// create observable
let quotesObservable = quotesService.quotes(quoteSearch: quoteSearch).observable()

// subscribe observer to observable
quotesObservable.subscribe(observer: quoteSearchObserver)
// Create request
val quotesSearch = QuotesSearch(origin: originLocationDetails,
                destination: destinationLocationDetails,
                date: scheduledDate)

// Create observer 
val observer = object : Observer<Resource<QuoteList>> {
    override fun onValueChanged(value: Resource<QuoteList>) {
        when (value) {
            is Resource.Success -> updateVehicles(value.data)
            is Resource.Failure -> handleAvailabilityError(value.error)
        }
    }
}

// Poll for quotes
val quotesObservable = quotesService
   .quotes(quotesSearch)
   .observable()
   .apply { subscribe(observer) }

🚧

Remember to unsubscribe Observers when you no longer need them, such as in the unloading of a view.

// unsubscribe observer from observable when finished
quotesObservable.unsubscribe(observer: quoteSearchObserver)
// unsubscribe observer from observable when finished
quotesObservable.unsubscribe(observer)

What’s Next