public async Task <List <ShortCompanyInfo> > SuggestCompanies(string name) { GetRequiredParameters(out string serviceUrl, out string apiKey); #if NETFRAMEWORK _restClient.BaseUrl = serviceUrl; #else _restClient.BaseUrl = new Uri(serviceUrl); #endif _restClient.AddDefaultHeader("ApiKey", apiKey); IRestRequest request = _restClient.CreateRequest(SuggestCompaniesMethodName); request.AddParameter(CompanyNameParameter, name); request.AddParameter(ApiKeyParameter, apiKey); IRestResponse response; try { response = await _restClient.ExecutePostTaskAsync(request).ConfigureAwait(false); } catch (Exception ex) { throw new HttpException((int)HttpStatusCode.InternalServerError, ex.Message); } #if !NETFRAMEWORK if (!response.IsSuccessful) { string message = response.Content.IsNotNullOrEmpty() ? response.Content : response.StatusDescription; throw new HttpException((int)HttpStatusCode.InternalServerError, message); } #endif var responseInfo = ServiceStackTextHelper.Deserialize <CloudRestResponse>(response.Content); if (responseInfo?.CompaniesInfo == null) { throw new HttpException((int)HttpStatusCode.InternalServerError, response.Content); } return(responseInfo.CompaniesInfo); }
/// <summary> /// Creates REST request with JSON content. /// </summary> /// <param name="restClient">REST client to setup.</param> /// <param name="resource">URI of the request.</param> /// <param name="body">Body of the request.</param> /// <param name="method">HTTP method to use.</param> /// <returns>REST request.</returns> public static IRestRequest CreateJsonRequest(this IRestClient restClient, string resource, object body, Method method = Method.POST) { var restRequest = restClient.CreateRequest(resource, method, DataFormat.Json); restRequest.JsonSerializer = new RestSharpJsonConverter(); #if NETFRAMEWORK restRequest.AddBody(body); #else restRequest.AddJsonBody(body); #endif return(restRequest); }
//this method can be extracted to separate class because requests might be created from different places private Request FillRequest() { var req = client.CreateRequest().Results().ForPage(GetPageNumber()); if (Nationality?.Length > 0) { req.ForNationality(Nationality.StripSpacesToLower()).ForSeed(); } if (Gender?.Length > 0) { //seeds don't work with gender selection req.ForGender(Gender.StripSpacesToLower()); } return(req); }