|
| 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 | + |
0 commit comments