Skip to content

Commit

Permalink
Added Strava API counts
Browse files Browse the repository at this point in the history
Added properties to maintain 15 min and daily Strava API counts and limits
  • Loading branch information
FleetPhil authored Oct 13, 2020
1 parent 6f83d5b commit f348883
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions Sources/StravaSwift/StravaClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ open class StravaClient: NSObject {
*/
public static let sharedInstance = StravaClient()


// Properties to hold the latest access counts and limits for Strava API calls as retrieved from the most recent API call
// First value in each pair is 15 minute value, second is daily value
// See https://developers.strava.com/docs/rate-limits/
fileprivate var stravaAccessUsage: [Int]?
fileprivate var stravaAccessLimit: [Int]?

fileprivate override init() {}
fileprivate var config: StravaConfig?

Expand Down Expand Up @@ -63,6 +70,17 @@ open class StravaClient: NSObject {
}
}

// MARK: Access API counts
extension StravaClient {
public func apiUsageCounts() -> [Int]? {
return self.stravaAccessUsage
}
public func apiUsageLimits() -> [Int]? {
return self.stravaAccessLimit
}
}


//MARK:varConfig

extension StravaClient {
Expand Down Expand Up @@ -106,7 +124,7 @@ extension StravaClient: ASWebAuthenticationPresentationContextProviding {
callbackURLScheme: config?.redirectUri,
completionHandler: { (url, error) in
if let url = url, error == nil {
_ = self.handleAuthorizationRedirect(url, result: result)
self.handleAuthorizationRedirect(url, result: result)
} else {
result(.failure(error!))
}
Expand All @@ -120,7 +138,7 @@ extension StravaClient: ASWebAuthenticationPresentationContextProviding {
let authenticationSession = SFAuthenticationSession(url: Router.webAuthorizationUrl,
callbackURLScheme: config?.redirectUri) { (url, error) in
if let url = url, error == nil {
_ = self.handleAuthorizationRedirect(url, result: result)
self.handleAuthorizationRedirect(url, result: result)
} else {
result(.failure(error!))
}
Expand Down Expand Up @@ -256,6 +274,7 @@ extension StravaClient {
if let statusCode = response.response?.statusCode, (400..<500).contains(statusCode) {
failure(self.generateError(failureReason: "Strava API Error", response: response.response))
} else {
self.updateStravaUsage(response: response.response)
result(response.result.value)
}
}
Expand All @@ -277,6 +296,7 @@ extension StravaClient {
if let statusCode = response.response?.statusCode, (400..<500).contains(statusCode) {
failure(self.generateError(failureReason: "Strava API Error", response: response.response))
} else {
self.updateStravaUsage(response: response.response)
result(response.result.value)
}
}
Expand All @@ -293,6 +313,13 @@ extension StravaClient {

return returnError
}

fileprivate func updateStravaUsage(response: HTTPURLResponse?) {
if #available(iOS 13.0, *) {
self.stravaAccessUsage = response?.value(forHTTPHeaderField: "x-ratelimit-usage")?.components(separatedBy: ",").map { Int($0) ?? 0 }
self.stravaAccessLimit = response?.value(forHTTPHeaderField: "x-ratelimit-limit")?.components(separatedBy: ",").map { Int($0) ?? 0 }
}
}

}

Expand Down

0 comments on commit f348883

Please sign in to comment.