ZalandoCommerceAPI
public struct ZalandoCommerceAPI
Provides all functional API calls with their business logic. Main entry point for the ZalandoCommerceAPI framework.
Note
If not specified otherwise – all API calls require user to be logged in and accepted a consent. OtherwiseResult.failure
with
APIError.unauthorized
is returned.
-
Configuration of a client handling API calls
Declaration
Swift
public let config: Config
-
Fetches
Article
with given SKU.Declaration
Swift
public func article(with sku: ConfigSKU, completion: @escaping APIResultCompletion<Article>)
Parameters
sku
SKU of an article to fetch
completion
completes async with
APIResult.success
withArticle
-
Fetches current logged user as
Customer
.Declaration
Swift
public func customer(completion: @escaping APIResultCompletion<Customer>)
Parameters
completion
completes async with
APIResult.success
with logged user asCustomer
-
Creates
Cart
with givenCartItemRequest
items.Declaration
Swift
public func createCart(with cartItemRequests: [CartItemRequest], completion: @escaping APIResultCompletion<Cart>)
Parameters
cartItemRequests
list articles SKUs with quantities to be added to cart
completion
completes async with
APIResult.success
withCart
model created. -
Creates
Cart
, and makesCheckout
from it. Additionally handle specific corner cases.Handled cases: - when article has no stock available
completion
returnsResult.failure
withCheckoutError.outOfStock
- when checkout creation failscompletion
returnsResult.failure
withAPIError.checkoutFailed
and createdCart
Declaration
Swift
public func createCartCheckout(with cartItemRequest: CartItemRequest, addresses: CheckoutAddresses? = nil, completion: @escaping APIResultCompletion<CartCheckout>)
Parameters
cartItemRequest
article SKU and quantity to be added to the cart
addresses
addresses to be passed to the checkout
completion
completes async with
APIResult.success
withCartCheckout
. -
Creates
Checkout
based onCartId
.Declaration
Swift
public func createCheckout(from cartId: CartId, addresses: CheckoutAddresses? = nil, completion: @escaping APIResultCompletion<Checkout>)
Parameters
cartId
identifier of a cart (
Cart.id
)addresses
set of billing and shipping addresses
completion
completes async with
APIResult.success
withCheckout
. -
Updates
Checkout
shipping and/or billing addresses.Declaration
Swift
public func updateCheckout(with checkoutId: CheckoutId, updateCheckoutRequest: UpdateCheckoutRequest, completion: @escaping APIResultCompletion<Checkout>)
Parameters
checkoutId
identifier of a checkout (
Checkout.id
)updateCheckoutRequest
new shipping and/or billing address
completion
completes async with
APIResult.success
withCheckout
. -
Places order based on formerly created checkout.
Declaration
Swift
public func createOrder(from checkout: Checkout, completion: @escaping APIResultCompletion<Order>)
Parameters
checkoutId
identifier of a checkout (
Checkout.id
)completion
completes async with
APIResult.success
withOrder
. -
Retrieves details of
GuestCheckout
.Declaration
Swift
public func guestCheckout(with guestCheckoutId: GuestCheckoutId, completion: @escaping APIResultCompletion<GuestCheckout>)
Parameters
checkoutId
identifier of a guest checkout (
GuestCheckout.id
)completion
completes async with
APIResult.success
withGuestCheckout
. -
Retrieves
URL
containing web page handling payment process.Note
The result
URL
should be passed to a web view, and the response returned should be parsed usingPaymentStatus
.Declaration
Swift
public func guestCheckoutPaymentSelectionURL(request: GuestPaymentSelectionRequest, completion: @escaping APIResultCompletion<URL>)
Parameters
request
request containing all the data needed by Guest Checkout
completion
completes async with
APIResult.success
withURL
. -
Creates order from a checkout in Guest Checkout mode.
Declaration
Swift
public func createGuestOrder(request: GuestOrderRequest, completion: @escaping APIResultCompletion<GuestOrder>)
Parameters
request
guest checkout with token
completion
completes async with
APIResult.success
withGuestOrder
. -
Fetches list of
UserAddress
containing all customer’s addresses available for both shipping and billing.Declaration
Swift
public func addresses(completion: @escaping APIResultCompletion<[UserAddress]>)
Parameters
completion
completes async with
APIResult.success
with[UserAddress]
-
Creates new address in customer’s address book.
Declaration
Swift
public func createAddress(with request: CreateAddressRequest, completion: @escaping APIResultCompletion<UserAddress>)
Parameters
request
address details request
completion
completes async with
APIResult.success
with createdUserAddress
. -
Updates customer’s address.
Declaration
Swift
public func updateAddress(with request: UpdateAddressRequest, completion: @escaping APIResultCompletion<UserAddress>)
Parameters
request
address details to be updated
completion
completes async with
APIResult.success
with updatedUserAddress
. -
Deletes given
EquatableAddress
from customer’s address book.Declaration
Swift
public func delete(_ address: EquatableAddress, completion: @escaping APIResultCompletion<Bool>)
Parameters
address
EquatableAddress
to be removed.completion
completes async with
APIResult.success
with success status. -
Verifies correctness of a given address
Declaration
Swift
public func checkAddress(with request: CheckAddressRequest, completion: @escaping APIResultCompletion<CheckAddressResponse>)
Parameters
request
address data
completion
completes async with
APIResult.success
withCheckAddressResponse
-
Fetches recommendations for given article’s SKU
Declaration
Swift
public func recommendations(for sku: ConfigSKU, with recommendationConfig: RecommendationConfig, completion: @escaping APIResultCompletion<[Recommendation]>)
Parameters
sku
article’s identifier to on which recommendations are based (
Article.sku
)recommendationConfig
RecommendationConfig
completion
completes async with
APIResult.success
with[Recommendation]
-
Configures and returns API client based on given options.
When neither
Options
are passed or correct, nor$INFOPLIST_FILE
contains required keys and values –completion
retunsResult.failure
with error returned fromOptions.validate()
Note
See configuration and project structure
Declaration
Swift
public static func configure(options: Options? = nil, completion: @escaping ResultCompletion<ZalandoCommerceAPI>)
Parameters
options
Options for API client to be created. When
nil
,$INFOPLIST_FILE
file of the app is used as configuration.completion
Fired when network configuration call is finished. Contains
Result.success
withZalandoCommerceAPI
orResult.failure
withError
reason.
-
Determines if a client has an access token to call restricted endpoints. Having token doesn’t guarantee it’s unexpired or invalidated.
Declaration
Swift
public var isAuthorized: Bool
-
Authorizes a client with given access token required in restricted endpoints.
Note
Stores
token
securely and makes it globally available for all calls to restricted endpoints identified by sameOptions.environment
Postcondition
Postcondition:
- If a client is authorized successfully
NSNotification.Name.ZalandoCommerceAPIAuthorized
NSNotification.Name.ZalandoCommerceAPIAuthorizationChanged
are posted onNotificationCenter.default
- Both notifications contain
ZalandoCommerceAPI
instance as sendingobject
, anduserInfo
containingZALANDO_COMMERCE_CLIENT_ID
equal toOptions.clientId
, andZALANDO_COMMERCE_USE_SANDBOX
equal toOptions.useSandboxEnvironment
.
Declaration
Swift
public func authorize(with token: AuthorizationToken) -> Bool
Parameters
token
access token passed to all restricted endpoint calls
Return Value
true
if token was correctly stored and client is authorized, otherwisefalse
- If a client is authorized successfully
-
Deauthorizes a client from accessing restricted endpoints.
Postcondition
- If a client is deauthorized successfully
NSNotification.Name.ZalandoCommerceAPIDeauthorized
andNSNotification.Name.ZalandoCommerceAPIAuthorizationChanged
are posted onNotificationCenter.default
. - Both notifications contain
ZalandoCommerceAPI
instance as sendingobject
, anduserInfo
containingZALANDO_COMMERCE_CLIENT_ID
equal toOptions.clientId
, andZALANDO_COMMERCE_USE_SANDBOX
equal toOptions.useSandboxEnvironment
.
Declaration
Swift
public func deauthorize()
- If a client is deauthorized successfully
-
Deauthorizes all clients by removing all stored tokens and notifying about it - SeeAlso:
ZalandoCommerceAPI.deauthorize(with:)
Declaration
Swift
public static func deauthorizeAll()