Skip to content

Commit ac1a61c

Browse files
committed
Add project files.
1 parent 6c713be commit ac1a61c

File tree

9 files changed

+579
-0
lines changed

9 files changed

+579
-0
lines changed

FmgLib.HttpClientHelper.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.8.34511.84
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FmgLib.HttpClientHelper", "FmgLib.HttpClientHelper\FmgLib.HttpClientHelper.csproj", "{16F17923-0D8E-44D7-A043-6462DFED566A}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{16F17923-0D8E-44D7-A043-6462DFED566A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{16F17923-0D8E-44D7-A043-6462DFED566A}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{16F17923-0D8E-44D7-A043-6462DFED566A}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{16F17923-0D8E-44D7-A043-6462DFED566A}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {6DF6D6E8-C42C-4D24-AB06-59A6FCE1ED95}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace FmgLib.HttpClientHelper;
2+
3+
public class BasicAuthModel
4+
{
5+
public string UserName { get; set;}
6+
public string Password { get; set;}
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace FmgLib.HttpClientHelper;
2+
3+
public enum ClientContentType
4+
{
5+
Json,
6+
Xml,
7+
Html,
8+
Text
9+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Net;
2+
using System.Net.Http.Headers;
3+
4+
namespace FmgLib.HttpClientHelper;
5+
6+
public class ClientResponse
7+
{
8+
public string? ResponseStr { get; set; }
9+
public bool IsSuccess { get; set; }
10+
public HttpStatusCode StatusCode { get; set; }
11+
public HttpContentHeaders? Header { get; set; }
12+
public string? ErrorMessage { get; set; }
13+
public ClientContentType? ContentType
14+
{
15+
get
16+
{
17+
if (Header is null)
18+
return null;
19+
20+
if ((bool)(Header?.ContentType?.MediaType?.Contains("json", StringComparison.InvariantCultureIgnoreCase)))
21+
return ClientContentType.Json;
22+
else if ((bool)(Header?.ContentType?.MediaType?.Contains("xml", StringComparison.InvariantCultureIgnoreCase)))
23+
return ClientContentType.Xml;
24+
else if ((bool)(Header?.ContentType?.MediaType?.Contains("html", StringComparison.InvariantCultureIgnoreCase)))
25+
return ClientContentType.Html;
26+
else
27+
return ClientContentType.Text;
28+
}
29+
}
30+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
using System.Text.Json;
2+
using System.Xml.Serialization;
3+
4+
namespace FmgLib.HttpClientHelper;
5+
6+
public static class ConvertExtensions
7+
{
8+
public static TModel? TryGenerateModel<TModel>(this ClientResponse response) where TModel : class
9+
{
10+
if (response == null)
11+
return default!;
12+
13+
if (!response.IsSuccess)
14+
return default!;
15+
16+
if (string.IsNullOrEmpty(response.ResponseStr))
17+
return default!;
18+
19+
if (response.ResponseStr.TryParseFromJson(out TModel modelJson))
20+
return modelJson;
21+
22+
if (response.ResponseStr.TryParseFromXml(out TModel modelXml))
23+
return modelXml;
24+
25+
return typeof(TModel) == typeof(string) ? (response.ResponseStr as TModel) : default!;
26+
}
27+
28+
public static TModel? TryGenerateModel<TModel>(this ClientResponse response, ClientContentType? contentType) where TModel : class
29+
{
30+
if (response == null)
31+
return default!;
32+
33+
if (contentType == null)
34+
return default!;
35+
36+
if (!response.IsSuccess)
37+
return default!;
38+
39+
if (string.IsNullOrEmpty(response.ResponseStr))
40+
return default!;
41+
42+
if (contentType == ClientContentType.Json && response.ResponseStr.TryParseFromJson(out TModel modelJson))
43+
return modelJson;
44+
else if (contentType == ClientContentType.Xml && response.ResponseStr.TryParseFromXml(out TModel modelXml))
45+
return modelXml;
46+
47+
return typeof(TModel) == typeof(string) ? (response.ResponseStr as TModel) : default!;
48+
}
49+
50+
51+
public static bool TryParseToJson<TModel>(this TModel model, out string json) where TModel : class
52+
{
53+
try
54+
{
55+
json = JsonSerializer.Serialize(model);
56+
return true;
57+
}
58+
catch
59+
{
60+
json = null;
61+
return false;
62+
}
63+
}
64+
65+
public static bool TryParseFromJson<TModel>(this string str, out TModel model) where TModel : class
66+
{
67+
try
68+
{
69+
model = JsonSerializer.Deserialize<TModel>(str);
70+
return true;
71+
}
72+
catch
73+
{
74+
model = null;
75+
return false;
76+
}
77+
}
78+
79+
public static bool TryParseToXml<TModel>(this TModel model, out string xml) where TModel : class
80+
{
81+
try
82+
{
83+
var serializer = new XmlSerializer(typeof(TModel));
84+
using (var sw = new StringWriter())
85+
{
86+
serializer.Serialize(sw, model);
87+
xml = sw.ToString();
88+
return true;
89+
}
90+
}
91+
catch
92+
{
93+
xml = null;
94+
return false;
95+
}
96+
}
97+
98+
public static bool TryParseFromXml<TModel>(this string str, out TModel model) where TModel : class
99+
{
100+
try
101+
{
102+
var serializer = new XmlSerializer(typeof(TModel));
103+
using (var sr = new StringReader(str))
104+
{
105+
model = (TModel)serializer.Deserialize(sr);
106+
return true;
107+
}
108+
}
109+
catch
110+
{
111+
model = null;
112+
return false;
113+
}
114+
}
115+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<PackageId>FmgLib.HttpClientHelper</PackageId>
8+
<Summary>It is a library that offers you dynamism for the HttpClient service and contains methods that parse the returned response to the model you want.</Summary>
9+
<Title>FmgLib.HttpClientHelper</Title>
10+
<Version>1.0.3</Version>
11+
<Authors>FmgYazılım</Authors>
12+
<Company>Fmg Yazılım</Company>
13+
<Copyright>©2024</Copyright>
14+
<PackageProjectUrl>https://github.com/FmgLib/FmgLib.HttpClientHelper</PackageProjectUrl>
15+
<RepositoryUrl>https://github.com/FmgLib/FmgLib.HttpClientHelper</RepositoryUrl>
16+
<UseFullSemVerForNuGet>false</UseFullSemVerForNuGet>
17+
<PackageTags>HttpClient, Client, Helper</PackageTags>
18+
<Description>It is a library that offers you dynamism for the HttpClient service and contains methods that parse the returned response to the model you want.</Description>
19+
<PackageIcon>nuget.png</PackageIcon>
20+
<AssemblyVersion>1.0.3</AssemblyVersion>
21+
<AssemblyFileVersion>1.0.3</AssemblyFileVersion>
22+
<PackageVersion>1.0.3</PackageVersion>
23+
<FileVersion>1.0.3</FileVersion>
24+
</PropertyGroup>
25+
26+
<ItemGroup>
27+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
28+
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
29+
</ItemGroup>
30+
31+
</Project>

0 commit comments

Comments
 (0)