Example #1
0
        public bool WritePayload(string pathwayId, string payload)
        {
            bool completed = false;

            if (!Pathways.ContainsKey(pathwayId))
            {
                throw new ArgumentException($"Pathway id not found: {pathwayId}");
            }

            string sendPayload = null;

            if (!string.IsNullOrWhiteSpace(this.EncryptKey))
            {
                sendPayload = MakeEncryptedPayload(payload);
            }
            else
            {
                sendPayload = Base64EncodeString(payload, true);
            }

            int fullCircleIndex = RestApiUriUseIndex;
            int serversLeft     = RestApiUris.Count;

            while (serversLeft > 0 && !completed)
            {
                serversLeft--;
                var baseUri = RestApiUris[RestApiUriUseIndex];
                RestApiUriUseIndex++;
                if (RestApiUriUseIndex == RestApiUris.Count)
                {
                    RestApiUriUseIndex = 0;
                }
                RestApiResponse r = MakeTheCall(
                    baseUri + $"/api/pathways/{pathwayId}/payloads/write",
                    "POST",
                    new string[] { $"Access-Token: {this.UserAccessToken}", $"Pathway-Token: {Pathways[pathwayId].WriteToken}", "content-type: text/plain" },
                    sendPayload
                    );
                switch (r.StatusCode)
                {
                case 200:
                    completed = true;
                    break;

                case 429:
                    continue;     // try the next server

                case 409:
                    continue;     // try the next server

                default:
                    throw new IOException($"{r.StatusCode}: {r.StatusDescription}");
                }
            }
            return(completed);
        }
Example #2
0
        public string ReadPayload(string pathwayId)
        {
            string result = string.Empty;

            if (!Pathways.ContainsKey(pathwayId))
            {
                throw new ArgumentException($"Pathway id not found: {pathwayId}");
            }
            int  fullCircleIndex = RestApiUriUseIndex;
            bool completed       = false;
            int  serversLeft     = RestApiUris.Count;

            while (serversLeft > 0 && !completed)
            {
                serversLeft--;
                var baseUri = RestApiUris[RestApiUriUseIndex];
                RestApiUriUseIndex++;
                if (RestApiUriUseIndex == RestApiUris.Count)
                {
                    RestApiUriUseIndex = 0;
                }
                RestApiResponse r = MakeTheCall(
                    baseUri + $"/api/pathways/{pathwayId}/payloads/read",
                    "GET",
                    new string[] { $"Access-Token: {this.UserAccessToken}", $"Pathway-Token: {Pathways[pathwayId].ReadToken}", "Accept: tet/plain" },
                    string.Empty
                    );
                switch (r.StatusCode)
                {
                case 204:
                    // no payload at this Uri: try the next one.
                    continue;

                case 200:
                    if (!string.IsNullOrWhiteSpace(this.EncryptKey))
                    {
                        result = ReadEncryptedPayload(r.Body);
                    }
                    else
                    {
                        result = Base64DecodeString(r.Body);
                    }
                    completed = true;
                    break;

                default:
                    throw new IOException($"{r.StatusCode}: {r.StatusDescription}");
                }
            }
            return(result);
        }
Example #3
0
        public void ServerShutdown()
        {
            foreach (var key in RestApiUris.Keys)
            {
                RestApiResponse r = MakeTheCall(
                    RestApiUris[key] + "/api/admin/shutdown",
                    "GET",
                    new string[] { $"Access-Token: {this.SuperAccessToken}" },
                    string.Empty
                    );
                switch (r.StatusCode)
                {
                case 200:
                    break;

                default:
                    throw new IOException(r.StatusDescription);
                }
            }
        }
Example #4
0
        public string GetReference(string pathwayId, string referenceKey)
        {
            string result = string.Empty;

            if (!Pathways.ContainsKey(pathwayId))
            {
                throw new ArgumentException($"Pathway id not found: {pathwayId}");
            }
            foreach (var key in RestApiUris.Keys)
            {
                RestApiResponse r = MakeTheCall(
                    RestApiUris[key] + $"/api/pathways/{pathwayId}/references/get/{referenceKey}",
                    "GET",
                    new string[] { $"Access-Token: {this.UserAccessToken}", $"Pathway-Token: {Pathways[pathwayId].ReadToken}" },
                    string.Empty
                    );
                switch (r.StatusCode)
                {
                case 204:
                    // no reference at this Uri: try the next one.
                    continue;

                case 200:
                    if (!string.IsNullOrWhiteSpace(this.EncryptKey))
                    {
                        result = ReadEncryptedPayload(r.Body);
                    }
                    else
                    {
                        result = Base64DecodeString(r.Body);
                    }
                    break;

                default:
                    throw new IOException($"{r.StatusCode}: {r.StatusDescription}");
                }
            }
            return(result);
        }
Example #5
0
        public void DeletePathway(string id)
        {
            if (!Pathways.ContainsKey(id))
            {
                throw new ArgumentException($"Pathway id not found: {id}");
            }
            foreach (var key in RestApiUris.Keys)
            {
                RestApiResponse r = MakeTheCall(
                    RestApiUris[key] + $"/api/pathways/delete/{id}",
                    "GET",
                    new string[] { $"Access-Token: {this.AdminAccessToken}" },
                    string.Empty
                    );
                switch (r.StatusCode)
                {
                case 200:
                    break;

                default:
                    throw new IOException($"{r.StatusCode}: {r.StatusDescription}");
                }
            }
        }
Example #6
0
        public string ServerClients()
        {
            StringBuilder result = new StringBuilder();

            result.AppendLine("{");
            int index = 0;

            foreach (var key in RestApiUris.Keys)
            {
                RestApiResponse r = MakeTheCall(
                    RestApiUris[key] + "/api/admin/clients",
                    "GET",
                    new string[] { $"Access-Token: {this.AdminAccessToken}" },
                    string.Empty
                    );
                switch (r.StatusCode)
                {
                case 200:
                    Uri u = new Uri(RestApiUris[key]);
                    result.Append(r.Body);
                    break;

                default:
                    result.Append($"\"{key}\": {{ \"statusCode\": \"{r.StatusCode}\", \"statusDescription\": \"{r.StatusDescription}\" }}");
                    break;
                }
                index++;
                if (index != RestApiUris.Keys.Count)
                {
                    result.Append(",");
                }
                result.AppendLine();
            }
            result.AppendLine("}");
            return(result.ToString());
        }
Example #7
0
        private RestApiResponse MakeTheCall(string url, string method, string[] headers, string body)
        {
            var             result   = new RestApiResponse();
            HttpWebRequest  request  = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse response = null;

            try
            {
                request.Credentials = CredentialCache.DefaultCredentials;
                request.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
                ((HttpWebRequest)request).UserAgent = ".NET BOG.Pathway.Client";
                request.Method = method.ToUpper();
                foreach (string headerLine in headers)
                {
                    var breakpoint = headerLine.IndexOf(":");
                    var key        = breakpoint == -1 ? headerLine.Trim() : headerLine.Substring(0, breakpoint);
                    var value      = (breakpoint == -1 || breakpoint == (headerLine.Length - 1)) ? string.Empty : headerLine.Substring(breakpoint + 1).Trim();
                    if (key.ToLower() == "accept")
                    {
                        request.Accept = value;
                    }
                    else if (key.ToLower() == "content-type")
                    {
                        request.ContentType = value;
                    }
                    else
                    {
                        request.Headers.Set(key, value);
                    }
                }
                if (method.ToUpper() == "POST" || method.ToUpper() == "PUT")
                {
                    request.ContentLength = body.Length;
                    using (var sw = new StreamWriter(request.GetRequestStream()))
                    {
                        sw.Write(body);
                    }
                }

                using (response = (HttpWebResponse)request.GetResponse())
                {
                    result.StatusCode        = (int)response.StatusCode;
                    result.StatusDescription = response.StatusDescription;
                    for (int index = 0; index < response.Headers.Count; index++)
                    {
                        var key    = response.Headers.GetKey(index);
                        var values = response.Headers.GetValues(index);
                        if (key.ToLower() == "content-type")
                        {
                            result.ContentType = values[0];
                        }
                    }
                    result.Body = new StreamReader(response.GetResponseStream()).ReadToEnd().Replace("\r", string.Empty).Replace("\n", "\r\n");
                }
            }
            catch (WebException webex)
            {
                if (webex.Status == WebExceptionStatus.ProtocolError && webex.Response != null)
                {
                    var resp = (HttpWebResponse)webex.Response;
                    result.StatusCode        = (int)resp.StatusCode;
                    result.StatusDescription = resp.StatusDescription;
                }
                else
                {
                    result.StatusCode        = 599;
                    result.StatusDescription = webex.Message;
                }
            }
            catch (Exception err)
            {
                result.StatusCode        = 999;
                result.StatusDescription = err.Message;
            }
            return(result);
        }