All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.wix.restaurants.examples.RetrieveNewOrdersExample Maven / Gradle / Ivy

There is a newer version: 1.22.0
Show newest version
package com.wix.restaurants.examples;

import com.openrest.v1_1.Order;
import com.wix.restaurants.DefaultWixRestaurantsClient;
import com.wix.restaurants.WixRestaurantsClient;
import com.wix.restaurants.authentication.WixRestaurantsAuthenticationClient;
import com.wix.restaurants.exceptions.NoPermissionException;
import scala.concurrent.duration.Duration;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Demonstrates the "Retrieve Orders" flow.
 * 1) Login with username and password to get an access token
 * 2) Retrieve new orders
 * 3) Mark orders as accepted
 *
 * @see The Testaurant
 */
public class RetrieveNewOrdersExample {
    private final WixRestaurantsClient wixRestaurants;

    public RetrieveNewOrdersExample(WixRestaurantsClient wixRestaurants) {
        this.wixRestaurants = wixRestaurants;
    }

    public void runExample() {
        final String restaurantId = "8830975305376234"; // "The Testaurant"

        // Placeholder value that will fail authentication. Use a real value in your live setting.
        final String wixInstance = "XXX";

        // 1. Login with Wix Instance to get an access token
        System.out.print("Authenticating...");
        final WixRestaurantsAuthenticationClient authentication = wixRestaurants.getAuthenticationClient();
        final String accessToken = authentication.loginWithWixInstance(wixInstance).accessToken;
        System.out.println(" done (accessToken: " + accessToken + ").");

        // 2. Retrieve new orders
        System.out.print("Retrieving new orders...");
        final List newOrders;
        try {
            newOrders = wixRestaurants.retrieveNewOrders(accessToken, restaurantId);
            System.out.println(" got " + newOrders.size() + " new orders.");
        } catch (NoPermissionException e) {
            System.out.println(" no permissions (did you change the test credentials to real ones?)");
            throw e;
        }

        // 3. Mark orders as accepted
        for (Order newOrder : newOrders) {
            // A common use-case is to submit orders to an external system, e.g. point-of-sale. If that's the case,
            // users are encouraged to report back the external system's order ID (for reference) via the externalIds
            // argument. Otherwise, an empty map should be used.
            final Map externalIds = Collections.singletonMap("org.example.pos", "SOME-POS-ORDER-ID");

            wixRestaurants.acceptOrder(accessToken, restaurantId, newOrder.id, externalIds);
        }
    }

    public static void main(String[] args) throws Exception {
        final WixRestaurantsClient wixRestaurants = new DefaultWixRestaurantsClient(
                "https://api.wixrestaurants.com/v2",
                "https://auth.wixrestaurants.com/v1",
                Duration.Inf());

        new RetrieveNewOrdersExample(wixRestaurants).runExample();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy