Ejemplo n.º 1
0
        private string GetDataFromFolio(FolioRequest folioRequest)
        {
            if (string.IsNullOrEmpty(_token))
            {
                SetToken();
            }

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_folioApiBaseAddress + folioRequest.Path);

            request.Accept      = folioRequest.Accept;
            request.ContentType = "application/json";
            request.Headers["x-okapi-tenant"] = _tenant;
            request.Headers["x-okapi-token"]  = _token;
            request.Method = folioRequest.Method;

            if (string.IsNullOrEmpty(folioRequest.Body) == false)
            {
                UTF8Encoding encoding  = new UTF8Encoding();
                var          bodyBytes = encoding.GetBytes(folioRequest.Body);
                request.ContentLength = bodyBytes.Length;
                var requestStream = request.GetRequestStream();
                requestStream.Write(bodyBytes, 0, bodyBytes.Length);
            }

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            if ((int)response.StatusCode >= 200 && (int)response.StatusCode < 300)
            {
                var outputStream = response.GetResponseStream();
                var sr           = new StreamReader(outputStream);
                var resultString = sr.ReadToEnd();

                if (resultString.ToLower().Contains("invalid token"))
                {
                    throw new InvalidTokenException();
                }

                return(resultString);
            }
            else
            {
                throw new FolioRequestException();
            }
        }
Ejemplo n.º 2
0
        private string GetDataFromFolioWithRetries(FolioRequest folioRequest)
        {
            dynamic res        = null;
            var     retry      = true;
            var     retryCount = 1;

            while (retry)
            {
                try
                {
                    res   = GetDataFromFolio(folioRequest);
                    retry = false;
                }
                catch (InvalidTokenException)
                {
                    SetToken();
                    retry = retryCount > 0;
                }
                retryCount -= 1;
            }
            return(res);
        }