/// <summary> /// Gets the resource details for a given url /// </summary> /// <param name="projectId">ProjectId</param> /// <param name="path">Only path of the url is being sent not the entire url </param> /// <param name="kitsuneRequestUrlType">To Identify DEMO | PREVIEW | PRODUCTION</param> /// <returns> /// Return null if file not found /// Throws error if Failed fetching route /// </returns> public static void CreateRouteTree(string projectId, string path, KitsuneRequestUrlType kitsuneRequestUrlType = KitsuneRequestUrlType.PRODUCTION) { if (String.IsNullOrEmpty(projectId)) { throw new ArgumentNullException(nameof(projectId)); } if (String.IsNullOrEmpty(path)) { throw new ArgumentNullException(nameof(path)); } try { var requestType = kitsuneRequestUrlType.Equals(KitsuneRequestUrlType.PRODUCTION) ? 1 : 0; var temp = "{\"ProjectId\":\"" + projectId + "\"}"; var routingRequestParams = new String[] { requestType.ToString(), projectId, "new_KitsuneResourcesProduction", temp }; var response = AWSLambdaHelpers.InvokeAWSLambda(Constants.RoutingCreateFunctionName, string.Join("\n", routingRequestParams), RegionEndpoint.GetBySystemName(IdentifierEnvironmentConstants.IdentifierConfigurations.RoutingLambdaCredentials.Region)).Result; var responseObject = JsonConvert.DeserializeObject <dynamic>(response); if (responseObject["body"] != null) { var responseData = ((string)responseObject["body"]).Split('\n'); if (responseData.Length <= 0 || responseData[0] != "200") { throw new Exception(message: $"ProjectId : {projectId} and SourcePath : {path}, Error : Unable to create routing tree with message {(responseData.Length > 1 ? responseData[1] : "")}"); } } else { throw new Exception($"ProjectId : {projectId} and SourcePath : {path}, Error : Unable to create routing tree"); } } catch (WebException webException) { HttpWebResponse response = (HttpWebResponse)webException.Response; switch (response.StatusCode) { case HttpStatusCode.NotFound: break; } throw new Exception($"ProjectId : {projectId} and SourcePath : {path}, Error : Error from server with ResponseCode : {response.StatusCode}"); } catch (Exception ex) { throw ex; } }
/// <summary> /// Gets the resource details for a given url /// </summary> /// <param name="projectId">ProjectId</param> /// <param name="path">Only path of the url is being sent not the entire url </param> /// <param name="kitsuneRequestUrlType">To Identify DEMO | PREVIEW | PRODUCTION</param> /// <returns> /// Return null if file not found /// Throws error if Failed fetching route /// </returns> public static RoutingObjectModel GetRoutingObject(string projectId, string path, KitsuneRequestUrlType kitsuneRequestUrlType = KitsuneRequestUrlType.PRODUCTION, bool createIfNotExists = false) { if (String.IsNullOrEmpty(projectId)) { throw new ArgumentNullException(nameof(projectId)); } if (String.IsNullOrEmpty(path)) { throw new ArgumentNullException(nameof(path)); } try { var requestType = kitsuneRequestUrlType.Equals(KitsuneRequestUrlType.PRODUCTION) ? 1 : 0; var routingRequestParams = new String[] { requestType.ToString(), projectId, path }; var response = AWSLambdaHelpers.InvokeAWSLambda(Constants.RoutingMatcherFunctionName, string.Join("\n", routingRequestParams), RegionEndpoint.GetBySystemName(IdentifierEnvironmentConstants.IdentifierConfigurations.RoutingLambdaCredentials.Region)).Result; var responseObject = JsonConvert.DeserializeObject <dynamic>(response); if (responseObject["body"] != null) { var responseData = ((string)responseObject["body"]).Split('\n'); var resp = new RoutingObjectModel() { File = responseData[2], ResourceId = responseData[1], RedirectPath = responseData[3], StatusCode = int.Parse(responseData[0]) }; if (resp.StatusCode == 500 && createIfNotExists == true) //500 means routing tree not found. { CreateRouteTree(projectId, path, kitsuneRequestUrlType); return(GetRoutingObject(projectId, path, kitsuneRequestUrlType, false)); } return(resp); } throw new Exception($"ProjectId : {projectId} and SourcePath : {path}, Error : Unable to Retrieve the Message"); } catch (Exception ex) { throw new Exception($"ProjectId : {projectId} and SourcePath : {path}, Error : Error from server with message : {ex.Message}"); } }