Search code examples
c#jsonrestwebspherecognos-tm1

C# - JSON Body Parameter for OLAP TM1 - Patch Call via RestSharp


i'm coding a C# Framework to interact with IBM's PA Rest Api, i figured out the calls i need in postman and used the examples to build the calls into a framework for my future projects.

I'm relativ new to rest api's and json, so may its a really dump question here, but i dont know how i could build the Body Parameter at the best way. Let me show you:

    request.AddParameter("application/json", "{\r\n    \"Cells\": [\r\n        {\r\n            \"[email protected]\": [\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);

This Example shows how the body must look to put a Value into TM1, got this from Postman.

So the Dimension Count is dynamic also the names, i just have the idea to work with an array but thats not beautiful and i'm really sure someone has a nice and easy solution for it, that i just dont know.

Thanks for your help!


Solution

  • i solved it now like this, maybe someone is interested in it, its may not the best way but works.

        /// <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"":[
                                    { ""[email protected]"": [";
            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;
        }