/* * dependencies: * * Json.NET * - http://www.newtonsoft.com/json * - https://www.nuget.org/packages/Newtonsoft.Json/ */ using System; using System.Net; namespace SlideRoom.SDK { using Newtonsoft.Json; using SlideRoom.SDK.Model; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.Reflection; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Web; public class SlideRoomClientV2 { public ApplicantResource Applicant { get; private set; } public ApplicationResource Application { get; private set; } public ExportResource Export { get; private set; } private const string DEFAULT_API_URL = "https://api.slideroom.com"; private string _oauthToken; private string _apiUrl; #region methods public SlideRoomClientV2(string oauthToken, string apiUrl = DEFAULT_API_URL) { _oauthToken = oauthToken; _apiUrl = apiUrl; Applicant = new ApplicantResource(this); Application = new ApplicationResource(this); Export = new ExportResource(this); } private async Task ExecuteAsync(string method, string path, object queryParameters, object bodyData, CancellationToken cancellationToken) { var qp = GatherQueryParameters(queryParameters); var uri = GetUri(path, qp); var request = HttpWebRequest.Create(uri) as HttpWebRequest; request.Method = method; request.UserAgent = "SlideRoom Client V2 (.Net)"; request.Headers[HttpRequestHeader.Authorization] = "Bearer " + _oauthToken; request.ContentType = "application/json"; if (bodyData != null) { var json = JsonConvert.SerializeObject(bodyData); var body = Encoding.UTF8.GetBytes(json); request.ContentLength = body.Length; using (var requestStream = await request.GetRequestStreamAsync()) { await requestStream.WriteAsync(body, 0, body.Length); } } else { request.ContentLength = 0; } var response = (HttpWebResponse)null; try { response = await request.GetResponseAsync() as HttpWebResponse; return JsonConvert.DeserializeObject(GetResponseText(response)); } catch (WebException ex) { response = ex.Response as HttpWebResponse; var error = JsonConvert.DeserializeObject(GetResponseText(response)); error.StatusCode = response.StatusCode; error.StatusDescription = response.StatusDescription; throw new ApiErrorException(error); } } private string GetResponseText(WebResponse response) { using (var responseStream = response.GetResponseStream()) using (var responseReader = new StreamReader(responseStream)) { return responseReader.ReadToEnd(); } } private NameValueCollection GatherQueryParameters(object queryParameters) { var result = new NameValueCollection(); if (queryParameters != null) { var properties = queryParameters.GetType().GetProperties(BindingFlags.Default | BindingFlags.Public | BindingFlags.Instance); foreach (var prop in properties) { var value = prop.GetValue(queryParameters); if (value != null) { if (prop.PropertyType.IsValueType || prop.PropertyType == typeof(string)) { result[prop.Name] = prop.GetValue(queryParameters).ToString(); } else { var innerResult = GatherQueryParameters(prop.GetValue(queryParameters)); foreach (var key in innerResult.AllKeys) { result[prop.Name + "." + key] = innerResult[key]; } } } } } return result; } private Uri GetUri(string path, NameValueCollection queryParameters) { var uri = new UriBuilder(_apiUrl); uri.Path = path; if (queryParameters.Count > 0) { var parms = HttpUtility.ParseQueryString(uri.Query); foreach (var key in queryParameters.AllKeys) { parms[key] = queryParameters[key]; } uri.Query = parms.ToString(); } return uri.Uri; } #endregion #region resources public class ApplicantResource { private readonly SlideRoomClientV2 _client; internal ApplicantResource(SlideRoomClientV2 client) { _client = client; } /// /// Deletes a custom attribute for an applicant. /// public async Task DeleteAttributesV2Async(Model.Applicant.DeleteAttributesV2Parameters parms, CancellationToken cancellationToken) { var __url = "/api/v2/applicant/attributes"; return await _client.ExecuteAsync("DELETE", __url, parms, null, cancellationToken).ConfigureAwait(false); } /// /// Gets the custom applicant attributes used by the organization. /// public async Task> GetAttributeNamesV2Async(CancellationToken cancellationToken) { var __url = "/api/v2/applicant/attributes/names"; return await _client.ExecuteAsync>("GET", __url, null, null, cancellationToken).ConfigureAwait(false); } /// /// Gets the custom attributes for an applicant. /// public async Task> GetAttributesV2Async(Model.Applicant.GetAttributesV2Parameters parms, CancellationToken cancellationToken) { var __url = "/api/v2/applicant/attributes"; return await _client.ExecuteAsync>("GET", __url, parms, null, cancellationToken).ConfigureAwait(false); } /// /// Updates the custom attributes for an applicant. /// public async Task PostAttributesV2Async(Model.Applicant.PostAttributesV2Parameters parms, Dictionary data, CancellationToken cancellationToken) { var __url = "/api/v2/applicant/attributes"; return await _client.ExecuteAsync("POST", __url, parms, data, cancellationToken).ConfigureAwait(false); } } public class ApplicationResource { private readonly SlideRoomClientV2 _client; internal ApplicationResource(SlideRoomClientV2 client) { _client = client; } /// /// Deletes a custom attribute for an application. /// public async Task DeleteAttributesV2Async(string applicationId, Model.Application.DeleteAttributesV2Parameters parms, CancellationToken cancellationToken) { var __url = "/api/v2/application/{applicationId}/attributes".Replace("{applicationId}", applicationId.ToString()); return await _client.ExecuteAsync("DELETE", __url, parms, null, cancellationToken).ConfigureAwait(false); } /// /// Gets the custom application attributes used by the organization. /// public async Task> GetAttributeNamesV2Async(CancellationToken cancellationToken) { var __url = "/api/v2/application/attributes/names"; return await _client.ExecuteAsync>("GET", __url, null, null, cancellationToken).ConfigureAwait(false); } /// /// Gets the custom attributes for an application. /// public async Task> GetAttributesV2Async(string applicationId, CancellationToken cancellationToken) { var __url = "/api/v2/application/{applicationId}/attributes".Replace("{applicationId}", applicationId.ToString()); return await _client.ExecuteAsync>("GET", __url, null, null, cancellationToken).ConfigureAwait(false); } /// /// Updates the custom attributes for an application. API Import is available in the Advanced Plan. /// public async Task PostAttributesV2Async(string applicationId, Dictionary data, CancellationToken cancellationToken) { var __url = "/api/v2/application/{applicationId}/attributes".Replace("{applicationId}", applicationId.ToString()); return await _client.ExecuteAsync("POST", __url, null, data, cancellationToken).ConfigureAwait(false); } /// /// Requests the generation of a single application export file (tabular, pdf, zip). /// public async Task RequestExportByApplicationIdV2Async(string applicationId, Model.Application.RequestExportByApplicationIdV2Parameters parms, CancellationToken cancellationToken) { var __url = "/api/v2/application/{applicationId}/request-export".Replace("{applicationId}", applicationId.ToString()); return await _client.ExecuteAsync("POST", __url, parms, null, cancellationToken).ConfigureAwait(false); } /// /// Requests the generation of application export files (tabular, pdf, zip). /// public async Task RequestExportV2Async(Model.Application.RequestExportV2Parameters parms, CancellationToken cancellationToken) { var __url = "/api/v2/application/request-export"; return await _client.ExecuteAsync("POST", __url, parms, null, cancellationToken).ConfigureAwait(false); } } public class ExportResource { private readonly SlideRoomClientV2 _client; internal ExportResource(SlideRoomClientV2 client) { _client = client; } /// /// Gets the status/result of a requested export. /// public async Task GetV2Async(int token, CancellationToken cancellationToken) { var __url = "/api/v2/export/{token}".Replace("{token}", token.ToString()); return await _client.ExecuteAsync("GET", __url, null, null, cancellationToken).ConfigureAwait(false); } } #endregion resources } } namespace SlideRoom.SDK.Model { public class ApiError { public HttpStatusCode StatusCode { get; set; } public string StatusDescription { get; set; } public string Message { get; set; } } public class ApiErrorException : Exception { public ApiError Error { get; set; } public ApiErrorException(ApiError error) : base(GetErrorMessage(error)) { Error = error; } private static string GetErrorMessage(ApiError error) { if (!String.IsNullOrWhiteSpace(error.Message)) { return error.Message; } else { return "An unknown API error occurred."; } } } namespace Applicant { public class DeleteAttributesV2Parameters { public string email { get; set; } public string name { get; set; } public PoolEnum pool { get; set; } public int commonAppYear { get; set; } public enum PoolEnum { Standard, CommonAppSDS } } public class GetAttributesV2Parameters { public string email { get; set; } public PoolEnum pool { get; set; } public int commonAppYear { get; set; } public enum PoolEnum { Standard, CommonAppSDS } } public class PostAttributesV2Parameters { public string email { get; set; } public PoolEnum pool { get; set; } public int commonAppYear { get; set; } public enum PoolEnum { Standard, CommonAppSDS } } } namespace Application { public class DeleteAttributesV2Parameters { public string name { get; set; } } public class RequestExportByApplicationIdV2Parameters { public FormatEnum format { get; set; } public RoundTypeEnum roundType { get; set; } public string roundName { get; set; } public TabParameters tab { get; set; } public PdfParameters pdf { get; set; } public ZipParameters zip { get; set; } public DeliveryParameters delivery { get; set; } public enum FormatEnum { csv, tsv, txt, tab, xlsx, pdf, zip, json } public enum RoundTypeEnum { Assigned, Current, Named, All } public class TabParameters { public string export { get; set; } } public class PdfParameters { public bool includeForms { get; set; } public bool includeReferences { get; set; } public bool includeMedia { get; set; } public bool includeApplicantAttachments { get; set; } public bool includeOrganizationAttachments { get; set; } public bool includeRatings { get; set; } public bool includeFullPageMedia { get; set; } public bool includeHighlights { get; set; } public bool includeComments { get; set; } public bool includeCommonApp { get; set; } } public class ZipParameters { public bool originalMedia { get; set; } public bool includeForms { get; set; } public bool includeReferences { get; set; } public bool includeMedia { get; set; } public bool includeApplicantAttachments { get; set; } public bool includeOrganizationAttachments { get; set; } public bool includeRatings { get; set; } public bool includeComments { get; set; } public bool includeCommonApp { get; set; } } public class DeliveryParameters { public string account { get; set; } public string folder { get; set; } } } public class RequestExportV2Parameters { public FormatEnum format { get; set; } public RoundTypeEnum roundType { get; set; } public string roundName { get; set; } public int since { get; set; } public PoolEnum pool { get; set; } public StatusEnum status { get; set; } public string searchName { get; set; } public string email { get; set; } public TabParameters tab { get; set; } public PdfParameters pdf { get; set; } public ZipParameters zip { get; set; } public DeliveryParameters delivery { get; set; } public enum FormatEnum { csv, tsv, txt, tab, xlsx, pdf, zip, json } public enum RoundTypeEnum { Assigned, Current, Named, All } public enum PoolEnum { All, Current, Archived, CommonAppSDS } public enum StatusEnum { All, InProgress, Submitted } public class TabParameters { public string export { get; set; } } public class PdfParameters { public bool includeForms { get; set; } public bool includeReferences { get; set; } public bool includeMedia { get; set; } public bool includeApplicantAttachments { get; set; } public bool includeOrganizationAttachments { get; set; } public bool includeRatings { get; set; } public bool includeFullPageMedia { get; set; } public bool includeHighlights { get; set; } public bool includeComments { get; set; } public bool includeCommonApp { get; set; } } public class ZipParameters { public bool originalMedia { get; set; } public bool includeForms { get; set; } public bool includeReferences { get; set; } public bool includeMedia { get; set; } public bool includeApplicantAttachments { get; set; } public bool includeOrganizationAttachments { get; set; } public bool includeRatings { get; set; } public bool includeComments { get; set; } public bool includeCommonApp { get; set; } } public class DeliveryParameters { public string account { get; set; } public string folder { get; set; } } } } public class RequestApplicationExportResultV2 { public string message { get; set; } public int submissions { get; set; } public int token { get; set; } } public class ExportResultV2 { public string status { get; set; } public int total_files { get; set; } public int completed_files { get; set; } public string[] file_urls { get; set; } } }