I am writing a program to integrate data between our Student Record System and the Finance System. The SRS is a cloud based solution while the Finance system is on-prem. Data is to be extracted from the SRS using its RESTful API. I then plan to write the returned resources to a staging database for integration with the finance system. After a bit of research I decided to use RestSharp as a foundation for my solution.
Following the Recommended Usage guide I have been able to get a solution that is successfully making requests and deserializing the response data into my classes. However I’ve hit a small speed bump and need a bit of help to get going again.
The SRS Rest API contains the resource PostalAddressOccupancy. According to the SRS API documentation it has following structure:
My problem is with the occupant property. This, as indicated by the mixed type, can be any one of a number other resource types (ie Student, Staff, Guardian etc).
I can easily represent this in my data model using generics. Eg
public class PostalAddressOccupancy<T>
{
public DateTime EffectiveDate { get; set; }
public DateTime EndDate { get; set; }
public string EntityType { get; set; }
public string Href { get; set; }
public string Id { get; set; }
public bool IsCorrespondenceAddress { get; set; }
public T Occupant { get; set; }
public PostalAddress PostalAddress { get; set; }
public string PostalAddressType { get; set; }
public static PostalAddressOccupancy<T> GetPostalAddressOccupancy(string id)
{
var request = new RestRequest
{
Resource = "PostalAddressOccupancy/{Id}",
RootElement = "postalAddressOccupancy"
};
request.AddParameter("Id", id, ParameterType.UrlSegment);
RestApiClient restApiClient = new RestApiClient("SRS API");
return restApiClient.Execute<PostalAddressOccupancy<T>>(request);
}
public static List<PostalAddressOccupancy<T>> GetPostalAddressOccupancies()
{
List<PostalAddressOccupancy<T>> list = new List<PostalAddressOccupancy<T>>();
var request = new RestRequest
{
Resource = "PostalAddressOccupancy",
RootElement = "postalAddressOccupancies"
};
RestApiClient restApiClient = new RestApiClient("SRS API");
foreach (var pao in restApiClient.Execute<List<PostalAddressOccupancy<T>>>(request))
{
list.Add(GetPostalAddressOccupancy(pao.Href.Split('/').Last()));
}
return list;
}
}
My problem is how to get RestSharp to correctly understand which type of occupant is being returned in the response data. Using the GetPostalAddressOccupancies method above I get a list of objects with everything correctly deserialized apart from the occupant. This is returned as a base object type with its properties contained as key/value pairs.
Do I need to decorate my class or generic property with some tags to help provide a hint to the RestSharp deserializers?
I've also noticed that resources include base properties (eg entityType). Thus an occupant type can be identified through this, but I am unsure how that may help.