예제 #1
0
        private static void GetOrdersForTimeWindow(
            OrdersMonitorApp theApp,
            DateTime updatedAfter,
            DateTime updatedBefore
            )
        {
            try
            {
                IMWSResponse                  response;
                ListOrdersResponse            listOrdersResponse            = null;
                ListOrdersByNextTokenResponse listOrdersByNextTokenResponse = null;
                string    nextTokenFromRequest = null;
                OrderList ordersFromRequest;

                do
                {
                    // if this is not the first request, get the rest of the request.
                    if (nextTokenFromRequest != null)
                    {
                        Console.WriteLine("Get the next of the data");
                        response = theApp.InvokeListOrdersByNextToken(nextTokenFromRequest);

                        listOrdersByNextTokenResponse = (ListOrdersByNextTokenResponse)response;

                        nextTokenFromRequest = listOrdersByNextTokenResponse.ListOrdersByNextTokenResult.NextToken;
                        ordersFromRequest    = listOrdersByNextTokenResponse.ListOrdersByNextTokenResult.Orders;
                    }
                    else
                    {
                        // we are making a request for the first time.
                        response = theApp.InvokeListOrders(updatedAfter, updatedBefore);

                        listOrdersResponse = (ListOrdersResponse)response;

                        nextTokenFromRequest = listOrdersResponse.ListOrdersResult.NextToken;
                        ordersFromRequest    = listOrdersResponse.ListOrdersResult.Orders;
                    }

                    ResponseHeaderMetadata rhmd = response.ResponseHeaderMetadata;

                    // We recommend logging the request id and timestamp of every call.
                    Console.WriteLine("RequestId: " + rhmd.RequestId + "Timestamp: " + rhmd.Timestamp);

                    using (var db = new OrderContext())
                    {
                        foreach (Order order in ordersFromRequest.Order)
                        {
                            Console.WriteLine(order.AmazonOrderId + " " + order.PurchaseDate + " " + order.LastUpdateDate);
                            // is this order already in the database ?
                            var queryDuplicateCheck = from o in db.Orders
                                                      where o.AmazonOrderId == order.AmazonOrderId
                                                      select o;

                            if (order.ShippingAddress != null)
                            {
                                if (queryDuplicateCheck.Any())
                                {
                                    var dbOrder = queryDuplicateCheck.First();
                                    db.Orders.Remove(dbOrder);
                                    db.Orders.Add(order);
                                }
                                else
                                {
                                    db.Orders.Add(order);
                                }
                            }

                            if (!queryDuplicateCheck.Any() && order.ShippingAddress != null)
                            {
                                db.Orders.Add(order);
                            }
                        }

                        db.SaveChanges();
                    }

                    Thread.Sleep(30000);
                } while (nextTokenFromRequest != null);
            }
            catch (MarketplaceWebServiceOrdersException ex)
            {
                // Exception properties are important for diagnostics.
                ResponseHeaderMetadata rhmd = ex.ResponseHeaderMetadata;
                Console.WriteLine("Service Exception:");
                if (rhmd != null)
                {
                    Console.WriteLine("RequestId: " + rhmd.RequestId);
                    Console.WriteLine("Timestamp: " + rhmd.Timestamp);
                }
                Console.WriteLine("Message: " + ex.Message);
                Console.WriteLine("StatusCode: " + ex.StatusCode);
                Console.WriteLine("ErrorCode: " + ex.ErrorCode);
                Console.WriteLine("ErrorType: " + ex.ErrorType);
            }
        }
예제 #2
0
        public static void Main(string[] args)
        {
            // The client application name
            const string appName = "OrderHistoryApp";

            // The client application version
            const string appVersion = "1.0";

            // The endpoint for region service and version (see developer guide)
            // ex: https://mws.amazonservices.com
            const string serviceUrl = "https://mws.amazonservices.com/Orders/2011-01-01";

            // Create a configuration object
            MarketplaceWebServiceOrdersConfig config = new MarketplaceWebServiceOrdersConfig {
                ServiceURL = serviceUrl
            };

            // Set other client connection configurations here if needed
            // Create the client itself
            MarketplaceWebServiceOrders.MarketplaceWebServiceOrders client = new MarketplaceWebServiceOrdersClient(AccessKey, SecretKey, appName, appVersion, config);

            OrdersMonitorApp ordersMonitorApp = new OrdersMonitorApp(client);

            try
            {
                // response = sample.InvokeGetOrder();
                // response = sample.InvokeGetServiceStatus();
                // response = sample.InvokeListOrderItems();
                // response = sample.InvokeListOrderItemsByNextToken();

                DateTime updatedAfterDefault  = new DateTime(2014, 7, 20);
                DateTime updatedBeforeDefault = DateTime.Now.AddMinutes(-5);

                using (var db = new OrderContext())
                {
                    // is this the first time ?
                    var query = from o in db.Orders orderby o.LastUpdateDate descending select o;

                    if (query.Any())
                    {
                        Console.WriteLine("Results exist in database: " + query.Count());

                        // although we are using a nullable type, there is no way that we will get a record that has a
                        // null PurchaseDate. So we are safe here.
                        DateTime?tempDate   = query.First().LastUpdateDate;
                        DateTime lastUpdate = DateTimeBegin;  // silly assignment so we cover the else case.
                        if (tempDate != null)
                        {
                            lastUpdate = (DateTime)tempDate;
                        }

                        Console.WriteLine("Last update date is : " + lastUpdate);

                        GetOrdersForTimeWindow(ordersMonitorApp, lastUpdate, updatedBeforeDefault);
                    }
                    else
                    {
                        Console.WriteLine("No results exist. Fresh Database.");

                        // get orders for the time window and save to the database
                        GetOrdersForTimeWindow(ordersMonitorApp, updatedAfterDefault, updatedBeforeDefault);
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }