Skip to content

Commit

Permalink
Merge fbc0b06 into 8584a1c
Browse files Browse the repository at this point in the history
  • Loading branch information
paultopher committed Aug 28, 2019
2 parents 8584a1c + fbc0b06 commit 6b47507
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 9 deletions.
4 changes: 4 additions & 0 deletions ButtonMerchant.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
DA756B8722B3ECE9003397E3 /* TrustEvaluatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA756B8622B3ECE9003397E3 /* TrustEvaluatorTests.swift */; };
DA756B8922B40207003397E3 /* TestURLProtectionSpace.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA756B8822B40207003397E3 /* TestURLProtectionSpace.swift */; };
DA98C00622B29BA0002D1823 /* PEMCertificate.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA98C00522B29BA0002D1823 /* PEMCertificate.swift */; };
DAB8DFC42316EB3200E16619 /* NetworkError.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAB8DFC32316EB3200E16619 /* NetworkError.swift */; };
DADE90C2209B9A630073144B /* TestError.swift in Sources */ = {isa = PBXBuildFile; fileRef = DADE90C1209B9A630073144B /* TestError.swift */; };
DAE8B96F22AF5F0700D11AF9 /* TrustEvaluator.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAE8B96D22AF5F0400D11AF9 /* TrustEvaluator.swift */; };
DC1F008122CA85F000E789D0 /* Configurable.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC1F008022CA85F000E789D0 /* Configurable.swift */; };
Expand Down Expand Up @@ -223,6 +224,7 @@
DA756B8622B3ECE9003397E3 /* TrustEvaluatorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TrustEvaluatorTests.swift; sourceTree = "<group>"; };
DA756B8822B40207003397E3 /* TestURLProtectionSpace.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestURLProtectionSpace.swift; sourceTree = "<group>"; };
DA98C00522B29BA0002D1823 /* PEMCertificate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PEMCertificate.swift; sourceTree = "<group>"; };
DAB8DFC32316EB3200E16619 /* NetworkError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkError.swift; sourceTree = "<group>"; };
DADE90C1209B9A630073144B /* TestError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestError.swift; sourceTree = "<group>"; };
DAE8B96D22AF5F0400D11AF9 /* TrustEvaluator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TrustEvaluator.swift; sourceTree = "<group>"; };
DC1F008022CA85F000E789D0 /* Configurable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Configurable.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -605,6 +607,7 @@
DA4C8C7822D3D909000E15A9 /* ReportOrderRequest.swift */,
DA4C8C7A22D3D956000E15A9 /* RetryPolicy.swift */,
DE2F744E208F6535001E4BD6 /* ConfigurationError.swift */,
DAB8DFC32316EB3200E16619 /* NetworkError.swift */,
9E77202020605126005F740B /* Extensions */,
DE865FA42053073B00F4054D /* Supporting Files */,
);
Expand Down Expand Up @@ -1148,6 +1151,7 @@
9E976B87207D1A3000783F1A /* DateExtensions.swift in Sources */,
9EB1B0A2207AB5ED00BE0A1A /* FileManagerExtensions.swift in Sources */,
9E2B4311206C1275009F2886 /* Client.swift in Sources */,
DAB8DFC42316EB3200E16619 /* NetworkError.swift in Sources */,
DA4C8C7B22D3D956000E15A9 /* RetryPolicy.swift in Sources */,
DA756B8522B3378B003397E3 /* URLProtectionSpaceExtensions.swift in Sources */,
);
Expand Down
7 changes: 6 additions & 1 deletion Examples/Objective-C/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ - (void)reportOrder {
order.customer = customer;
[ButtonMerchant reportOrder:order completion:^(NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^(){
[MessageView showWithTitle:@"Order Created" body:[NSString stringWithFormat:@"Id: %@", identifier] in:self.navigationController.view];
if (error) {
[MessageView showWithTitle:@"Order Reporting Failed" body:@"" in:self.navigationController.view];
}
else {
[MessageView showWithTitle:@"Order Created" body:[NSString stringWithFormat:@"Id: %@", identifier] in:self.navigationController.view];
}
});
}];
}
Expand Down
4 changes: 4 additions & 0 deletions Examples/Swift/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ class ViewController: UITableViewController {
order.customer = customer
ButtonMerchant.reportOrder(order) { (error) in
DispatchQueue.main.async {
guard error == nil else {
MessageView.showWithTitle("Order Reporting Failed", body: "", in: self.navigationController!.view)
return;
}
MessageView.showWithTitle("Order Created", body: "Id: \(id)", in: self.navigationController!.view)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ internal final class Client: ClientType {

func reportOrder(orderRequest: ReportOrderRequestType, _ completion: ((Error?) -> Void)?) {
var request = urlRequest(url: Service.order.url, parameters: orderRequest.parameters)
request.setValue("Basic \(orderRequest.encodedApplicationId):", forHTTPHeaderField: "Authorization")
request.setValue("Basic \(orderRequest.encodedApplicationId)", forHTTPHeaderField: "Authorization")
orderRequest.report(request, with: session, completion)
}

Expand Down
2 changes: 1 addition & 1 deletion Source/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ final internal class Core: CoreType {

func reportOrder(_ order: Order, _ completion: ((Error?) -> Void)?) {
guard let appId = applicationId, !appId.isEmpty,
let encodedAppId = appId.data(using: .utf8)?.base64EncodedString() else {
let encodedAppId = (appId + ":").data(using: .utf8)?.base64EncodedString() else {
if let completion = completion {
completion(ConfigurationError.noApplicationId)
}
Expand Down
56 changes: 56 additions & 0 deletions Source/NetworkError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// NetworkError.swift
//
// Copyright © 2019 Button, Inc. All rights reserved. (https://usebutton.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

import Foundation

/**
Button Merchant Library Configuration Error.
*/
public enum NetworkError: Error {

/**
There was an unknown network error.
*/
case unknown

var domain: String {
return "com.usebutton.merchant.network.error"
}

var localizedDescription: String {
switch self {
case .unknown:
return "An unknown network error occured."
}
}
}

extension NetworkError: Equatable {

/// :nodoc:
public static func == (lhs: NetworkError, rhs: NetworkError) -> Bool {
return lhs.domain == rhs.domain
&& lhs.localizedDescription == rhs.localizedDescription
}
}
8 changes: 6 additions & 2 deletions Source/ReportOrderRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ internal final class ReportOrderRequest: ReportOrderRequestType {
func report(_ request: URLRequest, with session: URLSessionType, _ completion: ((Error?) -> Void)?) {
let task = session.dataTask(with: request) { data, response, error in
var nextAttempt = true
var networkError: Error? = NetworkError.unknown
if let response = response as? HTTPURLResponse {
switch response.statusCode {
case 200:
networkError = nil
nextAttempt = false
case 500...599:
nextAttempt = true
default:
Expand All @@ -57,7 +61,7 @@ internal final class ReportOrderRequest: ReportOrderRequestType {
}
guard nextAttempt else {
if let completion = completion {
completion(error)
completion(networkError)
}
return
}
Expand All @@ -66,7 +70,7 @@ internal final class ReportOrderRequest: ReportOrderRequestType {

guard self.retryPolicy.shouldRetry else {
if let completion = completion {
completion(error)
completion(networkError)
}
return
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/UnitTests/ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ class ClientTests: XCTestCase {
}

XCTAssertEqualReferences(request.testSession as AnyObject, session)
XCTAssertEqual(request.testRequest?.allHTTPHeaderFields!["Authorization"], "Basic abc123:")
XCTAssertEqual(request.testRequest?.allHTTPHeaderFields!["Authorization"], "Basic abc123")
XCTAssertEqual(request.testRequest?.url, URL(string: "https://api.usebutton.com/v1/mobile-order")!)
XCTAssertEqual(request.testRequest?.httpMethod, "POST")
let body = try? JSONSerialization.jsonObject(with: (request.testRequest?.httpBody)!) as? [String: String]
Expand Down
2 changes: 1 addition & 1 deletion Tests/UnitTests/CoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ class CoreTests: XCTestCase {
"line_items": [["identifier": "unique-id-1234", "quantity": 1, "total": 120]],
"customer": ["id": "customer-id-123", "email_sha256": "21f61e98ab4ae120e88ac6b5dd218ffb8cf3e481276b499a2e0adab80092899c"]])

XCTAssertEqual(testClient.testReportOrderRequest!.encodedApplicationId, "YXBwLWFiYzEyMw==")
XCTAssertEqual(testClient.testReportOrderRequest!.encodedApplicationId, "YXBwLWFiYzEyMzo=")
testClient.reportOrderCompletion!(nil)

self.wait(for: [expectation], timeout: 2.0)
Expand Down
4 changes: 2 additions & 2 deletions Tests/UnitTests/ReportOrderRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class ReportOrderRequestTests: XCTestCase {
request.report(urlRequest, with: session) { error in

// Assert
XCTAssertNil(error)
XCTAssertEqual(error as? NetworkError, NetworkError.unknown)
XCTAssertFalse(self.policy.didCallNext)

expectation.fulfill()
Expand Down Expand Up @@ -153,7 +153,7 @@ class ReportOrderRequestTests: XCTestCase {
request.report(urlRequest, with: session) { error in

// Assert
XCTAssertNil(error)
XCTAssertEqual(error as? NetworkError, NetworkError.unknown)

expectation.fulfill()
}
Expand Down

0 comments on commit 6b47507

Please sign in to comment.