Skip to content

Commit 3a74328

Browse files
committed
applying apis in web project
1 parent ac981f3 commit 3a74328

37 files changed

+655
-1164
lines changed

src/Basket/Basket.API/Entities/BasketCartItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ public class BasketCartItem
55
public int Quantity { get; set; }
66
public string Color { get; set; }
77
public decimal Price { get; set; }
8-
public int ProductId { get; set; }
8+
public string ProductId { get; set; }
99
}
1010
}

src/Catalog/Catalog.API/Startup.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ public void ConfigureServices(IServiceCollection services)
3535

3636
#region Configuration Dependencies
3737

38-
services.Configure<CatalogDatabaseSettings>(
39-
Configuration.GetSection(nameof(CatalogDatabaseSettings)));
38+
services.Configure<CatalogDatabaseSettings>(Configuration.GetSection(nameof(CatalogDatabaseSettings)));
4039

4140
services.AddSingleton<ICatalogDatabaseSettings>(sp =>
4241
sp.GetRequiredService<IOptions<CatalogDatabaseSettings>>().Value);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using AspnetRunBasics.ApiCollection.Infrastructure;
2+
using AspnetRunBasics.ApiCollection.Interfaces;
3+
using AspnetRunBasics.Models;
4+
using AspnetRunBasics.Settings;
5+
using Newtonsoft.Json;
6+
using System;
7+
using System.Net.Http;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
namespace AspnetRunBasics.ApiCollection
12+
{
13+
public class BasketApi : BaseHttpClientWithFactory, IBasketApi
14+
{
15+
private readonly IApiSettings _settings;
16+
17+
public BasketApi(IHttpClientFactory factory, IApiSettings settings)
18+
: base(factory)
19+
{
20+
_settings = settings;
21+
}
22+
23+
public async Task<BasketModel> GetBasket(string userName)
24+
{
25+
var message = new HttpRequestBuilder(_settings.BaseAddress)
26+
.SetPath(_settings.BasketPath + "//" + userName)
27+
.HttpMethod(HttpMethod.Get)
28+
.GetHttpMessage();
29+
30+
return await SendRequest<BasketModel>(message);
31+
}
32+
33+
public async Task<BasketModel> AddBasket(BasketModel model)
34+
{
35+
var message = new HttpRequestBuilder(_settings.BaseAddress)
36+
.SetPath(_settings.BasketPath)
37+
.HttpMethod(HttpMethod.Post)
38+
.GetHttpMessage();
39+
40+
var json = JsonConvert.SerializeObject(model);
41+
message.Content = new StringContent(json, Encoding.UTF8, "application/json");
42+
43+
return await SendRequest<BasketModel>(message);
44+
}
45+
46+
public async Task CheckoutBasket(BasketCheckoutModel model)
47+
{
48+
var postPath = _settings.BasketPath + "/Checkout";
49+
50+
var message = new HttpRequestBuilder(_settings.BaseAddress)
51+
.SetPath(postPath)
52+
.HttpMethod(HttpMethod.Post)
53+
.GetHttpMessage();
54+
55+
var json = JsonConvert.SerializeObject(model);
56+
message.Content = new StringContent(json, Encoding.UTF8, "application/json");
57+
58+
await SendRequest<BasketCheckoutModel>(message);
59+
}
60+
}
61+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using AspnetRunBasics.ApiCollection.Infrastructure;
2+
using AspnetRunBasics.ApiCollection.Interfaces;
3+
using AspnetRunBasics.Models;
4+
using AspnetRunBasics.Settings;
5+
using Newtonsoft.Json;
6+
using System.Collections.Generic;
7+
using System.Net.Http;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
namespace AspnetRunBasics.ApiCollection
12+
{
13+
public class CatalogApi : BaseHttpClientWithFactory, ICatalogApi
14+
{
15+
private readonly IApiSettings _settings;
16+
17+
public CatalogApi(IHttpClientFactory factory, IApiSettings settings)
18+
: base(factory)
19+
{
20+
_settings = settings;
21+
}
22+
23+
public async Task<IEnumerable<CatalogModel>> GetCatalog()
24+
{
25+
var message = new HttpRequestBuilder(_settings.BaseAddress)
26+
.SetPath(_settings.CatalogPath)
27+
.HttpMethod(HttpMethod.Get)
28+
.GetHttpMessage();
29+
30+
return await SendRequest<IEnumerable<CatalogModel>>(message);
31+
}
32+
33+
public async Task<CatalogModel> GetCatalog(string id)
34+
{
35+
var message = new HttpRequestBuilder(_settings.BaseAddress)
36+
.SetPath(_settings.CatalogPath + "//" + id)
37+
.HttpMethod(HttpMethod.Get)
38+
.GetHttpMessage();
39+
40+
return await SendRequest<CatalogModel>(message);
41+
}
42+
43+
public async Task<IEnumerable<CatalogModel>> GetCatalogByCategory(string category)
44+
{
45+
var queryPath = _settings.CatalogPath + "/GetProductByCategory/" + category;
46+
47+
var message = new HttpRequestBuilder(_settings.BaseAddress)
48+
.SetPath(queryPath)
49+
.HttpMethod(HttpMethod.Get)
50+
.GetHttpMessage();
51+
52+
return await SendRequest<IEnumerable<CatalogModel>>(message);
53+
}
54+
55+
public async Task<CatalogModel> CreateCatalog(CatalogModel catalogModel)
56+
{
57+
var message = new HttpRequestBuilder(_settings.BaseAddress)
58+
.SetPath(_settings.CatalogPath)
59+
.HttpMethod(HttpMethod.Post)
60+
.GetHttpMessage();
61+
62+
var json = JsonConvert.SerializeObject(catalogModel);
63+
message.Content = new StringContent(json, Encoding.UTF8, "application/json");
64+
65+
return await SendRequest<CatalogModel>(message);
66+
}
67+
}
68+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
using System;
2+
using System.Web;
3+
4+
namespace AspnetRunBasics.ApiCollection.Infrastructure
5+
{
6+
public class ApiBuilder
7+
{
8+
private readonly string _fullUrl;
9+
private UriBuilder _builder;
10+
11+
public ApiBuilder(string url)
12+
{
13+
_fullUrl = url;
14+
_builder = new UriBuilder(url);
15+
}
16+
17+
public Uri GetUri() => _builder.Uri;
18+
19+
public ApiBuilder Scheme(string scheme)
20+
{
21+
_builder.Scheme = scheme;
22+
return this;
23+
}
24+
25+
public ApiBuilder Host(string host)
26+
{
27+
_builder.Host = host;
28+
return this;
29+
}
30+
31+
public ApiBuilder Port(int port)
32+
{
33+
_builder.Port = port;
34+
return this;
35+
}
36+
37+
public ApiBuilder AddToPath(string path)
38+
{
39+
IncludePath(path);
40+
return this;
41+
}
42+
43+
public ApiBuilder SetPath(string path)
44+
{
45+
_builder.Path = path;
46+
return this;
47+
}
48+
49+
public void IncludePath(string path)
50+
{
51+
if (string.IsNullOrEmpty(_builder.Path)
52+
|| _builder.Path == "/")
53+
{
54+
_builder.Path = path;
55+
}
56+
else
57+
{
58+
var newPath = $"{_builder.Path}/{path}";
59+
_builder.Path = newPath.Replace("//", "/");
60+
}
61+
}
62+
63+
public ApiBuilder Fragment(string fragment)
64+
{
65+
_builder.Fragment = fragment;
66+
return this;
67+
}
68+
69+
public ApiBuilder SetSubdomain(string subdomain)
70+
{
71+
_builder.Host = string.Concat(subdomain, ".", new Uri(_fullUrl).Host);
72+
return this;
73+
}
74+
75+
public bool HasSubdomain()
76+
{
77+
return _builder.Uri.HostNameType == UriHostNameType.Dns
78+
&& _builder.Uri.Host.Split('.').Length > 2;
79+
}
80+
81+
public ApiBuilder AddQueryString(string name, string value)
82+
{
83+
var qsNv = HttpUtility.ParseQueryString(_builder.Query);
84+
qsNv[name] = string.IsNullOrEmpty(qsNv[name])
85+
? value
86+
: string.Concat(qsNv[name], ",", value);
87+
88+
_builder.Query = qsNv.ToString();
89+
90+
return this;
91+
}
92+
93+
public ApiBuilder QueryString(string queryString)
94+
{
95+
if (!string.IsNullOrEmpty(queryString))
96+
{
97+
_builder.Query = queryString;
98+
}
99+
return this;
100+
}
101+
102+
public ApiBuilder UserName(string username)
103+
{
104+
_builder.UserName = username;
105+
return this;
106+
}
107+
108+
public ApiBuilder Password(string password)
109+
{
110+
_builder.Password = password;
111+
return this;
112+
}
113+
114+
public string GetLeftPart()
115+
{
116+
return _builder.Uri.GetLeftPart(UriPartial.Path);
117+
}
118+
}
119+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Net.Http;
4+
using System.Net.Http.Formatting;
5+
using System.Threading.Tasks;
6+
7+
namespace AspnetRunBasics.ApiCollection.Infrastructure
8+
{
9+
public abstract class BaseHttpClientWithFactory
10+
{
11+
private readonly IHttpClientFactory _factory;
12+
13+
public Uri BaseAddress { get; set; }
14+
public string BasePath { get; set; }
15+
16+
public BaseHttpClientWithFactory(IHttpClientFactory factory) => _factory = factory;
17+
18+
private HttpClient GetHttpClient()
19+
{
20+
return _factory.CreateClient();
21+
}
22+
23+
public virtual async Task<T> SendRequest<T>(HttpRequestMessage request) where T : class
24+
{
25+
var client = GetHttpClient();
26+
27+
var response = await client.SendAsync(request);
28+
29+
T result = null;
30+
31+
response.EnsureSuccessStatusCode();
32+
33+
if (response.IsSuccessStatusCode)
34+
{
35+
result = await response.Content.ReadAsAsync<T>(GetFormatters());
36+
}
37+
38+
return result;
39+
}
40+
41+
protected virtual IEnumerable<MediaTypeFormatter> GetFormatters()
42+
{
43+
// Make default the JSON
44+
return new List<MediaTypeFormatter> { new JsonMediaTypeFormatter() };
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)