Create, Read, Update, Submit of Service Requests Using API

using System; using MTOA.DomainObjects; using System.Linq; using System.Threading.Tasks; using System.Web.Mvc; using DTO; using MTOA.DomainObjects.DTO; using RestSharp; using Newtonsoft.Json; using Web.Portal.Helpers; namespace Web.Portal.Controllers { public class APIExampleController : Controller { private const int MyServiceId = 54; private const int UserId = 3755; private const string MyDocumentVersion = "MYSERV_V1.0"; private const string ApiUri = "https://wwwappstest.***/Saf-Sec-Sur/13/MTAPI-INT"; private const string ApiKey = "--- YOUR API KEY ---"; private const string JwtKey = "--- YOUR JWT ---"; private RestClient _restClient; private RestRequest _request; private int _serviceRequestId; public async Task<ActionResult> ServiceRequest() { #region "Check Enrollment" CreateRestClient("/api/v1/account/enrollments", Method.GET); _request.AddParameter("userId", UserId); _request.AddParameter("serviceId", MyServiceId); var response = await _restClient.ExecuteAsync(_request); var content = JsonConvert.DeserializeObject<System.Collections.Generic.ICollection<EnrollmentInfo>>(response.Content); // Verify that the current user is enrolled in your service. if (content.All(x => x.ServiceId != MyServiceId)) { Response.Write($@"User {UserId} is not enrolled in service id {MyServiceId}."); } #endregion #region "Create New Service Request" // Create a new service request. CreateRestClient("/api/v1/servicerequests", Method.POST); _request.AddParameter("userId", UserId, ParameterType.QueryString); _request.AddParameter("serviceId", MyServiceId, ParameterType.QueryString); _request.AddParameter("englishName", "a_displayNameEn", ParameterType.QueryString); _request.AddParameter("frenchName", "a_displayNameFr", ParameterType.QueryString); var restResponse = await _restClient.ExecuteAsync<ServiceRequestCreationResult>(_request); _serviceRequestId = restResponse.Data.ServiceRequestId; #endregion #region "Add Artifact" // Create and add your service request document (artifact). var document = new MyServiceRequestData { DisplayNameEn = "b_displayNameEn", DisplayNameFr = "b_displayNameFr" }; var json = JsonConvert.SerializeObject(document); CreateRestClient("/api/v1/artifacts", Method.POST); _request.AddParameter("serviceRequestId", _serviceRequestId, ParameterType.QueryString); _request.AddParameter("artifactType", ArtifactType.JsonMetadata, ParameterType.QueryString); _request.AddParameter("version", MyDocumentVersion, ParameterType.QueryString); _request.AddParameter("userId", UserId, ParameterType.QueryString); _request.AddParameter("artifact", json, ParameterType.RequestBody); await _restClient.ExecuteAsync(_request); #endregion #region "Get Service Request Document" // Retrieve the service request document. CreateRestClient("/api/v1/artifacts", Method.GET); _request.AddParameter("serviceRequestId", _serviceRequestId); _request.AddParameter("artifactType", ArtifactType.JsonMetadata); await _restClient.ExecuteAsync(_request); #endregion #region "Update Service Request" // Update the service request document with some new data. document.DisplayNameEn = "en"; document.DisplayNameFr = "fr"; json = JsonConvert.SerializeObject(document); CreateRestClient("/api/v1/artifacts", Method.PUT); _request.AddParameter("serviceRequestId", _serviceRequestId, ParameterType.QueryString); _request.AddParameter("artifactType", ArtifactType.JsonMetadata, ParameterType.QueryString); _request.AddParameter("version", "2.0", ParameterType.QueryString); _request.AddParameter("userId", UserId, ParameterType.QueryString); _request.AddParameter("artifact", json, ParameterType.RequestBody); await _restClient.ExecuteAsync(_request); #endregion #region "Update Status" CreateRestClient("/api/v1/servicerequests/{id}/updatestatus", Method.POST); _request.AddUrlSegment("id", _serviceRequestId); _request.AddParameter("userId", UserId, ParameterType.QueryString); _request.AddParameter("serviceRequestStatus", ServiceRequestStatus.Submitted.ToString(), ParameterType.QueryString); await _restClient.ExecuteAsync(_request); #endregion #region "Add Signed Document" var signed = JsonConvert.SerializeObject(GetSignatureInformation()); // Attach an eSignature to the service request. CreateRestClient("/api/v1/artifacts", Method.POST); _request.AddParameter("serviceRequestId", _serviceRequestId, ParameterType.QueryString); _request.AddParameter("artifactType", ArtifactType.SignedJsonDocument, ParameterType.QueryString); _request.AddParameter("version", MyDocumentVersion, ParameterType.QueryString); _request.AddParameter("userId", UserId, ParameterType.QueryString); _request.AddParameter("artifact", signed, ParameterType.RequestBody); await _restClient.ExecuteAsync(_request); #endregion Response.Write($@"Service request id: {restResponse.Data.ServiceRequestId} was successfully submitted."); return null; } private ESignature GetSignatureInformation() { return new ESignature { UserId = UserId.ToString(), FirstName = "Jane", LastName = "Smith", SignatureDateTime = DateTime.Now, IpAddress = WebRequestExtension.GetUserIPAddress(), HostName = WebRequestExtension.GetHostName(), BrowserDetails = WebRequestExtension.GetBrowserDetails() }; } private void CreateRestClient(string resource, Method method) { _restClient = new RestClient(ApiUri); _request = new RestRequest { UseDefaultCredentials = true, Resource = resource, Method = method }; _request.AddHeader("api-key", ApiKey); _request.AddHeader("app-jwt", JwtKey); } } }