-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Shipping Labels] Get origin address (#14689)
- Loading branch information
Showing
12 changed files
with
332 additions
and
0 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/WooShippingOriginAddressesMapper.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 WooShippingOriginAddressesMapper: Mapper { | ||
/// (Attempts) to convert a dictionary into WooShippingOriginAddress array. | ||
/// | ||
func map(response: Data) throws -> [WooShippingOriginAddress] { | ||
let decoder = JSONDecoder() | ||
if hasDataEnvelope(in: response) { | ||
return try decoder.decode(WooShippingOriginAddressesMapperEnvelope.self, from: response).data | ||
} else { | ||
return try decoder.decode([WooShippingOriginAddress].self, from: response) | ||
} | ||
} | ||
} | ||
|
||
/// WooShippingOriginAddressesMapperEnvelope Disposable Entity: | ||
/// `Woo Shipping Origin Addresses` endpoint returns the shipping label origin addresses in the `data` key. | ||
/// This entity allows us to do parse all the things with JSONDecoder. | ||
/// | ||
private struct WooShippingOriginAddressesMapperEnvelope: Decodable { | ||
let data: [WooShippingOriginAddress] | ||
|
||
private enum CodingKeys: String, CodingKey { | ||
case data = "data" | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
Networking/Networking/Model/ShippingLabel/Packages/WooShippingOriginAddress.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,105 @@ | ||
import Foundation | ||
import Codegen | ||
|
||
public struct WooShippingOriginAddress: Equatable, GeneratedFakeable, GeneratedCopiable { | ||
public let id: String | ||
public let company: String | ||
public let address1: String | ||
public let address2: String | ||
public let city: String | ||
public let state: String | ||
public let postcode: String | ||
public let country: String | ||
public let phone: String | ||
public let firstName: String | ||
public let lastName: String | ||
public let email: String | ||
public let defaultAddress: Bool | ||
public let isVerified: Bool | ||
|
||
public init(id: String, | ||
company: String, | ||
address1: String, | ||
address2: String, | ||
city: String, | ||
state: String, | ||
postcode: String, | ||
country: String, | ||
phone: String, | ||
firstName: String, | ||
lastName: String, | ||
email: String, | ||
defaultAddress: | ||
Bool, | ||
isVerified: Bool) { | ||
self.id = id | ||
self.company = company | ||
self.address1 = address1 | ||
self.address2 = address2 | ||
self.city = city | ||
self.state = state | ||
self.postcode = postcode | ||
self.country = country | ||
self.phone = phone | ||
self.firstName = firstName | ||
self.lastName = lastName | ||
self.email = email | ||
self.defaultAddress = defaultAddress | ||
self.isVerified = isVerified | ||
} | ||
} | ||
|
||
// MARK: Decodable | ||
extension WooShippingOriginAddress: Decodable { | ||
public init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
|
||
let id = try container.decode(String.self, forKey: CodingKeys.id) | ||
let company = try container.decodeIfPresent(String.self, forKey: CodingKeys.company) ?? "" | ||
let address1 = try container.decodeIfPresent(String.self, forKey: CodingKeys.address1) ?? "" | ||
let address2 = try container.decodeIfPresent(String.self, forKey: CodingKeys.address2) ?? "" | ||
let city = try container.decodeIfPresent(String.self, forKey: CodingKeys.city) ?? "" | ||
let state = try container.decodeIfPresent(String.self, forKey: CodingKeys.state) ?? "" | ||
let postcode = try container.decodeIfPresent(String.self, forKey: CodingKeys.postcode) ?? "" | ||
let country = try container.decodeIfPresent(String.self, forKey: CodingKeys.country) ?? "" | ||
let phone = try container.decodeIfPresent(String.self, forKey: CodingKeys.phone) ?? "" | ||
let firstName = try container.decodeIfPresent(String.self, forKey: CodingKeys.firstName) ?? "" | ||
let lastName = try container.decodeIfPresent(String.self, forKey: CodingKeys.lastName) ?? "" | ||
let email = try container.decodeIfPresent(String.self, forKey: CodingKeys.email) ?? "" | ||
|
||
let defaultAddress = try container.decodeIfPresent(Bool.self, forKey: CodingKeys.defaultAddress) ?? false | ||
let isVerified = try container.decodeIfPresent(Bool.self, forKey: CodingKeys.isVerified) ?? false | ||
|
||
self.init(id: id, | ||
company: company, | ||
address1: address1, | ||
address2: address2, | ||
city: city, | ||
state: state, | ||
postcode: postcode, | ||
country: country, | ||
phone: phone, | ||
firstName: firstName, | ||
lastName: lastName, | ||
email: email, | ||
defaultAddress: defaultAddress, | ||
isVerified: isVerified) | ||
} | ||
|
||
private enum CodingKeys: String, CodingKey { | ||
case id | ||
case company | ||
case address1 = "address_1" | ||
case address2 = "address_2" | ||
case city | ||
case state | ||
case postcode | ||
case country | ||
case phone | ||
case firstName = "first_name" | ||
case lastName = "last_name" | ||
case email | ||
case defaultAddress = "default_address" | ||
case isVerified = "is_verified" | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
Networking/NetworkingTests/Responses/wooshipping-get-origin-addresses-success.json
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,20 @@ | ||
{ | ||
"data": [ | ||
{ | ||
"company": "Superlative Centaur", | ||
"address_2": "", | ||
"city": "SAN FRANCISCO", | ||
"state": "CA", | ||
"postcode": "94110-4929", | ||
"country": "US", | ||
"phone": "12345678901", | ||
"address_1": "60 29TH ST PMB 343", | ||
"first_name": "First", | ||
"last_name": "Last", | ||
"email": "email@automattic.com", | ||
"id": "store_details", | ||
"default_address": true, | ||
"is_verified": true | ||
} | ||
] | ||
} |
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
Oops, something went wrong.