|
|
@ -1,29 +1,29 @@ |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Microsoft.eShopOnContainers.WebMVC.ViewModels; |
|
|
|
using Microsoft.AspNetCore.Authentication; |
|
|
|
using Microsoft.AspNetCore.Http; |
|
|
|
using System.Net.Http; |
|
|
|
using Microsoft.eShopOnContainers.WebMVC.ViewModels; |
|
|
|
using Microsoft.Extensions.Options; |
|
|
|
using Newtonsoft.Json; |
|
|
|
using Microsoft.AspNetCore.Authentication; |
|
|
|
using Microsoft.eShopOnContainers.WebMVC.Extensions; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using System.Net.Http; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using WebMVC.Services.Utilities; |
|
|
|
|
|
|
|
namespace Microsoft.eShopOnContainers.WebMVC.Services |
|
|
|
{ |
|
|
|
public class BasketService : IBasketService |
|
|
|
{ |
|
|
|
private readonly IOptionsSnapshot<AppSettings> _settings; |
|
|
|
private HttpClient _apiClient; |
|
|
|
private IHttpClient _apiClient; |
|
|
|
private readonly string _remoteServiceBaseUrl; |
|
|
|
private IHttpContextAccessor _httpContextAccesor; |
|
|
|
|
|
|
|
public BasketService(IOptionsSnapshot<AppSettings> settings, IHttpContextAccessor httpContextAccesor) |
|
|
|
public BasketService(IOptionsSnapshot<AppSettings> settings, IHttpContextAccessor httpContextAccesor, IHttpClient httpClient) |
|
|
|
{ |
|
|
|
_settings = settings; |
|
|
|
_remoteServiceBaseUrl = _settings.Value.BasketUrl; |
|
|
|
_httpContextAccesor = httpContextAccesor; |
|
|
|
_apiClient = httpClient; |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<Basket> GetBasket(ApplicationUser user) |
|
|
@ -31,8 +31,7 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services |
|
|
|
var context = _httpContextAccesor.HttpContext; |
|
|
|
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 basketUrl = $"{_remoteServiceBaseUrl}/{user.Id.ToString()}"; |
|
|
|
var dataString = await _apiClient.GetStringAsync(basketUrl); |
|
|
@ -52,13 +51,12 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services |
|
|
|
{ |
|
|
|
var context = _httpContextAccesor.HttpContext; |
|
|
|
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 basketUrl = _remoteServiceBaseUrl; |
|
|
|
StringContent content = new StringContent(JsonConvert.SerializeObject(basket), System.Text.Encoding.UTF8, "application/json"); |
|
|
|
var response = await _apiClient.PostAsync(basketUrl, content); |
|
|
|
|
|
|
|
var response = await _apiClient.PostAsync(basketUrl, basket); |
|
|
|
|
|
|
|
return basket; |
|
|
|
} |
|
|
@ -120,8 +118,7 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services |
|
|
|
var context = _httpContextAccesor.HttpContext; |
|
|
|
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 basketUrl = $"{_remoteServiceBaseUrl}/{user.Id.ToString()}"; |
|
|
|
var response = await _apiClient.DeleteAsync(basketUrl); |
|
|
|
|
|
|
|