Esempio n. 1
0
        /// <summary>
        /// Converts an Algo object to JSON format (string).
        /// </summary>
        public static AlgoValue ToJson(ParserRuleContext context, params AlgoValue[] args)
        {
            //Check the argument given is an object.
            if (args[0].Type != AlgoValueType.Object && args[0].Type != AlgoValueType.List)
            {
                Error.Fatal(context, "Value to serialize to JSON must be an object or list.");
                return(null);
            }

            if (args[0].Type != AlgoValueType.List)
            {
                //Returning the object parsed string.
                return(new AlgoValue()
                {
                    Type = AlgoValueType.String,
                    Value = AlgoConversion.ObjToJsonStr(context, args[0])
                });
            }
            else
            {
                //Returning the list parsed string.
                return(new AlgoValue()
                {
                    Type = AlgoValueType.String,
                    Value = AlgoConversion.ListToJsonStr(context, args[0])
                });
            }
        }
Esempio n. 2
0
 //Convert values with static casts.
 //String.
 public static AlgoValue ConvertString(ParserRuleContext context, params AlgoValue[] args)
 {
     //Return converted string representation of the value.
     return(new AlgoValue()
     {
         Type = AlgoValueType.String,
         Value = AlgoConversion.GetStringRepresentation(args[0])
     });
 }
Esempio n. 3
0
        //Returns whether the given string contains a specific substring.
        public static AlgoValue ContainsString(ParserRuleContext context, params AlgoValue[] args)
        {
            //Get the base string and the substring.
            string baseString = AlgoConversion.GetStringRepresentation(args[0]);
            string substring  = AlgoConversion.GetStringRepresentation(args[1]);

            //Return a bool.
            return(new AlgoValue()
            {
                Type = AlgoValueType.Boolean,
                Value = baseString.Contains(substring)
            });
        }
Esempio n. 4
0
        public static AlgoValue POST(ParserRuleContext context, params AlgoValue[] args)
        {
            //Check first parameter is a string, second is an object.
            if (args[0].Type != AlgoValueType.String)
            {
                Error.Fatal(context, "URL to POST to must be a string.");
                return(null);
            }

            if (args[1].Type != AlgoValueType.Object)
            {
                Error.Fatal(context, "Data to POST must be an object.");
                return(null);
            }

            //Attempt to convert object to JSON.
            string json = AlgoConversion.ObjToJsonStr(context, args[1]);

            //Attempt to POST.
            var httpWebRequest = (HttpWebRequest)WebRequest.Create((string)args[0].Value);

            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method      = "POST";

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                streamWriter.Write(json);
            }

            var             result       = "";
            HttpWebResponse httpResponse = null;

            //Attempt to grab result.
            try
            {
                httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    result = streamReader.ReadToEnd();
                }
            }
            catch (Exception e)
            {
                Error.Fatal(context, "Error when sending POST request: '" + e.Message + "'.");
                return(null);
            }

            //Creating return object.
            AlgoScopeCollection scope = new AlgoScopeCollection();

            scope.AddVariable("status", new AlgoValue()
            {
                Type  = AlgoValueType.Integer,
                Value = new BigInteger((int)httpResponse.StatusCode)
            });
            scope.AddVariable("status_desc", new AlgoValue()
            {
                Type  = AlgoValueType.String,
                Value = httpResponse.StatusDescription
            });
            scope.AddVariable("content", new AlgoValue()
            {
                Type  = AlgoValueType.String,
                Value = result
            });

            //Returning it.
            return(new AlgoValue()
            {
                Type = AlgoValueType.Object,
                Value = new AlgoObject()
                {
                    ObjectScopes = scope
                }
            });
        }