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. Otherwise Result.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 with Article

  • 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 as Customer

  • Creates Cart with given CartItemRequest 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 with Cart model created.

  • Creates Cart, and makes Checkout from it. Additionally handle specific corner cases.

    Handled cases: - when article has no stock available completion returns Result.failure with CheckoutError.outOfStock - when checkout creation fails completion returns Result.failure with APIError.checkoutFailed and created Cart

    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 with CartCheckout.

  • Creates Checkout based on CartId.

    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 with Checkout.

  • 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 with Checkout.

  • 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 with Order.

  • 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 with GuestCheckout.

  • 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 using PaymentStatus.

    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 with URL.

  • 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 with GuestOrder.

  • 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 created UserAddress.

  • 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 updated UserAddress.

  • 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 with CheckAddressResponse

  • 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 retuns Result.failure with error returned from Options.validate()

    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 with ZalandoCommerceAPI or Result.failure with Error 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 same Options.environment

    Postcondition

    Postcondition:

    • If a client is authorized successfully NSNotification.Name.ZalandoCommerceAPIAuthorized NSNotification.Name.ZalandoCommerceAPIAuthorizationChanged are posted on NotificationCenter.default
    • Both notifications contain ZalandoCommerceAPI instance as sending object, and userInfo containing ZALANDO_COMMERCE_CLIENT_ID equal to Options.clientId, and ZALANDO_COMMERCE_USE_SANDBOX equal to Options.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, otherwise false

  • Deauthorizes a client from accessing restricted endpoints.

    Postcondition

    • If a client is deauthorized successfully NSNotification.Name.ZalandoCommerceAPIDeauthorized and NSNotification.Name.ZalandoCommerceAPIAuthorizationChanged are posted on NotificationCenter.default.
    • Both notifications contain ZalandoCommerceAPI instance as sending object, and userInfo containing ZALANDO_COMMERCE_CLIENT_ID equal to Options.clientId, and ZALANDO_COMMERCE_USE_SANDBOX equal to Options.useSandboxEnvironment.

    Declaration

    Swift

    public func deauthorize()
  • Deauthorizes all clients by removing all stored tokens and notifying about it - SeeAlso: ZalandoCommerceAPI.deauthorize(with:)

    Declaration

    Swift

    public static func deauthorizeAll()