private void Service_SimilarityLookupCompleted(IAsyncResult result)
        {
            BooksFoundEvent foundEvent = new BooksFoundEvent();
            TextSearchState requestState = (TextSearchState)result.AsyncState;

            try
            {
                List<Book> books = new List<Book>();
                books = BuildResponseList(requestState.Request, result);

                //cache the search results, and the induvidual books
                _similaritySearches[requestState.SearchText].AddRange(books);
                AddBooks(books.ToArray());

                //record the results
                foundEvent.Results = _similaritySearches[requestState.SearchText];
                foundEvent.Success = true;
            }
            catch (Exception)
            {
                foundEvent.Success = false;
                foundEvent.FailureMessage = "No Results Found";
            }
            requestState.Callback.Invoke(requestState.SearchText, foundEvent);
        }
        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);
            }
        }
        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);
            }
        }