Convert Coveo Rest API JSON result into object using custom Deserialize

Convert Coveo Rest API JSON result into object using custom Deserialize

When we use the Coveo Rest API by passing parameters to get the results in JSON, it provides a lots of things in the results, and creating class and properties for all these result is a tedious work. And also in our case we don’t require all the result properties and creating multiple classes for the different value will make the thing complex. So we have decided to use Json to object converter, but problem arrives when we don’t know the properties returns in the result because result can return any type of sitecore page item. One solution we have use to create a Dynamic Json Converter. So how it is going to work? We got the result by making call to the coveo cloud and passing query and then we need to deserialize using the custom deserialize.

            var result = MakeCoveoRequest.MakeRequest(q);

var serializer = new JavaScriptSerializer();

serializer.RegisterConverters(new[] { new DynamicJsonConverter() });

dynamic obj = serializer.Deserialize(result, typeof(object));

we need to create as instance of the JavaScriptSerializer and then we need to registor the convertor with our custom dynamic Json convertor. Now the object of the JavaScriptSerializer will deserialize the result in dynamic object.

Our Custom DynamicJsonConverter will override default Deserialize function to convert the json into dictionary key value pair where key will be dynamic object property and value will be value or any dynamic object.

using System;

using System.Collections;

using System.Collections.Generic;

using System.Collections.ObjectModel;

using System.Dynamic;

using System.Linq;

using System.Text;

using System.Web.Script.Serialization;

 

namespace MyNameSpace

{

public class DynamicJsonConverter : JavaScriptConverter

{

public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)

{

if (dictionary == null)

throw new ArgumentNullException(“dictionary”);

 

return type == typeof(object) ? new DynamicJsonObject(dictionary) : null;

}

 

public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)

{

throw new NotImplementedException();

}

 

public override IEnumerable<Type> SupportedTypes

{

get { return new ReadOnlyCollection<Type>(new List<Type>(new[] { typeof(object) })); }

}

 

#region Nested type: DynamicJsonObject

 

private sealed class DynamicJsonObject : DynamicObject

{

private readonly IDictionary<string, object> _dictionary;

 

public DynamicJsonObject(IDictionary<string, object> dictionary)

{

if (dictionary == null)

throw new ArgumentNullException(“dictionary”);

_dictionary = dictionary;

}

 

public override string ToString()

{

var sb = new StringBuilder(“{“);

ToString(sb);

return sb.ToString();

}

 

private void ToString(StringBuilder sb)

{

var firstInDictionary = true;

foreach (var pair in _dictionary)

{

if (!firstInDictionary)

sb.Append(“,”);

firstInDictionary = false;

var value = pair.Value;

var name = pair.Key;

if (value is string)

{

sb.AppendFormat(“{0}:\”{1}\””, name, value);

}

else if (value is IDictionary<string, object>)

{

new DynamicJsonObject((IDictionary<string, object>)value).ToString(sb);

}

else if (value is ArrayList)

{

sb.Append(name + “:[“);

var firstInArray = true;

foreach (var arrayValue in (ArrayList)value)

{

if (!firstInArray)

sb.Append(“,”);

firstInArray = false;

if (arrayValue is IDictionary<string, object>)

new DynamicJsonObject((IDictionary<string, object>)arrayValue).ToString(sb);

else if (arrayValue is string)

sb.AppendFormat(“\”{0}\””, arrayValue);

else

sb.AppendFormat(“{0}”, arrayValue);

 

}

sb.Append(“]”);

}

else

{

sb.AppendFormat(“{0}:{1}”, name, value);

}

}

sb.Append(“}”);

}

 

public override bool TryGetMember(GetMemberBinder binder, out object result)

{

if (!_dictionary.TryGetValue(binder.Name, out result))

{

// return null to avoid exception.  caller can check for null this way…

result = null;

return true;

}

 

result = WrapResultObject(result);

return true;

}

 

public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)

{

if (indexes.Length == 1 && indexes[0] != null)

{

if (!_dictionary.TryGetValue(indexes[0].ToString(), out result))

{

// return null to avoid exception.  caller can check for null this way…

result = null;

return true;

}

 

result = WrapResultObject(result);

return true;

}

 

return base.TryGetIndex(binder, indexes, out result);

}

 

private static object WrapResultObject(object result)

{

var dictionary = result as IDictionary<string, object>;

if (dictionary != null)

return new DynamicJsonObject(dictionary);

 

var arrayList = result as ArrayList;

if (arrayList != null && arrayList.Count > 0)

{

return arrayList[0] is IDictionary<string, object>

? new List<object>(arrayList.Cast<IDictionary<string, object>>().Select(x => new DynamicJsonObject(x)))

: new List<object>(arrayList.Cast<object>());

}

 

return result;

}

}

}

#endregion

}

Advertisements
Coveo Get Authentication key

Coveo Get Authentication key

In our pervious blog we were passing the authorization key in the header of the POST method for the authorization. This blog is a help guide to generate the API key from the Coveo Cloud Plateform. Here are the steps –

1. login into the platform.cloud.coveo.com/admin- It will show you the following UI, where in left side you will have search, analytics, and organization menus

coveologin

 

 

2  – Click on the API key menu under in the organization menu (it could be second last link). It will open a page where you can see existing API key’s Name, description, status, creation date and author of the key but you can’t see the actual key in this list.

apikey2

 

3 – Now click on the add key button on top right section of the api key page, it will open a pop up which will ask you about the configuration of the key such as Name, description, allowed and denied IPs if any.

APIKEY_3

 

You can also add the view, edit, enable privileges for the different search, analytics and organization services

APIKEY_priv.png

in the privilges please make sure to enable execute queries in search configuration seaction.

 

4 – After adding the configuration information click on the Add button at bottom right section of the page. It will open a dialog containing the API key

APIKEY_4

 

5 – Please make sure to copy the API key before closing the dialog, you will not able to see it again. this API key is your authorization key for the REST API authorization.

 

Coveo REST Search API Using Post Method

Coveo REST Search API Using Post Method

Coveo Rest API is a simple REST interface for invoking, exposing and manipulating the search based on the different filtering and search criteria.

Coveo provide both cloud and on-permises indexing Where Coveo cluod customer can accessed the REST API throught Coveo Cloud Plateform. But for on-permise indexing, we need to deploy the Search API locally. We are using the Coveo Cloud Platform and this blog is based on it. And when we are taking about the Coveo Cloud Platform then REST API is available to all users through a single URL – https://cloudplatform.coveo.com/rest/search/v2

Coveo provide many means of client authentication, but we will use the Authorization Key for the authenticating the client. We can perform both GET and POST method for the search. We can pass multiple supporting parameter either in the query string or in the body of the POST method as JSON format.

A typical POST method example in post man for the coveo cloud plateform is as follow –

postman

Where POST URL is https://cloudplatform.coveo.com/rest/search/v2, and authorization is done using Authentication key. Different parameters for the search such as organizationid, query, advance query etc. are passed in the body as content type – application/json.

A C# static class for making query to the coveo cloud plate form is as follow –

using System;

using System.IO;

using System.Net;

using System.Text;

using Sitecore.Diagnostics;

using MyStringExtentions

 

namespace MyNameSpace

{

Public static class MakeCoveoRequest

{

public static string MakeRequest(string query)

{

var result = string.Empty;

var CoveoRestUrl = “https://cloudplatform.coveo.com/rest/search/v2”;

 

var coveoUri = new Uri(CoveoRestUrl);

 

var httpWebRequest = WebRequest.Create(coveoUri) as HttpWebRequest;

if (httpWebRequest == null) return result;

 

try

{

var postData = Encoding.UTF8.GetBytes(query);

httpWebRequest.Method = “POST”;

httpWebRequest.PreAuthenticate = true;

httpWebRequest.Headers.Add(“Authorization”, “Bearer MyAuthorizationKey”);

httpWebRequest.ContentType = “application/json”;

httpWebRequest.ContentLength = postData.Length;

using (var dataStream = httpWebRequest.GetRequestStream())

{

dataStream.Write(postData, 0, postData.Length);

}

var myWebResponse = httpWebRequest.GetResponse();

var responseStream = myWebResponse.GetResponseStream();

if (responseStream == null) return null;

var myStreamReader = new StreamReader(responseStream, Encoding.Default);

result = myStreamReader.ReadToEnd();

responseStream.Close();

myWebResponse.Close();

}

catch (WebException ex)

{

Log.Error(“Message” + ex.Message, ex);

}

 

return result;

}

}

}

 

Thanks