private DadataResponse GetSuggestionsInternal(string query, ContractorType type) { var request = (HttpWebRequest)WebRequest.Create(_dadataUrl); request.Method = WebRequestMethods.Http.Post; request.ContentType = "application/json"; request.Accept = "application/json"; request.Headers.Add("Authorization", $"Token {_apiKey}"); var requestJson = JsonConvert.SerializeObject(new DadataRequest(query, type)); HttpWebResponse response = null; try { request.ContentLength = requestJson.Length; using (Stream stream = request.GetRequestStream()) stream.Write(Encoding.UTF8.GetBytes(requestJson), 0, requestJson.Length); response = (HttpWebResponse)request.GetResponse(); string responseJson; using (var sr = new StreamReader(response.GetResponseStream())) responseJson = sr.ReadToEnd(); if (response.StatusCode == HttpStatusCode.OK && !responseJson.IsNullOrEmpty()) { DadataResponse dadataResponse = JsonConvert.DeserializeObject <DadataResponse>(responseJson); if (dadataResponse.Suggestions.Length == 0) { return(null); } return(dadataResponse); } } catch (WebException ex) { // TODO } catch (Exception ex) { // TODO } finally { response?.Close(); } return(null); }
public ActionResult <Contractor> Create(Contractor contractor) { if (contractor == null) { return(BadRequest()); } // name, type, inn не могут быть пустыми if (contractor.Name.IsNullOrEmpty() || !contractor.Type.InList(ContractorType.Legal, ContractorType.Individual) || contractor.Inn.IsNullOrEmpty()) { return(BadRequest("Поля name, type, inn не могут быть пустыми")); } // kpp не может быть пусто у Юр.лица if (contractor.Type == ContractorType.Legal && contractor.Kpp.IsNullOrEmpty()) { return(BadRequest("kpp не может быть пусто у Юр.лица")); } // Проверка на существование элемента if (_liteDbContractorService.Exists(contractor.Id)) { return(BadRequest($"Id {contractor.Id} has already exists.")); } // При создании контрагента проверять его наличие в ЕГРЮЛ по полям inn kpp для Юр.лица и по inn для ИП, через сервис dadata.ru(https://dadata.ru/api/find-party/), // если организация с указанными inn kpp или ИП с указанным inn не существует, выдавать ошибку DadataResponse response = _dadataClient.GetSuggestions(contractor.Inn, contractor.Kpp, contractor.Type); if (response == null) { return(BadRequest()); } // Ответов может быть несколько, берём первый contractor.FullName = response.Suggestions[0].Data.Name.FullWithOpf; _liteDbContractorService.Insert(contractor); return(CreatedAtAction(nameof(Create), new { id = contractor.Id }, contractor)); }
public DadataResponse GetSuggestions(string inn, string kpp, ContractorType type) { DadataResponse response = null; // При создании контрагента проверять его наличие в ЕГРЮЛ по полям inn kpp для Юр.лица и по inn для ИП if (type == ContractorType.Legal) { response = GetSuggestionsInternal(inn, type); if (response == null) { response = GetSuggestionsInternal(kpp, type); } } else if (type == ContractorType.Individual) { response = GetSuggestionsInternal(inn, type); } return(response); }