Example #1
0
        public static async Task <ToxResponse> sendRequest(Skynet host, ToxRequest req)
        {
            // if req is not send to local node
            if (host.tox.Id.ToString() != req.toToxId)
            {
                bool mResStatus = false;
                return(await host.sendRequest(new ToxId(req.toToxId), req, out mResStatus));
            }

            string baseUrl = "http://localhost:" + host.httpPort + "/";
            var    request = (HttpWebRequest)WebRequest.Create(baseUrl + "api/" + req.url);

            request.Headers.Add("Uuid", req.uuid);
            request.Headers.Add("From-Node-Id", req.fromNodeId);
            request.Headers.Add("From-Tox-Id", req.fromToxId);
            request.Headers.Add("To-Node-Id", req.toNodeId);
            request.Headers.Add("To-Tox-Id", req.toToxId);
            request.Headers.Add("Skynet-Time", req.time + "");
            request.Method      = req.method.ToUpper();
            request.ContentType = "application/json";

            List <string> allowedMethods = new List <string> {
                "POST", "PUT", "PATCH"
            };

            if (allowedMethods.Any(x => x == req.method.ToUpper()))
            {
                // only the above methods are allowed to add body data
                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    streamWriter.Write(req.content);
                }
            }
            var response = await request.GetResponseAsync();

            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

            return(req.createResponse(responseString));
        }