|
@ -8,30 +8,30 @@ using Microsoft.Extensions.Options; |
|
|
using System.Net.Http; |
|
|
using System.Net.Http; |
|
|
using Newtonsoft.Json; |
|
|
using Newtonsoft.Json; |
|
|
using Microsoft.AspNetCore.Authentication; |
|
|
using Microsoft.AspNetCore.Authentication; |
|
|
|
|
|
using WebMVC.Services.Utilities; |
|
|
|
|
|
|
|
|
namespace Microsoft.eShopOnContainers.WebMVC.Services |
|
|
namespace Microsoft.eShopOnContainers.WebMVC.Services |
|
|
{ |
|
|
{ |
|
|
public class OrderingService : IOrderingService |
|
|
public class OrderingService : IOrderingService |
|
|
{ |
|
|
{ |
|
|
private HttpClient _apiClient; |
|
|
|
|
|
|
|
|
private IHttpClient _apiClient; |
|
|
private readonly string _remoteServiceBaseUrl; |
|
|
private readonly string _remoteServiceBaseUrl; |
|
|
private readonly IOptionsSnapshot<AppSettings> _settings; |
|
|
private readonly IOptionsSnapshot<AppSettings> _settings; |
|
|
private readonly IHttpContextAccessor _httpContextAccesor; |
|
|
private readonly IHttpContextAccessor _httpContextAccesor; |
|
|
|
|
|
|
|
|
public OrderingService(IOptionsSnapshot<AppSettings> settings, IHttpContextAccessor httpContextAccesor) |
|
|
|
|
|
|
|
|
public OrderingService(IOptionsSnapshot<AppSettings> settings, IHttpContextAccessor httpContextAccesor, IHttpClient httpClient) |
|
|
{ |
|
|
{ |
|
|
_remoteServiceBaseUrl = $"{settings.Value.OrderingUrl}/api/v1/orders"; |
|
|
_remoteServiceBaseUrl = $"{settings.Value.OrderingUrl}/api/v1/orders"; |
|
|
_settings = settings; |
|
|
_settings = settings; |
|
|
_httpContextAccesor = httpContextAccesor; |
|
|
_httpContextAccesor = httpContextAccesor; |
|
|
|
|
|
_apiClient = httpClient; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async public Task<Order> GetOrder(ApplicationUser user, string Id) |
|
|
async public Task<Order> GetOrder(ApplicationUser user, string Id) |
|
|
{ |
|
|
{ |
|
|
var context = _httpContextAccesor.HttpContext; |
|
|
var context = _httpContextAccesor.HttpContext; |
|
|
var token = await context.Authentication.GetTokenAsync("access_token"); |
|
|
var token = await context.Authentication.GetTokenAsync("access_token"); |
|
|
|
|
|
|
|
|
_apiClient = new HttpClient(); |
|
|
|
|
|
_apiClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); |
|
|
|
|
|
|
|
|
_apiClient.Inst.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); |
|
|
|
|
|
|
|
|
var ordersUrl = $"{_remoteServiceBaseUrl}/{Id}"; |
|
|
var ordersUrl = $"{_remoteServiceBaseUrl}/{Id}"; |
|
|
var dataString = await _apiClient.GetStringAsync(ordersUrl); |
|
|
var dataString = await _apiClient.GetStringAsync(ordersUrl); |
|
@ -46,8 +46,7 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services |
|
|
var context = _httpContextAccesor.HttpContext; |
|
|
var context = _httpContextAccesor.HttpContext; |
|
|
var token = await context.Authentication.GetTokenAsync("access_token"); |
|
|
var token = await context.Authentication.GetTokenAsync("access_token"); |
|
|
|
|
|
|
|
|
_apiClient = new HttpClient(); |
|
|
|
|
|
_apiClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); |
|
|
|
|
|
|
|
|
_apiClient.Inst.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); |
|
|
|
|
|
|
|
|
var ordersUrl = _remoteServiceBaseUrl; |
|
|
var ordersUrl = _remoteServiceBaseUrl; |
|
|
var dataString = await _apiClient.GetStringAsync(ordersUrl); |
|
|
var dataString = await _apiClient.GetStringAsync(ordersUrl); |
|
@ -77,17 +76,15 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services |
|
|
var context = _httpContextAccesor.HttpContext; |
|
|
var context = _httpContextAccesor.HttpContext; |
|
|
var token = await context.Authentication.GetTokenAsync("access_token"); |
|
|
var token = await context.Authentication.GetTokenAsync("access_token"); |
|
|
|
|
|
|
|
|
_apiClient = new HttpClient(); |
|
|
|
|
|
_apiClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); |
|
|
|
|
|
_apiClient.DefaultRequestHeaders.Add("x-requestid", order.RequestId.ToString()); |
|
|
|
|
|
|
|
|
_apiClient.Inst.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); |
|
|
|
|
|
_apiClient.Inst.DefaultRequestHeaders.Add("x-requestid", order.RequestId.ToString()); |
|
|
|
|
|
|
|
|
var ordersUrl = $"{_remoteServiceBaseUrl}/new"; |
|
|
var ordersUrl = $"{_remoteServiceBaseUrl}/new"; |
|
|
order.CardTypeId = 1; |
|
|
order.CardTypeId = 1; |
|
|
order.CardExpirationApiFormat(); |
|
|
order.CardExpirationApiFormat(); |
|
|
SetFakeIdToProducts(order); |
|
|
SetFakeIdToProducts(order); |
|
|
|
|
|
|
|
|
StringContent content = new StringContent(JsonConvert.SerializeObject(order), System.Text.Encoding.UTF8, "application/json"); |
|
|
|
|
|
|
|
|
|
|
|
var response = await _apiClient.PostAsync(ordersUrl, content); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var response = await _apiClient.PostAsync(ordersUrl, order); |
|
|
|
|
|
|
|
|
if (response.StatusCode == System.Net.HttpStatusCode.InternalServerError) |
|
|
if (response.StatusCode == System.Net.HttpStatusCode.InternalServerError) |
|
|
throw new Exception("Error creating order, try later"); |
|
|
throw new Exception("Error creating order, try later"); |
|
|