public async Task <string> KeywordSearch(string keyword) { var resourcePath = "/services/partsearch/v2/keywordsearch"; var request = new KeywordSearchRequest { Keywords = keyword ?? "P5555-ND", RecordCount = 25 }; ResetExpiredAccessTokenIfNeeded(); var postResponse = await PostAsJsonAsync(resourcePath, request); return(GetServiceResponse(postResponse).Result); }
public async Task <IApiResponse> GetPartsAsync(string partNumber, string partType = "", string mountingType = "") { var authResponse = await AuthorizeAsync(); if (authResponse == null || !authResponse.IsAuthorized) { return(ApiResponse.Create(true, authResponse.AuthorizationUrl, $"User must authorize", nameof(DigikeyApi))); } var keywords = new List <string>(); if (!string.IsNullOrEmpty(partNumber)) { keywords = partNumber.ToLower().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).ToList(); } var packageTypeEnum = MountingTypes.None; if (!string.IsNullOrEmpty(mountingType)) { switch (mountingType.ToLower()) { case "surface mount": packageTypeEnum = MountingTypes.SurfaceMount; break; case "through hole": packageTypeEnum = MountingTypes.ThroughHole; break; } } return(await WrapApiRequestAsync(authResponse, async(authenticationResponse) => { try { // set what fields we want from the API var includes = new List <string> { "DigiKeyPartNumber", "QuantityAvailable", "Manufacturer", "ManufacturerPartNumber", "PrimaryDatasheet", "ProductDescription", "DetailedDescription", "MinimumOrderQuantity", "NonStock", "UnitPrice", "ProductStatus", "ProductUrl", "Parameters" }; var values = new Dictionary <string, string> { { "Includes", $"Products({string.Join(",", includes)})" }, }; var uri = Url.Combine(_apiUrl, "/Search/v3/Products", $"/Keyword?" + string.Join("&", values.Select(x => $"{x.Key}={x.Value}"))); var requestMessage = CreateRequest(authenticationResponse, HttpMethod.Post, uri); var taxonomies = MapTaxonomies(partType, packageTypeEnum); var parametricFilters = MapParametricFilters(keywords, packageTypeEnum, taxonomies); var request = new KeywordSearchRequest { Keywords = string.Join(" ", keywords), Filters = new Filters { TaxonomyIds = taxonomies.Select(x => (int)x).ToList(), ParametricFilters = parametricFilters } }; var json = JsonConvert.SerializeObject(request, _serializerSettings); requestMessage.Content = new StringContent(json, Encoding.UTF8, "application/json"); // perform a keywords API search var response = await _client.SendAsync(requestMessage); if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized) { throw new DigikeyUnauthorizedException(authenticationResponse); } if (response.IsSuccessStatusCode) { var resultString = response.Content.ReadAsStringAsync().Result; var results = JsonConvert.DeserializeObject <KeywordSearchResponse>(resultString, _serializerSettings); return new ApiResponse(results, nameof(DigikeyApi)); } return ApiResponse.Create($"Api returned error status code {response.StatusCode}: {response.ReasonPhrase}", nameof(DigikeyApi)); } catch (Exception ex) { throw ex; } })); }