forked from AvdLee/appstoreconnect-swift-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into codableResponse
- Loading branch information
Showing
9 changed files
with
130 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// HTTPURLResponseExtensions.swift | ||
// | ||
// | ||
// Created by Mathias Emil Mortensen on 10/04/2023. | ||
// | ||
|
||
import Foundation | ||
|
||
extension HTTPURLResponse { | ||
var rateLimit: RateLimit? { | ||
if let value = value(forHTTPHeaderField: "X-Rate-Limit") { | ||
return RateLimit(value: value, requestURL: url) | ||
} else { | ||
return nil | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88166,7 +88166,8 @@ | |
"IPHONE", | ||
"IPOD", | ||
"APPLE_TV", | ||
"MAC" | ||
"MAC", | ||
"APPLE_SILICON_MAC" | ||
] | ||
}, | ||
"status": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// RateLimit.swift | ||
// | ||
// | ||
// Created by Mathias Emil Mortensen on 05/04/2023. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct RateLimit { | ||
/// Rate limit entries | ||
public let entries: [String: Int] | ||
|
||
/// The url of the request the rate limit were returned for. | ||
public let requestURL: URL? | ||
|
||
init(value: String, requestURL: URL?) { | ||
self.requestURL = requestURL | ||
|
||
let items = value.split(separator: ";", omittingEmptySubsequences: true) | ||
entries = items.reduce(into: [:]) { partialResult, item in | ||
guard let colonIdx = item.firstIndex(of: ":") else { | ||
return | ||
} | ||
let key = item[..<colonIdx] | ||
let valueString = item[item.index(after: colonIdx)...] | ||
guard let value = Int(valueString) else { | ||
return | ||
} | ||
partialResult[String(key)] = value | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// | ||
// RateLimitTests.swift | ||
// | ||
// | ||
// Created by Mathias Emil Mortensen on 05/04/2023. | ||
// | ||
|
||
import XCTest | ||
@testable import AppStoreConnect_Swift_SDK | ||
|
||
final class RateLimitTests: XCTestCase { | ||
|
||
func testValidValue() { | ||
let rateLimit = RateLimit(value: "user-hour-lim:3600;user-hour-rem:3545;", requestURL: nil) | ||
XCTAssertEqual(rateLimit.entries.count, 2) | ||
XCTAssertEqual(rateLimit.entries["user-hour-lim"], 3600) | ||
XCTAssertEqual(rateLimit.entries["user-hour-rem"], 3545) | ||
} | ||
|
||
func testInvalidValue() { | ||
let rateLimit = RateLimit(value: "user-hour-rem3545", requestURL: nil) | ||
XCTAssertEqual(rateLimit.entries.count, 0) | ||
} | ||
|
||
func testPartialInvalidValue() { | ||
let rateLimit = RateLimit(value: "user-hour-rem3545;user-hour-rem:3545;", requestURL: nil) | ||
XCTAssertEqual(rateLimit.entries.count, 1) | ||
XCTAssertEqual(rateLimit.entries["user-hour-rem"], 3545) | ||
} | ||
|
||
func testMoreItems() { | ||
let rateLimit = RateLimit(value: "user-hour-lim:3600;user-hour-rem:3598;user-minute-lim:150;user-minute-rem:149;", requestURL: nil) | ||
XCTAssertEqual(rateLimit.entries.count, 4) | ||
XCTAssertEqual(rateLimit.entries["user-hour-lim"], 3600) | ||
XCTAssertEqual(rateLimit.entries["user-hour-rem"], 3598) | ||
XCTAssertEqual(rateLimit.entries["user-minute-lim"], 150) | ||
XCTAssertEqual(rateLimit.entries["user-minute-rem"], 149) | ||
} | ||
|
||
func testExtractingHeaderField() { | ||
let httpUrlResponse = HTTPURLResponse(url: URL(string: "https://api.appstoreconnect.apple.com/")!, statusCode: 200, httpVersion: nil, headerFields: ["X-ratE-limiT": "user-hour-lim:3600;user-hour-rem:3545;"]) | ||
let rateLimit = httpUrlResponse?.rateLimit | ||
XCTAssertNotNil(rateLimit) | ||
if let rateLimit { | ||
XCTAssertEqual(rateLimit.entries.count, 2) | ||
XCTAssertEqual(rateLimit.entries["user-hour-lim"], 3600) | ||
XCTAssertEqual(rateLimit.entries["user-hour-rem"], 3545) | ||
} | ||
} | ||
} |