This is an example of C# code suitable for pasting into a website in order to serve up recommendations directly from Raptor's proprietary algorithms. A number of parameters need to be filled in to make it usable, and determine which module you are implementing. These can be found in the Raptor Control Panel.
Within the code-block itself, you will find detailed information about where to locate the various information you require.
Non-blocking, async request to the Raptor API, with timeout and fallback included.
using System;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
namespace RaptorCaller
{
public class Program
{
public static void Main(string[] args)
{
CallRaptor().Wait();
}
public static async Task CallRaptor()
{
var customerId = "[YourCustomerIdHere]"; //A number string, which can be found in the Raptor controlpanel
var apiMethodName = "[Your apimethod name here]"; //For instance: "GetOverAllTopVisits"
var apiKey = "[Your api key here]"; //A guid, which can be found in the Raptor controlpanel
var numberOfRecommendations = 10; //Can be set to a number from 1-100
var timeout = TimeSpan.FromSeconds(1.5); //first call to c# httpclient can take 1+ seconds. next calls should be <400ms depending on network latency
try
{
var json = await RaptorCaller.GetJson(customerId, apiMethodName, apiKey, timeout,
numberOfRecommendations);
//Using Json.NET to parse the json result. Can be installed using nuget.
var jsonArray = JArray.Parse(json);
var productIds = jsonArray
.Select(item => item["ProductID"])
.ToList(); //NB: Refer to the raptor controlpanel, for exact parameter names
}
catch (OperationCanceledException cancelled)
{
//timeout
//Log
//Return fallback
}
catch (HttpRequestException exception)
{
//?? exception
//Log
//Return fallback
}
}
}
public static class RaptorCaller
{
private static readonly HttpClient HttpClient = new HttpClient();
/// <summary>
/// Returns JSON array from RaptorApi. Ie. [{"ProductId":"aaa"},{"ProductId":"bbb"},{"ProductId":"ccc"}]
/// </summary>
/// <param name="customerId">Found in controlpanel</param>
/// <param name="apiMethodName">Found in controlpanel. Ie.GetOverAllTopVisits </param>
/// <param name="apiKey">Found in controlpanel. Ie 786A5152-9B08-4577-BBBA-5EC8F65AB3EF</param>
/// <param name="timeout"></param>
/// <param name="numberOfRecommendations">expected number to receive. default= 10</param>
/// <returns>JArray</returns>
/// <exception cref="OperationCanceledException"></exception>
/// <exception cref="HttpRequestException"></exception>
public static async Task<string> GetJson(string customerId, string apiMethodName, string apiKey, TimeSpan timeout, int numberOfRecommendations = 10)
{
var url = new Uri($"https://api.raptorsmartadvisor.com/v1/{customerId}/{apiMethodName}/{numberOfRecommendations}/{apiKey}?");
HttpClient.DefaultRequestHeaders.Accept.Clear();
HttpClient.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("appplication/json"));
var cancellationTokenSource = new CancellationTokenSource(timeout);
var responseMessage = await HttpClient.GetAsync(url, cancellationTokenSource.Token);
responseMessage.EnsureSuccessStatusCode();
return await responseMessage.Content.ReadAsStringAsync();
}
}
}
Download from Bitbucket: https://bitbucket.org/raptorservices/csharp
⚠️THIS SAMPLE CODE IS PROVIDED “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RAPTOR SERVICES OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) SUSTAINED BY YOU OR A THIRD PARTY, HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ARISING IN ANY WAY OUT OF THE USE OF THIS SAMPLE CODE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.