public async Task <IActionResult> Register(InvestorInfoModel investorInfo)
        {
            if (investorInfo == null)
            {
                throw new ArgumentNullException(nameof(investorInfo));
            }

            ResponseContractDTO responseContractDTO = new ResponseContractDTO();

            using (var httpClient = new HttpClient())
            {
                string jsonNewContract = JsonConvert.SerializeObject(investorInfo);

                //Convert object properties in lowercase
                var newContractObject = JObject.Parse(jsonNewContract, new JsonLoadSettings());
                JsonHelpers.ChangePropertiesToLowerCase(newContractObject);

                try
                {
                    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    var builder = new UriBuilder(new Uri(urlAPI));
                    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, builder.Uri);

                    request.Content = new StringContent(newContractObject.ToString(Formatting.None), Encoding.UTF8, "application/json");//CONTENT-TYPE header

                    using (var response = await httpClient.SendAsync(request))
                    {
                        string apiResponse = await response.Content.ReadAsStringAsync();

                        responseContractDTO = JsonConvert.DeserializeObject <ResponseContractDTO>(apiResponse);

                        ViewBag.TokenCustomer = responseContractDTO.Users[0].Token;
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            return(View("SignContract"));
            //return Ok(responseContractDTO);
        }
Esempio n. 2
0
        public async Task <ResponseContractDTO> CreateContract(ContractInvestor contractInvestor)
        {
            NewContractDTO contractToSend = GetNewContractToSign(contractInvestor);

            ResponseContractDTO responseContractDTO = new ResponseContractDTO();

            using (var httpClient = new HttpClient())
            {
                string jsonNewContract = JsonConvert.SerializeObject(contractToSend);

                //Convert object properties in lowercase
                var newContractObject = JObject.Parse(jsonNewContract, new JsonLoadSettings());
                JsonHelpers.ChangePropertiesToLowerCase(newContractObject);

                try
                {
                    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    httpClient.DefaultRequestHeaders.Add("Authorization", _keyNUASettings.API_Token);
                    httpClient.DefaultRequestHeaders.Add("x-api-key", _keyNUASettings.API_Key);

                    var builder = new UriBuilder(new Uri(_keyNUASettings.API_Url + EndPointURL));
                    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Put, builder.Uri);

                    request.Content = new StringContent(newContractObject.ToString(Formatting.None), Encoding.UTF8, "application/json");//CONTENT-TYPE header

                    using (var response = await httpClient.SendAsync(request))
                    {
                        string apiResponse = await response.Content.ReadAsStringAsync();

                        responseContractDTO = JsonConvert.DeserializeObject <ResponseContractDTO>(apiResponse);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            return(responseContractDTO);
        }