|
| 1 | +using Microsoft.AspNetCore.Authentication; |
| 2 | +using Microsoft.AspNetCore.Authorization; |
| 3 | +using Microsoft.AspNetCore.Mvc; |
| 4 | +using Microsoft.eShopOnContainers.BuildingBlocks.Resilience.Http; |
| 5 | +using Microsoft.eShopOnContainers.WebMVC.Services; |
| 6 | +using Microsoft.eShopOnContainers.WebMVC.ViewModels; |
| 7 | +using System; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.Linq; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | +namespace WebMVC.Controllers |
| 13 | +{ |
| 14 | + class TestPayload |
| 15 | + { |
| 16 | + public int CatalogItemId { get; set; } |
| 17 | + public string BasketId { get; set; } |
| 18 | + |
| 19 | + public int Quantity { get; set; } |
| 20 | + } |
| 21 | + |
| 22 | + [Authorize] |
| 23 | + public class TestController : Controller |
| 24 | + { |
| 25 | + private readonly IHttpClient _client; |
| 26 | + private readonly IIdentityParser<ApplicationUser> _appUserParser; |
| 27 | + public TestController(IHttpClient client, IIdentityParser<ApplicationUser> identityParser) |
| 28 | + { |
| 29 | + _client = client; |
| 30 | + _appUserParser = identityParser; |
| 31 | + } |
| 32 | + |
| 33 | + public async Task<IActionResult> Ocelot() |
| 34 | + { |
| 35 | + var url = "http://apigw/purchase-bff/api/v1/basket/items"; |
| 36 | + var payload = new TestPayload() |
| 37 | + { |
| 38 | + CatalogItemId = 1, |
| 39 | + Quantity = 1, |
| 40 | + BasketId = _appUserParser.Parse(User).Id |
| 41 | + }; |
| 42 | + var token = await HttpContext.GetTokenAsync("access_token"); |
| 43 | + var response = await _client.PostAsync<TestPayload>(url, payload, token); |
| 44 | + |
| 45 | + if (response.IsSuccessStatusCode) |
| 46 | + { |
| 47 | + var str = await response.Content.ReadAsStringAsync(); |
| 48 | + return Ok(str); |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + return Ok(new { response.StatusCode, response.ReasonPhrase }); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | +} |
0 commit comments