1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FacebookApplication.Helpers;
using FacebookApplication.Models;
using System.Configuration;
using System.Threading.Tasks;
using System.Net.Http;
namespace FacebookApplication.Helpers
{
public static class ShoppingSearchClient
{
private const string SearchApiTemplate = "https://www.googleapis.com/shopping/search/v1/public/products?key={0}&country=US&q={1}&alt=json";
private static HttpClient client = new HttpClient();
public static string AppKey = ConfigurationManager.AppSettings["Search:AppKey"];
public static Task<SearchResult> GetProductsAsync(string query)
{
if (String.IsNullOrEmpty(AppKey))
{
throw new InvalidOperationException("Search:AppKey cannot be empty. Make sure you set it in the configuration file.");
}
query = query.Replace(" ", "+");
string searchQuery = String.Format(SearchApiTemplate, AppKey, query);
var response = client.GetAsync(searchQuery).Result.EnsureSuccessStatusCode();
return response.Content.ReadAsAsync<SearchResult>();
}
}
} |
Partager