Skip to content

Commit 8ec8442

Browse files
committed
Update 1.0.2 - 01/10/2024
* Added a new class HttpRequest.UserAgent for use in web service requests. * Updated copyright to 2024.
1 parent 8d13901 commit 8ec8442

6 files changed

+232
-4
lines changed

CustomException.cs

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
namespace Utility.BaseClasses
55
{
6+
/// <summary>
7+
/// Extends the Exception class to give you a HttpStatusCode and Message as well as the base Exception information.
8+
/// </summary>
69
public class CustomException : Exception
710
{
811
public HttpStatusCode Status { get; set; }

Error.cs

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Utility.BaseClasses
44
{
5+
/// <summary>
6+
/// A class that allows you to store and return an HttpStatusCode and a string message. This is very useful when returning responses from API requests where you need to return the status code and a message about why the status code was returned.
7+
/// </summary>
58
public class Error
69
{
710
public HttpStatusCode Status { get; set; }

HttpRequest/UserAgent.cs

+204
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Utility.BaseClasses.HttpRequest
6+
{
7+
/// <summary>
8+
/// The static HttpRequest.UserAgent class is used to correctly format the UserAgent string that is sent in the request header of web service calls. It simplifies the process so all you need to do is call the Get() method.The static HttpRequest.UserAgent class is used to correctly format the UserAgent string that is sent in the request header of web service calls. It simplifies the process so all you need to do is call the Get() method.
9+
/// </summary>
10+
public static class UserAgent
11+
{
12+
private static string UserAgentString { get; set; } = string.Empty;
13+
private static string Name { get; set; } = string.Empty;
14+
private static string Version { get; set; } = string.Empty;
15+
16+
/// <summary>
17+
/// ShowErrors flag allows you to toggle on and off the displaying of errors in the HttpRequest.UserAgent methods. Useful when you are building a console application and you plan to check if the value is set rather than dealing with the error stopping the process and triggering your exception handling call.
18+
/// </summary>
19+
public static bool ShowErrors { get; set; } = true;
20+
21+
/// <summary>
22+
/// Simple method to return the UserAgent string.
23+
/// </summary>
24+
/// <returns>{Name}/{Version} i.e. "Utility.BaseClasses/1.0"</returns>
25+
public static string Get()
26+
{
27+
return Get(string.Empty);
28+
}
29+
/// <summary>
30+
/// Method to return the UserAgent, Name, or Version values. By default it will return 'UserAgent'.
31+
/// </summary>
32+
/// <param name="value2Return">Name, Version, or UserAgent (Default: 'UserAgent')</param>
33+
/// <returns>{Name}/{Version} i.e. "Utility.BaseClasses/1.0"</returns>
34+
public static string Get(string value2Return = "")
35+
{
36+
string value = string.Empty;
37+
try
38+
{
39+
switch (value2Return.ToLower())
40+
{
41+
case "name":
42+
if (!(string.IsNullOrEmpty(Name)))
43+
value = Name;
44+
else
45+
{
46+
SetUserAgentValues();
47+
value = Name;
48+
}
49+
break;
50+
case "version":
51+
if (!(string.IsNullOrEmpty(Version)))
52+
value = Version;
53+
else
54+
{
55+
SetUserAgentValues();
56+
value = Version;
57+
}
58+
break;
59+
default:
60+
if (!(string.IsNullOrEmpty(UserAgentString)))
61+
value = UserAgentString;
62+
else
63+
{
64+
SetUserAgentValues();
65+
value = UserAgentString;
66+
}
67+
break;
68+
}
69+
}
70+
catch (Exception ex)
71+
{
72+
if (ShowErrors)
73+
throw ex;
74+
}
75+
76+
return value;
77+
}
78+
79+
private static void SetUserAgentValues()
80+
{
81+
if (string.IsNullOrEmpty(Name) && string.IsNullOrEmpty(Version))
82+
Set(string.Empty, string.Empty);
83+
else if (string.IsNullOrEmpty(Name) && (!(string.IsNullOrEmpty(Version))))
84+
Set(string.Empty, Version);
85+
else if (!(string.IsNullOrEmpty(Name)) && (string.IsNullOrEmpty(Version)))
86+
Set(Name, string.Empty);
87+
else if (string.IsNullOrEmpty(Name) && (!(string.IsNullOrEmpty(Version))))
88+
Set(Name, string.Empty);
89+
else if (!(string.IsNullOrEmpty(Name)) && !(string.IsNullOrEmpty(Version)) && string.IsNullOrEmpty(UserAgentString))
90+
Set(Name, Version);
91+
}
92+
/// <summary>
93+
/// The Set method allows you to set the Name of the application and Version for the UserAgent if you don't like or the returned derrived values are not what you would like them to be.
94+
/// </summary>
95+
/// <param name="appName">Assembly Name of the application.</param>
96+
/// <param name="appVersion">Version of the application in x.x or x.x.x format.</param>
97+
public static void Set(string appName, string appVersion)
98+
{
99+
try
100+
{
101+
102+
if (string.IsNullOrEmpty(appName) || string.IsNullOrEmpty(appVersion))
103+
{
104+
string assemblyLocation = GetAssemblyLocation();
105+
string productVersion = string.Empty;
106+
if (System.Diagnostics.FileVersionInfo.GetVersionInfo(assemblyLocation).ProductBuildPart == 0)
107+
productVersion = string.Format("{0}.{1}", System.Diagnostics.FileVersionInfo.GetVersionInfo(assemblyLocation).ProductMajorPart
108+
, System.Diagnostics.FileVersionInfo.GetVersionInfo(assemblyLocation).ProductMinorPart);
109+
else
110+
productVersion = string.Format("{0}.{1}.{2}", System.Diagnostics.FileVersionInfo.GetVersionInfo(assemblyLocation).ProductMajorPart
111+
, System.Diagnostics.FileVersionInfo.GetVersionInfo(assemblyLocation).ProductMinorPart
112+
, System.Diagnostics.FileVersionInfo.GetVersionInfo(assemblyLocation).ProductBuildPart);
113+
114+
appName = GetAssemblyName();
115+
appVersion = productVersion ?? "0.0.0";
116+
}
117+
}
118+
catch (Exception ex)
119+
{
120+
if (ShowErrors)
121+
throw ex;
122+
else
123+
{
124+
appName = "Unknown";
125+
appVersion = "0.0";
126+
}
127+
}
128+
Name = appName;
129+
Version = appVersion;
130+
UserAgentString = string.Format("{0}/{1}", appName, appVersion);
131+
}
132+
private static string GetAssemblyLocation()
133+
{
134+
string location = string.Empty;
135+
136+
//try to get it from the System.Reflection.Assembly.GetExecutingAssembly().Location
137+
location = GetAssemblyLocation("GetExecutingAssembly");
138+
139+
if (string.IsNullOrEmpty(location))
140+
location = GetAssemblyLocation("GetEntryAssembly");
141+
142+
if (string.IsNullOrEmpty(location))
143+
location = GetAssemblyLocation("GetCallingAssembly");
144+
145+
return location;
146+
}
147+
private static string GetAssemblyLocation(string method2use)
148+
{
149+
try
150+
{
151+
switch (method2use.ToLower())
152+
{
153+
case "getexecutingassembly":
154+
return System.Reflection.Assembly.GetExecutingAssembly().Location;
155+
case "getentryassembly":
156+
return System.Reflection.Assembly.GetEntryAssembly().Location;
157+
default:
158+
case "getcallingassembly":
159+
return System.Reflection.Assembly.GetCallingAssembly().Location;
160+
}
161+
}
162+
catch
163+
{
164+
return string.Empty;
165+
}
166+
}
167+
private static string GetAssemblyName()
168+
{
169+
string name = string.Empty;
170+
171+
name = GetAssemblyName("GetEntryAssembly");
172+
173+
if (string.IsNullOrEmpty(name))
174+
name = GetAssemblyName("GetCallingAssembly");
175+
176+
if (string.IsNullOrEmpty(name))
177+
name = GetAssemblyName("GetExecutingAssembly");
178+
179+
return name;
180+
}
181+
private static string GetAssemblyName(string method2use)
182+
{
183+
try
184+
{
185+
switch (method2use.ToLower())
186+
{
187+
case "getexecutingassembly":
188+
return System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
189+
case "getentryassembly":
190+
return System.Reflection.Assembly.GetEntryAssembly().GetName().Name;
191+
default:
192+
case "getcallingassembly":
193+
return System.Reflection.Assembly.GetCallingAssembly().GetName().Name;
194+
}
195+
}
196+
catch
197+
{
198+
return string.Empty;
199+
}
200+
}
201+
202+
}
203+
}
204+

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ A class that allows you to store and return an HttpStatusCode and a string messa
1111
#### CustomException
1212
***
1313
Extends the Exception class to give you a HttpStatusCode and Message as well as the base Exception information.
14+
15+
#### HttpRequest.UserAgent
16+
***
17+
The static HttpRequest.UserAgent class is used to correctly format the UserAgent string that is sent in the request header of web service calls. It simplifies the process so all you need to do is call the Get() method.The static HttpRequest.UserAgent class is used to correctly format the UserAgent string that is sent in the request header of web service calls. It simplifies the process so all you need to do is call the Get() method.
18+
19+

Release Notes.md

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ The Release Notes for Utility.BaseClasses contains information about when change
44

55
***
66

7+
**Update 1.0.2 - 01/10/2024**
8+
* Added a new class HttpRequest.UserAgent for use in web service requests.
9+
* Added .NET 8 to the TargetFrameworks list.
10+
* Updated copyright to 2024.
11+
712
**Update 1.0.1 - 03/06/2023**
813
* Corrected an issue with a CS0122 error on the Error class.
914

Utility.BaseClasses.csproj

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net20;net35;net40;net45;net451;net452;net46;net461;net462;net47;net471;net472;net48;net5.0;net6.0;net7.0</TargetFrameworks>
4+
<TargetFrameworks>net20;net35;net40;net45;net451;net452;net46;net461;net462;net47;net471;net472;net48;net5.0;net6.0;net7.0;net8.0</TargetFrameworks>
55
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
66
<Company>Woodward Coding Solutions</Company>
77
<Authors>Andy Woodward</Authors>
88
<Description>This .dll contains base classes that can be easily used in multiple projects. It reduces the need to copy class code into new projects and allows for simper updates accross all projects that reference these classes.</Description>
9-
<Copyright>Copyright © Woodward Coding Solutions 2023</Copyright>
9+
<Copyright>Copyright © Woodward Coding Solutions 2024</Copyright>
1010
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1111
<PackageTags>Utilities</PackageTags>
1212
<PackageReleaseNotes>Release Notes.md</PackageReleaseNotes>
@@ -15,10 +15,17 @@
1515
<RepositoryType>git</RepositoryType>
1616
<Title>Utility.BaseClasses</Title>
1717
<PackageReadmeFile>README.md</PackageReadmeFile>
18-
<Version>1.0.1</Version>
18+
<Version>1.0.2</Version>
19+
<IncludeSymbols>True</IncludeSymbols>
20+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
21+
<NeutralLanguage>en-US</NeutralLanguage>
22+
</PropertyGroup>
1923

24+
<PropertyGroup Condition="'$(Configuration)'=='Release'">
25+
<DocumentationFile>bin\Release\Utility.BaseClasses.xml</DocumentationFile>
2026
</PropertyGroup>
21-
<ItemGroup>
27+
28+
<ItemGroup>
2229
<None Include="Release Notes.md">
2330
<Pack>True</Pack>
2431
<PackagePath></PackagePath>

0 commit comments

Comments
 (0)