Skip to content

Commit

Permalink
- Add header methods in RESTApi classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
a-panizza_globant committed Jan 27, 2025
1 parent 6b905ed commit e24a865
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
9 changes: 9 additions & 0 deletions dotnet/src/dotnetframework/GxClasses/Domain/GxHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace GeneXus.Http.Client
using GeneXus.Application;
using GeneXus.Configuration;
using GeneXus.Utils;

#if NETCORE
using Microsoft.AspNetCore.WebUtilities;
#endif
Expand Down Expand Up @@ -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);
Expand All @@ -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+))");
Expand Down
149 changes: 149 additions & 0 deletions dotnet/src/dotnetframework/GxClasses/Helpers/GXRestAPIClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public GXRestAPIClient()
private string httpMethod = "GET";

private Dictionary<string, string> _queryVars = new Dictionary<string, string>();
private Dictionary<string, string> _headerVars = new Dictionary<string, string>();
private Dictionary<string, string> _bodyVars = new Dictionary<string, string>();
//private Dictionary<string, string> _pathVars = new Dictionary<string, string>();
private Dictionary<string,object> _responseData = new Dictionary<string, object>();
Expand All @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -325,6 +466,7 @@ public GxSimpleCollection<T> GetBodySimpleCollection<T>(string varName)
}
return collection;
}
#endregion

public void AddUploadFile(string FilePath, string name)
{
Expand All @@ -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)
{
Expand Down

0 comments on commit e24a865

Please sign in to comment.