I want to catch Status Code in Moya (Status Code: 429)

265 views Asked by At

How can I catch the Status Code returned from Moya? Because when I send too many requests, Status Code: 429 is returned to me from the service.

If I can catch this status Code, I can inform the user and show the user a warning like "You sent too many requests. You have to wait a bit".

Console:

    Moya_Logger: [1/25/22, 5:23 PM] Response: <NSHTTPURLResponse: 0x600003273780> { URL: https://wft-geo-db.p.rapidapi.com/v1/geo/countries?limit=10&offset=40 } { 
Status Code: 429, Headers {
        Connection =     (
            "keep-alive"
        );
        "Content-Length" =     (
            99
        );
        "Content-Type" =     (
            "application/json"
        );
        Date =     (
            "Tue, 25 Jan 2022 14:23:47 GMT"
        );
        Server =     (
            "RapidAPI-1.2.8"
        );
        "X-RapidAPI-Proxy-Response" =     (
            true
        );
        "X-RapidAPI-Region" =     (
            "AWS - eu-central-1"
        );
        "X-RapidAPI-Version" =     (
            "1.2.8"
        );
    } }

Service:

protocol Networkable {
  var provider: MoyaProvider<CountryApi> { get }
  
  func fetchCountry(req: CountriesRequest, completion: @escaping (Result<CountryResponse, MoyaError>) -> ())
  func fetchCountryDetail(req: CountryDetailRequest, completion: @escaping (Result<CountryDetailResponse, MoyaError>) -> ())

}

class CountryServiceManager: Networkable {

  static var shared = CountryServiceManager()
  var provider = MoyaProvider<CountryApi>(plugins: [NetworkLoggerPlugin()])
  
  func fetchCountry(req: CountriesRequest, completion: @escaping (Result<CountryResponse, MoyaError>) -> ()) {
    request(target: .countries(req: req), completion: completion)
  }
  
  func fetchCountryDetail(req: CountryDetailRequest, completion: @escaping (Result<CountryDetailResponse, MoyaError>) -> ()) {
    request(target: .countryDetail(req: req), completion: completion)
  }
}

private extension CountryServiceManager {
  
  private func request<T: Decodable>(target: CountryApi, completion: @escaping (Result<T, MoyaError>) -> ()) {
    provider.request(target) { result in
      switch result {
      case let .success(response):
        do {
          let results = try JSONDecoder().decode(T.self, from: response.data)
          completion(.success(results))
        } catch let error {
          completion(.failure(error as! MoyaError))
        }
      case let .failure(error):
        completion(.failure(error))
      }
    }
  }
}

Service Fetch Function:

  func getCountries(req: CountriesRequest) {
    countryService.fetchCountry(req: req) { result in
      switch result {
      case .success(let countries):
        guard let countriesData = countries.data else { return }
        self.presenter?.countryResponse = countries
        self.presenter?.interactorDidFetchCountries(with: result)
      case .failure(let error):
        print(error.localizedDescription)
      }
    }
  }
0

There are 0 answers