Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate duplicate BTHTTP HTTP methods #1140

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 3 additions & 19 deletions Sources/BraintreeCore/BTGraphQLHTTP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,19 @@ class BTGraphQLHTTP: BTHTTP {

// MARK: - Overrides

override func get(_ path: String, completion: @escaping RequestCompletion) {
override func get(_ path: String, parameters: [String: Any]? = nil, shouldCache: Bool = false, completion: @escaping RequestCompletion) {
NSException(name: exceptionName, reason: "GET is unsupported").raise()
}

override func get(_ path: String, parameters: [String: Any]? = nil, completion: RequestCompletion?) {
NSException(name: exceptionName, reason: "GET is unsupported").raise()
}

override func post(_ path: String, completion: @escaping RequestCompletion) {
httpRequest(method: "POST", parameters: nil, completion: completion)
}

override func post(_ path: String, parameters: [String: Any]? = nil, completion: @escaping RequestCompletion) {
httpRequest(method: "POST", parameters: parameters, completion: completion)
}

override func put(_ path: String, completion: @escaping RequestCompletion) {
override func put(_ path: String, parameters: [String: Any]? = nil, completion: @escaping RequestCompletion) {
NSException(name: exceptionName, reason: "PUT is unsupported").raise()
}

override func put(_ path: String, parameters: [String: Any]? = nil, completion: RequestCompletion?) {
NSException(name: exceptionName, reason: "PUT is unsupported").raise()
}

override func delete(_ path: String, completion: @escaping RequestCompletion) {
NSException(name: exceptionName, reason: "DELETE is unsupported").raise()
}

override func delete(_ path: String, parameters: [String: Any]? = nil, completion: RequestCompletion?) {
override func delete(_ path: String, parameters: [String: Any]? = nil, completion: @escaping RequestCompletion) {
NSException(name: exceptionName, reason: "DELETE is unsupported").raise()
}

Expand Down
22 changes: 1 addition & 21 deletions Sources/BraintreeCore/BTHTTP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,14 @@ class BTHTTP: NSObject, NSCopying, URLSessionDelegate {

// MARK: - HTTP Methods

func get(_ path: String, completion: @escaping RequestCompletion) {
get(path, parameters: nil, completion: completion)
}

func get(_ path: String, parameters: [String: Any]? = nil, shouldCache: Bool, completion: RequestCompletion?) {
func get(_ path: String, parameters: [String: Any]? = nil, shouldCache: Bool = false, completion: @escaping RequestCompletion) {
if shouldCache {
httpRequestWithCaching(method: "GET", path: path, parameters: parameters, completion: completion)
} else {
httpRequest(method: "GET", path: path, parameters: parameters, completion: completion)
}
}

func get(_ path: String, parameters: [String: Any]? = nil, completion: RequestCompletion?) {
httpRequest(method: "GET", path: path, parameters: parameters, completion: completion)
}

func post(_ path: String, completion: @escaping RequestCompletion) {
post(path, parameters: nil, completion: completion)
}

// TODO: - Remove when all POST bodies use Codable, instead of BTJSON/raw dictionaries
func post(_ path: String, parameters: [String: Any]? = nil, completion: @escaping RequestCompletion) {
httpRequest(method: "POST", path: path, parameters: parameters, completion: completion)
Expand All @@ -133,18 +121,10 @@ class BTHTTP: NSObject, NSCopying, URLSessionDelegate {
}
}

func put(_ path: String, completion: @escaping RequestCompletion) {
put(path, parameters: nil, completion: completion)
}

func put(_ path: String, parameters: [String: Any]? = nil, completion: @escaping RequestCompletion) {
httpRequest(method: "PUT", path: path, parameters: parameters, completion: completion)
}

func delete(_ path: String, completion: @escaping RequestCompletion) {
delete(path, parameters: nil, completion: completion)
}

func delete(_ path: String, parameters: [String: Any]? = nil, completion: @escaping RequestCompletion) {
httpRequest(method: "DELETE", path: path, parameters: parameters, completion: completion)
}
Expand Down
22 changes: 0 additions & 22 deletions UnitTests/BraintreeTestShared/FakeHTTP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,6 @@ import Foundation
stubEndpoint = endpoint
cannedError = error
}

public override func get(_ path: String, parameters: [String: Any]? = nil, completion: ((BTJSON?, HTTPURLResponse?, Error?) -> Void)? = nil) {
GETRequestCount += 1
lastRequestEndpoint = path
lastRequestParameters = parameters
lastRequestMethod = "GET"

if cannedError != nil {
dispatchQueue.async {
completion?(nil, nil, self.cannedError)
}
} else {
let httpResponse = HTTPURLResponse(url: URL(string: path)!, statusCode: cannedStatusCode, httpVersion: nil, headerFields: nil)
dispatchQueue.async {
if path.contains("v1/configuration") {
completion?(self.cannedConfiguration, httpResponse, nil)
} else {
completion?(self.cannedResponse, httpResponse, nil)
}
}
}
}

public override func get(_ path: String, parameters: [String: Any]? = nil, shouldCache: Bool, completion: BTHTTP.RequestCompletion?) {
GETRequestCount += 1
Expand Down