C# - JSON Body OLAP TM1 的参数 - 通过 RestSharp 调用补丁
C# - JSON Body Parameter for OLAP TM1 - Patch Call via RestSharp
我正在编写一个 C# 框架来与 IBM 的 PA Rest 进行交互 Api,我想出了邮递员中需要的调用,并使用这些示例将调用构建到我未来项目的框架中。
我是 rest api 和 json 的相对新手,所以这里可能是一个真正的转储问题,但我不知道如何构建 Body 参数以最好的方式。让我告诉你:
request.AddParameter("application/json", "{\r\n \"Cells\": [\r\n {\r\n \"Tuple@odata.bind\": [\r\n \"Dimensions('Version')/Hierarchies('Version')/Elements('Actual')\",\r\n \"Dimensions('Year')/Hierarchies('Year')/Elements('2018')\",\r\n \"Dimensions('Period')/Hierarchies('Period')/Elements('Jan')\",\r\n \"Dimensions('Currency')/Hierarchies('Currency')/Elements('Local')\",\r\n \"Dimensions('Region')/Hierarchies('Region')/Elements('England')\",\r\n \"Dimensions('Department')/Hierarchies('Department')/Elements('Executive General and Administration')\",\r\n \"Dimensions('Account')/Hierarchies('Account')/Elements('Meals')\",\r\n \"Dimensions('General Ledger Measure')/Hierarchies('General Ledger Measure')/Elements('Amount')\"\r\n ]\r\n }\r\n ],\r\n \"Value\": \"1234\"\r\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
此示例展示了 body 必须如何将值放入 TM1,从 Postman 那里获得。
所以维数也是动态的,名称也是动态的,我只是想使用数组,但这并不漂亮,我真的确定有人有一个很好的简单的解决方案,我只是不知道.
感谢您的帮助!
我现在是这样解决的,也许有人对此感兴趣,这可能不是最好的方法,但有效。
/// <summary>
/// Update string or numeric cells in TM1
/// </summary>
/// <param name="Value">The Value you want to insert, for numeric values u have to convert it to a string before calling the methode</param>
/// <param name="Cube">Target cube</param>
/// <param name="Dimensions">2 Dimensional Array, the 2nd dimension contains the TM1 dim infos, 0 = DimName, 1=Hierarchy name (can be empty), 2 = Element Name</param>
/// <returns></returns>
public string CellPutValue(string Value, string Cube, string[,] Dimensions )
{
//create body header
string body = @"{
""Cells"":[
{ ""Tuple@odata.bind"": [";
for (int i = 0; i < Dimensions.GetLength(0); i++)
{
//check if array data is correct...
if (Dimensions[i, 0] == "")
break;
//for cleanness, used temp vars for reading out the array and build the body
string dim = Dimensions[i, 0];
string hierarchy = Dimensions[i, 1] == null ? Dimensions[i, 0] : Dimensions[i, 1];
string dimEl = Dimensions[i, 2];
//loop through the array and construct the body json
if (i < Dimensions.GetLength(0)-1)
{
body = body + @"
""Dimensions('" + dim + @"')/Hierarchies('" + hierarchy + @"')/Elements('" + dimEl + @"')"",";
}
else
{
body = body + @"
""Dimensions('" + dim + @"')/Hierarchies('" + hierarchy + @"')/Elements('" + dimEl + @"')""";
}
}
//add body footer
body = body + @"
]
}
],
""Value"":""" + Value + @"""
}";
var request = new RestRequest("Cubes('" + Cube + "')/tm1.Update",Method.POST);
request.AddCookie(sessionCookie.Name, sessionCookie.Value);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", body, ParameterType.RequestBody);
IRestResponse response = restClient.Execute(request);
//return response
return response.Content;
}
我正在编写一个 C# 框架来与 IBM 的 PA Rest 进行交互 Api,我想出了邮递员中需要的调用,并使用这些示例将调用构建到我未来项目的框架中。
我是 rest api 和 json 的相对新手,所以这里可能是一个真正的转储问题,但我不知道如何构建 Body 参数以最好的方式。让我告诉你:
request.AddParameter("application/json", "{\r\n \"Cells\": [\r\n {\r\n \"Tuple@odata.bind\": [\r\n \"Dimensions('Version')/Hierarchies('Version')/Elements('Actual')\",\r\n \"Dimensions('Year')/Hierarchies('Year')/Elements('2018')\",\r\n \"Dimensions('Period')/Hierarchies('Period')/Elements('Jan')\",\r\n \"Dimensions('Currency')/Hierarchies('Currency')/Elements('Local')\",\r\n \"Dimensions('Region')/Hierarchies('Region')/Elements('England')\",\r\n \"Dimensions('Department')/Hierarchies('Department')/Elements('Executive General and Administration')\",\r\n \"Dimensions('Account')/Hierarchies('Account')/Elements('Meals')\",\r\n \"Dimensions('General Ledger Measure')/Hierarchies('General Ledger Measure')/Elements('Amount')\"\r\n ]\r\n }\r\n ],\r\n \"Value\": \"1234\"\r\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
此示例展示了 body 必须如何将值放入 TM1,从 Postman 那里获得。
所以维数也是动态的,名称也是动态的,我只是想使用数组,但这并不漂亮,我真的确定有人有一个很好的简单的解决方案,我只是不知道.
感谢您的帮助!
我现在是这样解决的,也许有人对此感兴趣,这可能不是最好的方法,但有效。
/// <summary>
/// Update string or numeric cells in TM1
/// </summary>
/// <param name="Value">The Value you want to insert, for numeric values u have to convert it to a string before calling the methode</param>
/// <param name="Cube">Target cube</param>
/// <param name="Dimensions">2 Dimensional Array, the 2nd dimension contains the TM1 dim infos, 0 = DimName, 1=Hierarchy name (can be empty), 2 = Element Name</param>
/// <returns></returns>
public string CellPutValue(string Value, string Cube, string[,] Dimensions )
{
//create body header
string body = @"{
""Cells"":[
{ ""Tuple@odata.bind"": [";
for (int i = 0; i < Dimensions.GetLength(0); i++)
{
//check if array data is correct...
if (Dimensions[i, 0] == "")
break;
//for cleanness, used temp vars for reading out the array and build the body
string dim = Dimensions[i, 0];
string hierarchy = Dimensions[i, 1] == null ? Dimensions[i, 0] : Dimensions[i, 1];
string dimEl = Dimensions[i, 2];
//loop through the array and construct the body json
if (i < Dimensions.GetLength(0)-1)
{
body = body + @"
""Dimensions('" + dim + @"')/Hierarchies('" + hierarchy + @"')/Elements('" + dimEl + @"')"",";
}
else
{
body = body + @"
""Dimensions('" + dim + @"')/Hierarchies('" + hierarchy + @"')/Elements('" + dimEl + @"')""";
}
}
//add body footer
body = body + @"
]
}
],
""Value"":""" + Value + @"""
}";
var request = new RestRequest("Cubes('" + Cube + "')/tm1.Update",Method.POST);
request.AddCookie(sessionCookie.Name, sessionCookie.Value);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", body, ParameterType.RequestBody);
IRestResponse response = restClient.Execute(request);
//return response
return response.Content;
}