Пример #1
0
        public void FindBooksByKeyword(string searchText, BooksFoundEventHandler callback)
        {
            //check if the search has been cached, if it hasn't then call the web service
            if (!_textSearches.ContainsKey(searchText))
            {
                IDictionary<string, string> requestParams = new Dictionary<string, String>();
                requestParams["Service"] = "AWSECommerceService";
                requestParams["Operation"] = "ItemSearch";
                requestParams["Keywords"] = HttpUtility.UrlEncode(searchText);
                requestParams["ResponseGroup"] = RESPONSE_GROUP;
                requestParams["SearchIndex"] = SEARCH_INDEX;
                requestParams["ItemPage"] = ITEM_PAGE;
                requestParams["Version"] = SERVICE_VERSION;
                requestParams["AssociateTag"] = Constants.MY_AWS_ASSOCIATE_TAG;

                _textSearches.Add(searchText, new List<Book>());
                WebRequest request = CreateRequest(requestParams);

                TextSearchState state = new TextSearchState();
                state.SearchText = searchText;
                state.Callback = callback;
                state.Request = request;

                request.BeginGetResponse(Service_ItemSearchCompleted, state);
            }
            //if it has then retrive the results from the cache
            else
            {
                BooksFoundEvent foundEvent = new BooksFoundEvent();
                foundEvent.Success = true;
                foundEvent.Results = _textSearches[searchText];
                callback.Invoke(string.Empty, foundEvent);
            }
        }
Пример #2
0
        public void FindSimilarBooks(string itemID, BooksFoundEventHandler callback)
        {
            if (!_books.ContainsKey(itemID)) {
                BooksFoundEvent foundEvent = new BooksFoundEvent();
                foundEvent.Success = false;
                foundEvent.FailureMessage = "No item exists for the given ID";
                foundEvent.Results = new List<Book>();
                callback.Invoke(itemID, foundEvent);
            }

            //check if the search has been cached, if it hasn't then call the web service
            if (!_similaritySearches.ContainsKey(itemID))
            {
                IDictionary<string, string> requestParams = new Dictionary<string, String>();
                requestParams["Service"] = "AWSECommerceService";
                requestParams["Operation"] = "SimilarityLookup";
                requestParams["ItemId"] = itemID;
                requestParams["ResponseGroup"] = RESPONSE_GROUP;
                requestParams["SimilarityType"] = "Random";
                requestParams["Version"] = SERVICE_VERSION;
                requestParams["AssociateTag"] = Constants.MY_AWS_ASSOCIATE_TAG;

                _similaritySearches.Add(itemID, new List<Book>());
                WebRequest request = CreateRequest(requestParams);

                TextSearchState state = new TextSearchState();
                state.SearchText = itemID;
                state.Callback = callback;
                state.Request = request;

                request.BeginGetResponse(Service_SimilarityLookupCompleted, state);
            }
            //if it has then retrieve the results from the cache
            else
            {
                BooksFoundEvent foundEvent = new BooksFoundEvent();
                foundEvent.Success = true;
                foundEvent.Results = _similaritySearches[itemID];
                callback.Invoke(itemID, foundEvent);
            }
        }