-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Shipping labels] Add Networking support for getting list of packages (…
- Loading branch information
Showing
17 changed files
with
584 additions
and
19 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
26 changes: 26 additions & 0 deletions
26
Networking/Networking/Mapper/WooShippingPackagesMapper.swift
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,26 @@ | ||
import Foundation | ||
|
||
struct WooShippingPackagesMapper: Mapper { | ||
/// (Attempts) to convert a dictionary into WooShippingPackagesResponse. | ||
/// | ||
func map(response: Data) throws -> WooShippingPackagesResponse { | ||
let decoder = JSONDecoder() | ||
if hasDataEnvelope(in: response) { | ||
return try decoder.decode(WooShippingPackagesMapperEnvelope.self, from: response).data | ||
} else { | ||
return try decoder.decode(WooShippingPackagesResponse.self, from: response) | ||
} | ||
} | ||
} | ||
|
||
/// WooShippingPackagesMapperEnvelope Disposable Entity: | ||
/// `Woo Shipping Packages` endpoint returns the shipping label packages in the `data` key. | ||
/// This entity allows us to do parse all the things with JSONDecoder. | ||
/// | ||
private struct WooShippingPackagesMapperEnvelope: Decodable { | ||
let data: WooShippingPackagesResponse | ||
|
||
private enum CodingKeys: String, CodingKey { | ||
case data = "data" | ||
} | ||
} |
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
92 changes: 92 additions & 0 deletions
92
...orking/Model/ShippingLabel/Packages/Predefined package/WooShippingPredefinedPackage.swift
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,92 @@ | ||
import Foundation | ||
import Codegen | ||
import WooFoundation | ||
|
||
/// Represents a predefined Shipping Label Packages for the WooCommerce Shipping extension. | ||
/// | ||
public struct WooShippingPredefinedPackage: Equatable, GeneratedFakeable, Identifiable { | ||
|
||
/// The id of the predefined package | ||
public let id: String | ||
|
||
/// The name of the package, like `USPS Priority Mail Boxes` | ||
public let name: String | ||
|
||
/// Defines if package is a box or a letter. By default is a box, so it's equal to `false` | ||
public let isLetter: Bool | ||
|
||
/// Will be a string formatted like this: `21.91 x 13.65 x 4.13` | ||
public let dimensions: String | ||
|
||
public let boxWeight: String | ||
|
||
// Will be a string for the groupId, like `pri_flat_boxes` | ||
public let groupId: String | ||
|
||
public init(id: String, | ||
name: String, | ||
isLetter: Bool, | ||
dimensions: String, | ||
boxWeight: String, | ||
groupId: String) { | ||
self.id = id | ||
self.name = name | ||
self.isLetter = isLetter | ||
self.dimensions = dimensions | ||
self.boxWeight = boxWeight | ||
self.groupId = groupId | ||
} | ||
|
||
public func getLength() -> Double { | ||
let firstComponent = dimensions.components(separatedBy: " x ").first ?? "" | ||
return Double(firstComponent) ?? 0 | ||
} | ||
|
||
public func getWidth() -> Double { | ||
let secondComponent = dimensions.components(separatedBy: " x ")[safe: 1] ?? "" | ||
return Double(secondComponent) ?? 0 | ||
} | ||
|
||
public func getHeight() -> Double { | ||
let lastComponent = dimensions.components(separatedBy: " x ").last ?? "" | ||
return Double(lastComponent) ?? 0 | ||
} | ||
} | ||
|
||
// MARK: Decodable | ||
|
||
extension WooShippingPredefinedPackage: Decodable { | ||
public init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
|
||
let id = try container.decode(String.self, forKey: .id) | ||
let name = try container.decode(String.self, forKey: .name) | ||
let isLetter = try container.decodeIfPresent(Bool.self, forKey: .isLetter) ?? false | ||
let dimensions = try container.decode(String.self, forKey: .dimensions) | ||
let groupId = try container.decode(String.self, forKey: .groupId) | ||
var boxWeight: String = "" | ||
// Looks like some endpoints have boxWeight as String and some as Double | ||
if let boxWeightDouble = try? container.decodeIfPresent(Double.self, forKey: .boxWeight) { | ||
boxWeight = String(boxWeightDouble) | ||
} | ||
else if let boxWeightString = try? container.decodeIfPresent(String.self, forKey: .boxWeight) { | ||
boxWeight = boxWeightString | ||
} | ||
|
||
self.init(id: id, | ||
name: name, | ||
isLetter: isLetter, | ||
dimensions: dimensions, | ||
boxWeight: boxWeight, | ||
groupId: groupId) | ||
} | ||
|
||
private enum CodingKeys: String, CodingKey { | ||
case id | ||
case name | ||
case isLetter = "is_letter" | ||
case dimensions | ||
case groupId = "group_id" | ||
case boxWeight = "box_weight" | ||
} | ||
} |
Oops, something went wrong.