Is there a ODS ORD API .Net c# example

Hello all, I’m trying the find an .Net example project with all possible scenarios for ODS ORD API, mainly

https://directory.spineservices.nhs.uk/ORD/2-0-0/organisations?PrimaryRoleId=RO177

Please point me in the right direction

Regards
Judson

Hi Judson
Have you seen the ODS ORD API synchroniser demonstrator as this may help?
Thanks
Laura

Hi Laura,
Thank you very much for your response. Yes, just found the demonstrator day before yesterday. I have begun to take a look. Thank you once again, very much appreciated.
Kind regards
Judson

UPDATE: I managed to get the sample code working by making a few changes to the ‘CAPIRequester.cs’ file.
The existing code seems to be using deprecated ‘HttpWebResponse’ and ‘HttpWebRequest’ methods. Please find the updated c# code below:

using System;
using System.IO;
using System.Net;
using System.Xml.Linq;
using System.Text.Json;
using System.Reflection.PortableExecutable;
namespace CORDAPIInterrogatorDLL
{
public class CAPIRequester
{
//private HttpWebResponse moResponse = null;
private HttpResponseMessage moResponse = null;
private String requestURL = null;

    public CAPIRequester(String requestURL)
    {
        this.requestURL = requestURL;
        makeRequest();
    }

    public string getRequestURL()
    {
        return requestURL;
    }

    public XDocument getResponseAsXMLDocument()
    {
        XDocument orgs = XDocument.Parse(getResponseAsString());
        return orgs;
    }

    public String getResponseAsString()
    {
        String xmlString = null;

        //Stream responseStream = moResponse.GetResponseStream();
        Stream responseStream = moResponse.Content.ReadAsStreamAsync().Result;
        if (responseStream != null)
        {
            StreamReader reader = new StreamReader(responseStream);
            xmlString = reader.ReadToEnd();
        }
        return xmlString;
    }

    public String getResponseHeaderValue(String property)
    {
        //return moResponse.Headers[property];
        return moResponse.Headers.FirstOrDefault(x => x.Key == property).Value.FirstOrDefault();
    }

    public void makeRequest()
    {
        try
        {
            using (var client = new HttpClient())
            {
                //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestURL);
                //request.Method = "GET";
                //request.ContentLength = 0;
                //request.ContentType = "application/xml";
                //request.Accept = "application/xml";
                //request.Proxy = new WebProxy();

                HttpResponseMessage request = client.GetAsync(requestURL).Result;
                moResponse = request;

                if (moResponse.StatusCode != HttpStatusCode.OK)
                {
                    throw new ApplicationException(String.Format("Request failed. Received HTTP {0}",
                        moResponse.StatusCode));
                }

                request = null;
            }
        }
        catch (WebException e)
        {
            WebResponse exceptionResponse = e.Response;
            Stream responseStream = exceptionResponse.GetResponseStream();

            if (responseStream != null)
            {
                var reader = new StreamReader(responseStream);
                String resp = reader.ReadToEnd();
                throw new ApiException(resp);
            }

            throw;
        }
    }
}

public class APIError
{
    public int errorCode { get; set; }
    public string errorText { get; set; }
}

public class ApiException : Exception
{
    public ApiException(string message)
        : base(Convert(message))
    { }

    private static string Convert(string message)
    {
        try
        {
            APIError o = JsonSerializer.Deserialize<APIError>(message);
            return $"Error raised by ODS API - errorCode: {o.errorCode} errorText: {o.errorText}";
        }
        catch (JsonException)
        {
            return $"Error raised by ODS API but the following Json could not parsed - {message}";
        }
    }
}

}

1 Like