diff --git a/dotnet/src/dotnetframework/GxClasses/Domain/GxHttpClient.cs b/dotnet/src/dotnetframework/GxClasses/Domain/GxHttpClient.cs index 00de45e5d..a6a89915a 100644 --- a/dotnet/src/dotnetframework/GxClasses/Domain/GxHttpClient.cs +++ b/dotnet/src/dotnetframework/GxClasses/Domain/GxHttpClient.cs @@ -21,6 +21,7 @@ namespace GeneXus.Http.Client using GeneXus.Application; using GeneXus.Configuration; using GeneXus.Utils; + #if NETCORE using Microsoft.AspNetCore.WebUtilities; #endif @@ -2002,6 +2003,10 @@ public void GetHeader(string name, out double value) { value = Convert.ToDouble(GetHeader(name)); } + public void GetHeader(string name, out Decimal value) + { + value = Convert.ToDecimal(GetHeader(name)); + } public void GetHeader(string name, out string value) { value = GetHeader(name); @@ -2010,6 +2015,10 @@ public void GetHeader(string name, out DateTime value) { value = Convert.ToDateTime(GetHeader(name)); } + public void GetHeader(string name, out bool value) + { + value = Convert.ToBoolean(GetHeader(name)); + } public void AddCertificate(string cert) { Regex r = new Regex(@"(\s*\((?'fName'\S+)\s*\,\s*(?'pass'\S+)\s*\)|(?'fName'\S+))"); diff --git a/dotnet/src/dotnetframework/GxClasses/Helpers/GXRestAPIClient.cs b/dotnet/src/dotnetframework/GxClasses/Helpers/GXRestAPIClient.cs index 295e266a0..6874e930d 100644 --- a/dotnet/src/dotnetframework/GxClasses/Helpers/GXRestAPIClient.cs +++ b/dotnet/src/dotnetframework/GxClasses/Helpers/GXRestAPIClient.cs @@ -50,6 +50,7 @@ public GXRestAPIClient() private string httpMethod = "GET"; private Dictionary _queryVars = new Dictionary(); + private Dictionary _headerVars = new Dictionary(); private Dictionary _bodyVars = new Dictionary(); //private Dictionary _pathVars = new Dictionary(); private Dictionary _responseData = new Dictionary(); @@ -61,6 +62,70 @@ public GXRestAPIClient() private int responseCode = 0; private string responseMessage = String.Empty; + + #region "Header Vars" + + public void AddHeaderVar(String varName, String varValue) + { + _headerVars[varName] = GXUtil.UrlEncode(varValue); + } + public void AddHeaderVar(String varName, int varValue) + { + _headerVars[varName] = varValue.ToString(); + } + public void AddHeaderVar(String varName, long varValue) + { + _headerVars[varName] = varValue.ToString(); + } + + public void AddHeaderVar(String varName, short varValue) + { + _headerVars[varName] = varValue.ToString(); + } + public void AddHeaderVar(String varName, Decimal varValue) + { + _headerVars[varName] = varValue.ToString(System.Globalization.CultureInfo.InvariantCulture); + } + public void AddHeaderVar(String varName, DateTime varValue) + { + _headerVars[varName] = varValue.ToString(DATE_FORMAT); + } + public void AddHeaderVar(String varName, DateTime varValue, bool hasMilliseconds) + { + string fmt = DATETIME_FORMAT; + if (hasMilliseconds) + fmt = DATETIME_MS_FORMAT; + _headerVars[varName] = varValue.ToString(fmt); + } + public void AddHeaderVar(String varName, Guid varValue) + { + _headerVars[varName] = varValue.ToString(); + } + public void AddHeaderVar(String varName, Geospatial varValue) + { + _headerVars[varName] = GXUtil.UrlEncode(varValue.ToString()); + } + public void AddHeaderVar(String varName, bool varValue) + { + _headerVars[varName] = StringUtil.BoolToStr(varValue); + } + public void AddHeaderVar(String varName, GxUserType varValue) + { + if (varValue != null) + { + _headerVars[varName] = varValue.ToJSonString(); + } + } + public void AddHeaderVar(String varName, IGxCollection varValue) + { + if (varValue != null) + { + _headerVars[varName] = varValue.ToJSonString(); + } + } + #endregion + + #region "Query Vars" public void AddQueryVar(String varName, String varValue) { _queryVars[varName] = GXUtil.UrlEncode(varValue); @@ -127,6 +192,9 @@ public void AddQueryVar(String varName, IGxCollection varValue) _bodyVars[varName] = varValue.ToJSonString(); } } + #endregion + + #region "Body Vars" public void AddBodyVar(String varName, DateTime varValue) { @@ -192,6 +260,79 @@ public void AddBodyVar(String varName, IGxCollection varValue) } } + #endregion + + #region "Get Header Vars" + public string GetHeaderString(string varName) + { + return httpClient.GetHeader(varName); + } + + public DateTime GetHeaderDate(string varName) + { + string val = GetHeaderString(varName); + if (val.StartsWith(DATE_NULL)) + return DateTimeUtil.NullDate(); + return DateTime.ParseExact(val, DATE_FORMAT, System.Globalization.CultureInfo.InvariantCulture); + } + + public DateTime GetHeaderDateTime(string varName, bool hasMilliseconds) + { + string val = GetHeaderString(varName); + if (val.StartsWith(DATETIME_NULL)) + return DateTimeUtil.NullDate(); + string fmt = DATETIME_FORMAT; + if (hasMilliseconds) + fmt = DATETIME_MS_FORMAT; + return DateTime.ParseExact(val, fmt, System.Globalization.CultureInfo.InvariantCulture); + } + + public bool GetHeaderBool(string varName) + { + httpClient.GetHeader(varName, out bool val); + return val; + } + public Guid GetHeaderGuid(string varName) + { + return Guid.Parse(GetHeaderString(varName)); + } + + public Decimal GetHeaderNum(string varName) + { + httpClient.GetHeader(varName, out Decimal val); + return val; + } + public long GetHeaderLong(string varName) + { + httpClient.GetHeader(varName, out long val); + return val; + } + public int GetHeaderInt(string varName) + { + httpClient.GetHeader(varName, out int val); + return val; + } + + public short GetHeaderShort(string varName) + { + httpClient.GetHeader(varName, out short val); + return val; + } + + public Geospatial GetHeaderGeospatial(string varName) + { + Geospatial g = new Geospatial(GetHeaderString(varName)); + if (Geospatial.IsNullOrEmpty(g)) + { + g.FromGeoJSON(GetJsonStr(varName)); + } + return g; + } + + #endregion + + #region "Get Body Vars" + public string GetBodyString(string varName) { return GetJsonStr(varName); @@ -325,6 +466,7 @@ public GxSimpleCollection GetBodySimpleCollection(string varName) } return collection; } + #endregion public void AddUploadFile(string FilePath, string name) { @@ -336,6 +478,13 @@ public void AddUploadFile(string FilePath, string name) public void RestExecute() { this.ErrorCode = 0; + if (_headerVars.Count > 0) + { + foreach (string key in _headerVars.Keys) + { + httpClient.AddHeader(key, _headerVars[key]); + } + } _queryString = String.Empty; if (_queryVars.Count > 0) {