private static Dictionary <string, object> getCron(string prefix) { string url = BaseUrl + "/getCron"; XMLHttpRequest request = new XMLHttpRequest(); request.open("POST", url, false); Dictionary <string, object> data = new Dictionary <string, object>(); data.Add("namePrefix", prefix); request.setRequestHeader("content-type", "application/json"); System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer(); request.send(jss.Serialize(data)); if (request.status != 200) { throw new Durados.DuradosException("Server return status " + request.status + ", " + request.responseText); } Dictionary <string, object> response = null; try { response = jss.Deserialize <Dictionary <string, object> >(request.responseText); } catch (Exception exception) { throw new Durados.DuradosException("Could not parse getCron response", exception); } return(response); }
public virtual void Delete(string folder, string functionName, string arn = ARN) { string url = BaseUrl + "/deleteLambda"; XMLHttpRequest request = new XMLHttpRequest(); request.open("POST", url, false); Dictionary <string, object> data = new Dictionary <string, object>(); data.Add("folder", folder); data.Add("functionName", functionName); data.Add("Role", arn); request.setRequestHeader("content-type", "application/json"); System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer(); request.send(jss.Serialize(data)); if (request.status != 200) { throw new Durados.DuradosException("Server return status " + request.status + ", " + request.responseText); } Dictionary <string, object> response = null; try { response = jss.Deserialize <Dictionary <string, object> >(request.responseText); } catch (Exception exception) { throw new Durados.DuradosException("Could not parse upload response", exception); } }
public virtual object Download(Durados.Security.Cloud.ICloudCredentials cloudCredentials, string lambdaFunctionName) { string url = BaseUrl + "/downloadLambda"; XMLHttpRequest request = new XMLHttpRequest(); request.open("POST", url, false); Dictionary <string, object> data = new Dictionary <string, object>(); data.Add("credentials", cloudCredentials.GetCredentials()); data.Add("cloudProvider", cloudCredentials.GetProvider()); data.Add("functionName", lambdaFunctionName); request.setRequestHeader("content-type", "application/json"); System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer(); request.send(jss.Serialize(data)); if (request.status != 200) { throw new NodeJsException(request.responseText.TrimStart("{}; ".ToCharArray())); } object response = null; try { response = jss.Deserialize <object>(request.responseText); } catch (Exception exception) { throw new Durados.DuradosException("Could not parse NodeJS response", exception); } return(response); }
private async Task <string> GetAsync(string url) { var xmlHttp = new XMLHttpRequest(); xmlHttp.open("GET", url, true); xmlHttp.setRequestHeader("Access-Control-Allow-Origin", "*"); var tcs = new TaskCompletionSource <string>(); xmlHttp.onreadystatechange = (e) => { if (xmlHttp.readyState == 0) { tcs.SetException(new Exception("Request aborted")); } else if (xmlHttp.readyState == 4) { if (xmlHttp.status == 200 || xmlHttp.status == 201 || xmlHttp.status == 304) { tcs.SetResult(xmlHttp.responseText); } else { tcs.SetException(new Exception("Request failed")); } } }; xmlHttp.send(); var tcsTask = tcs.Task; while (true) { await Task.WhenAny(tcsTask, Task.Delay(150)); if (tcsTask.IsCompleted) { if (tcsTask.IsFaulted) { throw tcsTask.Exception; } else { return(tcsTask.Result); } } } }
public IHttpActionResult smartListFolder(string path = null) { try { if (path == null) { path = string.Empty; } string url = GetNodeUrl() + "/smartListFolder"; XMLHttpRequest request = new XMLHttpRequest(); request.open("POST", url, false); Dictionary <string, object> data = new Dictionary <string, object>(); string appName = Map.AppName; data.Add("bucket", Maps.S3FilesBucket); data.Add("folder", appName); data.Add("pathInFolder", path); request.setRequestHeader("content-type", "application/json"); System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer(); request.send(jss.Serialize(data)); if (request.status != 200) { Maps.Instance.DuradosMap.Logger.Log("files", "folder", request.responseText, null, 1, "status: " + request.status); return(ResponseMessage(Request.CreateResponse(HttpStatusCode.InternalServerError, request.responseText))); } Dictionary <string, object>[] response = null; try { response = jss.Deserialize <Dictionary <string, object>[]>(request.responseText); } catch (Exception exception) { Maps.Instance.DuradosMap.Logger.Log("files", "folder", exception.Source, exception, 1, request.responseText); return(ResponseMessage(Request.CreateResponse(HttpStatusCode.InternalServerError, "Could not parse upload response: " + request.responseText + ". With the following error: " + exception.Message))); } return(Ok(response)); } catch (Exception exception) { throw new BackAndApiUnexpectedResponseException(exception, this); } }
private static void putCron(string name, string schedule, Dictionary <string, object> input, bool?active, string description) { string url = BaseUrl + "/putCron"; XMLHttpRequest request = new XMLHttpRequest(); request.open("POST", url, false); Dictionary <string, object> data = new Dictionary <string, object>(); data.Add("name", name); data.Add("schedule", HandleSchedule(schedule)); data.Add("lambdaArn", LambdaArn); data.Add("input", input); if (active.HasValue) { data.Add("active", active.Value); } else { data.Add("active", null); } data.Add("description", description); request.setRequestHeader("content-type", "application/json"); System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer(); request.send(jss.Serialize(data)); if (request.status != 200) { string message = request.responseText; if (request.responseText.Contains("Parameter ScheduleExpression is not valid")) { message = "The schedule expression '" + schedule + "' is not valid"; } throw new Durados.DuradosException(message); } Dictionary <string, object> response = null; try { response = jss.Deserialize <Dictionary <string, object> >(request.responseText); } catch (Exception exception) { throw new Durados.DuradosException("Could not parse putCron response", exception); } }
/// <summary> /// /// </summary> /// <param name="bucket">bucket is S3</param> /// <param name="folder">app name</param> /// <param name="fileName">the zip file created by the CLI</param> /// <param name="functionName">the lambda function name</param> /// <param name="handlerName">the js file with the root function</param> /// <param name="callFunctionName">the root function name</param> public virtual void Create(string bucket, string folder, string fileName, string functionName, string handlerName, string callFunctionName, string cloudProvider, string arn = ARN, int memorySize = MemorySize, int timeout = Timeout) { string url = BaseUrl + "/createLambda"; XMLHttpRequest request = new XMLHttpRequest(); request.open("POST", url, false); Dictionary <string, object> data = new Dictionary <string, object>(); data.Add("bucket", bucket); data.Add("folder", folder); data.Add("fileName", fileName); data.Add("functionName", functionName); data.Add("handlerName", handlerName); data.Add("callFunctionName", callFunctionName); data.Add("Role", arn); data.Add("memorySize", memorySize); data.Add("timeout", timeout); data.Add("cloudProvider", cloudProvider); request.setRequestHeader("content-type", "application/json"); System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer(); request.send(jss.Serialize(data)); if (request.status != 200) { throw new Durados.DuradosException("Server return status " + request.status + ", " + request.responseText); } Dictionary <string, object> response = null; try { response = jss.Deserialize <Dictionary <string, object> >(request.responseText); } catch (Exception exception) { throw new Durados.DuradosException("Could not parse upload response", exception); } }
public void ChangeFolderName(string bucket, string oldFolder, string newFolder) { try { string url = nodeUrl + "/folder/rename"; XMLHttpRequest request = new XMLHttpRequest(); request.open("POST", url, false); Dictionary <string, object> data = new Dictionary <string, object>(); data.Add("bucket", bucket); data.Add("oldFolder", oldFolder); data.Add("newFolder", newFolder); request.setRequestHeader("content-type", "application/json"); System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer(); request.send(jss.Serialize(data)); if (request.status != 200) { Maps.Instance.DuradosMap.Logger.Log("hosting", "folder", request.responseText, null, 1, "status: " + request.status); throw new DuradosException(request.responseText); } Dictionary <string, object>[] response = null; try { response = jss.Deserialize <Dictionary <string, object>[]>(request.responseText); } catch (Exception exception) { Maps.Instance.DuradosMap.Logger.Log("hosting", "folder", exception.Source, exception, 1, request.responseText); throw new DuradosException("Could not parse upload response: " + request.responseText + ". With the following error: " + exception.Message, exception); } } catch (Exception exception) { throw new DuradosException("Failed to rename folder", exception); } }
private void CopyS3(string bucketName, int sourceAppId, int targetAppId) { string sourceAppName = Maps.Instance.GetAppNameById(sourceAppId); string destAppName = Maps.Instance.GetAppNameById(targetAppId); string url = GetNodeUrl() + "/copyFolder"; XMLHttpRequest request = new XMLHttpRequest(); request.open("POST", url, false); Dictionary <string, object> data = new Dictionary <string, object>(); data.Add("bucket", bucketName); data.Add("sourceFolder", sourceAppName); data.Add("destFolder", destAppName); request.setRequestHeader("content-type", "application/json"); System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer(); request.send(jss.Serialize(data)); if (request.status != 200) { Maps.Instance.DuradosMap.Logger.Log("Cloner", "CopyS3", request.responseText, null, 1, "status: " + request.status); throw new DuradosException(request.responseText); } Dictionary <string, object>[] response = null; try { response = jss.Deserialize <Dictionary <string, object>[]>(request.responseText); } catch (Exception exception) { Maps.Instance.DuradosMap.Logger.Log("Cloner", "CopyS3", exception.Source, exception, 1, request.responseText); throw new DuradosException("Could not copy folder, response: " + request.responseText + ". With the following error: " + exception.Message); } }
public void Then(Delegate fulfilledHandler, Delegate errorHandler = null, Delegate progressHandler = null) { var request = new XMLHttpRequest(); request.onreadystatechange = (ev) => { if (request.readyState != 4) { return; } if ((request.status == 200) || (request.status == 304)) { fulfilledHandler.Call(null, request); } else { errorHandler.Call(null, request.responseText); } }; if (!string.IsNullOrWhiteSpace(Options.ContentType)) { request.setRequestHeader("Content-type", Options.ContentType); } request.open(Options.Method, Options.Url); if (Options.Method.ToLower() == "post") { request.send(Options.Data); } else { request.send(); } }
public IHttpActionResult run(int id, bool test = false, bool async = true) { try { if (async && !test) { return(runAsync(id)); } if (!Map.Database.Crons.ContainsKey(id)) { return(ResponseMessage(Request.CreateResponse(HttpStatusCode.NotFound, "The Cron " + id + " does not exist"))); } Cron cron = Map.Database.Crons[id]; if (!IsActive(id)) { return(ResponseMessage(Request.CreateResponse(HttpStatusCode.NotFound, "The Cron " + id + " is not active. Please switch active to 'ON'"))); } CronRequestInfo cronInfo = GetRequestInfo(cron, test); XMLHttpRequest request = new XMLHttpRequest(); request.open(cronInfo.method, cronInfo.url, false); string appName = Map.AppName; request.setRequestHeader("content-type", "application/json"); if (cronInfo.headers != null) { foreach (string key in cronInfo.headers.Keys) { try { request.setRequestHeader(key, cronInfo.headers[key].ToString()); } catch { } } } System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer(); string data = cronInfo.data; request.send(data); if (request.status != 200) { Maps.Instance.DuradosMap.Logger.Log("cron", "run", "failed", new Exception(request.responseText), 1, "status: " + request.status); return(ResponseMessage(Request.CreateResponse(HttpStatusCode.InternalServerError, request.responseText))); } Maps.Instance.DuradosMap.Logger.Log("cron", "run", "success app " + Map.AppName, null, 3, request.responseText.Length > 4000 ? request.responseText.Substring(0, 4000) : request.responseText); object response = request.responseText; try { response = jss.Deserialize <object>(request.responseText); } catch { } if (test) { return(Ok(new { request = GetTestRequest(cronInfo), response = response })); } else { return(Ok(response)); } } catch (ActionNotFoundException exception) { return(ResponseMessage(Request.CreateResponse(HttpStatusCode.NotFound, exception.Message))); } catch (QueryNotFoundException exception) { return(ResponseMessage(Request.CreateResponse(HttpStatusCode.NotFound, exception.Message))); } catch (Exception exception) { throw new BackAndApiUnexpectedResponseException(exception, this); } }
public IHttpActionResult Get(int pageNumber = 1, int pageSize = 100, int minutesAgo = 120) { try { if (pageNumber <= 0) { throw new Durados.DuradosException("pageNumber must be larger than 0"); } if (pageSize <= 0) { throw new Durados.DuradosException("pageSize must be larger than 0"); } if (minutesAgo <= 0) { throw new Durados.DuradosException("minutesAgo must be larger than 0"); } if (pageSize > 1000) { return(ResponseMessage(Request.CreateResponse(HttpStatusCode.BadRequest, new { Messsage = "pageSize is limited to 1000" }))); } if (minutesAgo > 1200) { return(ResponseMessage(Request.CreateResponse(HttpStatusCode.BadRequest, new { Messsage = "minutesAgo is limited to 1200" }))); } pageNumber = pageNumber - 1; Dictionary <string, object> data = new Dictionary <string, object>(); DateTime now = DateTime.UtcNow; var fromTimeEpochTime = GetEpochTime(now.Subtract(new TimeSpan(0, minutesAgo, 0))); var toTimeEpochTime = GetEpochTime(now.Add(new TimeSpan(0, 1, 0))); Debug.WriteLine("now:" + now); Debug.WriteLine("fromTimeEpochTime:" + fromTimeEpochTime); Debug.WriteLine("toTimeEpochTime:" + toTimeEpochTime); data.Add("appName", Map.AppName); data.Add("fromTimeEpochTime", fromTimeEpochTime); data.Add("toTimeEpochTime", toTimeEpochTime); data.Add("offset", pageNumber); data.Add("count", pageSize); string url = BaseUrl + "/lastHourExceptions"; XMLHttpRequest request = new XMLHttpRequest(); request.open("POST", url, false); request.setRequestHeader("content-type", "application/json"); System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer(); string json = jss.Serialize(data); request.send(json); Dictionary <string, object>[] response = null; if (request.status == 200) { try { response = jss.Deserialize <Dictionary <string, object>[]>(request.responseText); } catch (Exception exception) { throw new Durados.DuradosException("Failed to deserialize response " + request.status + ", " + request.responseText, exception); } } else { throw new Durados.DuradosException("Status: " + request.status + ", " + request.responseText); } return(Ok(response)); } catch (Exception exception) { throw new BackAndApiUnexpectedResponseException(exception, this); } }
public virtual void ExecuteOld(object controller, Dictionary <string, Parameter> parameters, View view, Dictionary <string, object> values, DataRow prevRow, string pk, string connectionString, int currentUsetId, string currentUserRole, IDbCommand command, IDbCommand sysCommand, string actionName, string arn, Durados.Security.Cloud.ICloudCredentials awsCredentials) { const string Payload = "Payload"; const string ErrorMessage = "errorMessage"; bool isDebug = IsDebug(values); string url = BaseUrl + "/callLambda"; XMLHttpRequest request = new XMLHttpRequest(); request.open("POST", url, false); Dictionary <string, object> data = new Dictionary <string, object>(); Dictionary <string, object> payload = GetCallLambdaPayload(controller, parameters, view, values, prevRow, pk, connectionString, currentUsetId, currentUserRole, command); string functionName = view.Name + "_" + actionName; string folder = view.Database.GetCurrentAppName(); data.Add("folder", folder); data.Add("functionName", functionName); data.Add("payload", payload); data.Add("Role", arn); if (isDebug) { if (!System.Web.HttpContext.Current.Items.Contains(JavaScript.GuidKey)) { System.Web.HttpContext.Current.Items.Add(JavaScript.GuidKey, Guid.NewGuid()); } data.Add("getLog", true); } request.setRequestHeader("content-type", "application/json"); System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer(); request.send(jss.Serialize(data)); if (request.status != 200) { throw new NodeJsException(request.responseText.TrimStart("{}; ".ToCharArray())); } Dictionary <string, object> response = null; try { response = jss.Deserialize <Dictionary <string, object> >(request.responseText); } catch (Exception exception) { throw new Durados.DuradosException("Could not parse NodeJS response", exception); } if (response.ContainsKey(FunctionError)) { if (response.ContainsKey(Payload)) { var responsePayload = response[Payload]; if (responsePayload is string) { IDictionary <string, object> responsePayloadError = null; try { responsePayloadError = jss.Deserialize <Dictionary <string, object> >((string)responsePayload); } catch { throw new NodeJsException((string)responsePayload); } if (responsePayloadError.ContainsKey(ErrorMessage)) { throw new NodeJsException(responsePayloadError[ErrorMessage].ToString()); } else { throw new NodeJsException((string)responsePayload); } } else { throw new NodeJsException(request.responseText.TrimStart("{}; ".ToCharArray())); } } else { throw new NodeJsException(request.responseText.TrimStart("{}; ".ToCharArray())); } } if (response != null && values != null) { if (isDebug) { //HandleLog(response); } if (!values.ContainsKey(JavaScript.ReturnedValueKey)) { values.Add(JavaScript.ReturnedValueKey, response); } else { values[JavaScript.ReturnedValueKey] = response; } } }
public virtual void Execute(object controller, Dictionary <string, Parameter> parameters, View view, Dictionary <string, object> values, DataRow prevRow, string pk, string connectionString, int currentUserId, string currentUserRole, IDbCommand command, IDbCommand sysCommand, string actionName, string arn, Durados.Security.Cloud.ICloudCredentials cloudCredentials, bool isLambda) { bool isDebug = IsDebug(values); string url = BaseUrl + "/invokeFunction"; XMLHttpRequest request = new XMLHttpRequest(); request.open("POST", url, false); Dictionary <string, object> data = new Dictionary <string, object>(); data.Add("credentials", cloudCredentials.GetCredentials()); data.Add("cloudProvider", cloudCredentials.GetProvider()); data.Add("method", GetActionMethod()); Dictionary <string, object> payload = GetCallLambdaPayload(controller, parameters, view, values, prevRow, pk, connectionString, currentUserId, currentUserRole, command); string folder = view.Database.GetCurrentAppName(); string functionArn = arn + folder + "_" + view.Name + "_" + actionName; if (isLambda) { functionArn = arn; payload = GetCallLambdaPayloadExternal(controller, parameters, view, values, prevRow, pk, connectionString, currentUserId, currentUserRole, command); foreach (string key in values.Keys) { string stripedKey = key.StripToken(); if (payload.ContainsKey(stripedKey)) { throw new WorkflowEngineException("You can not add " + stripedKey + " parameter in the request body"); } if (key != DebugKey) { payload.Add(stripedKey, values[key]); } } } data.Add("payload", payload); data.Add("function", cloudCredentials.GetFunctionObject(functionArn)); Guid requestId = Guid.NewGuid(); if (isDebug) { if (!System.Web.HttpContext.Current.Items.Contains(JavaScript.GuidKey)) { System.Web.HttpContext.Current.Items.Add(JavaScript.GuidKey, requestId); } else { requestId = (Guid)System.Web.HttpContext.Current.Items[JavaScript.GuidKey]; } string appName = (string)System.Web.HttpContext.Current.Items["appName"]; string username = (string)System.Web.HttpContext.Current.Items["username"]; string token = (string)System.Web.HttpContext.Current.Request.Headers["authorization"]; data.Add("backandRequest", new { id = requestId.ToString(), appName = appName, username = username, accessToken = token }); data.Add("isProduction", false); } else { data.Add("isProduction", true); } request.setRequestHeader("content-type", "application/json"); System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer(); request.send(jss.Serialize(data)); if (IsFunction(view)) { FunctionResponse(request, jss, values, isDebug, requestId); return; } if (request.status != 200) { throw new NodeJsException(request.responseText.TrimStart("{}; ".ToCharArray())); } Dictionary <string, object> response = null; try { response = jss.Deserialize <Dictionary <string, object> >(request.responseText); } catch (Exception exception) { throw new Durados.DuradosException("Could not parse NodeJS response", exception); } if (response.ContainsKey(FunctionError)) { if (response.ContainsKey(Payload)) { var responsePayload = response[Payload]; if (responsePayload is string) { IDictionary <string, object> responsePayloadError = null; try { responsePayloadError = jss.Deserialize <Dictionary <string, object> >((string)responsePayload); } catch { throw new NodeJsException((string)responsePayload); } if (responsePayloadError.ContainsKey(ErrorMessage)) { throw new NodeJsException(responsePayloadError[ErrorMessage].ToString()); } else { throw new NodeJsException((string)responsePayload); } } else { throw new NodeJsException(request.responseText.TrimStart("{}; ".ToCharArray())); } } else { throw new NodeJsException(request.responseText.TrimStart("{}; ".ToCharArray())); } } if (response != null && values != null) { if (isDebug) { HandleLog(response, requestId); } CleanResponse(response); if (!values.ContainsKey(JavaScript.ReturnedValueKey)) { values.Add(JavaScript.ReturnedValueKey, response); } else { values[JavaScript.ReturnedValueKey] = response; } } }