Esempio n. 1
0
        /// <summary>
        /// Perform a PAPI request
        /// </summary>
        /// <param name="method">HTTP method of the PAPI method</param>
        /// <param name="url">The URL of the method, beginning with /PAPIService</param>
        /// <param name="pin">The patron's pin for public or protected token Access Secret for protected methods</param>
        /// <param name="body">The XML request body, if any</param>
        /// <param name="isOverride">Execute as an override request</param>
        /// <returns>A string of the response XML</returns>
        public PapiResponse <string> Execute(HttpMethod method, string url, string pin = null, string body = null, bool isOverride = false)
        {
            PapiResponse <string> papiResponse = new PapiResponse <string>();

            try
            {
                var request = CreateRequest(method, url, pin, body, isOverride);
                papiResponse = new PapiResponse <string>(request);
                var response = client.SendAsync(request).Result;
                papiResponse.Response = new HttpResponse(response);
                papiResponse.Data     = response.Content.ReadAsStringAsync().Result;
                return(papiResponse);
            }
            catch (Exception ex)
            {
                papiResponse.Exception = ex;
                return(papiResponse);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Execute a PAPI request
        /// </summary>
        /// <typeparam name="T">Response data type</typeparam>
        /// <param name="method">HTTP method of the PAPI method</param>
        /// <param name="url">The URL of the method, beginning with /PAPIService</param>
        /// <param name="barcode">The patron's barcode, for patron requests</param>
        /// <param name="pin">The patron's pin for public or protected token Access Secret for protected methods</param>
        /// <param name="body">The XML request body, if any</param>
        /// <param name="isOverride">Execute as an override request</param>
        /// <returns>A PapiResponse object with a deserialized data object of type T</returns>
        public PapiResponse <T> Execute <T>(HttpMethod method, string url, string pin = null, string body = null, bool isOverride = false, string barcode = null)
        {
            if (barcode != null)
            {
                pin = HandlePatronAuth(barcode, pin);
            }

            var request      = CreateRequest(method, url, pin, body, isOverride);
            var papiResponse = new PapiResponse <T>(request);

            try
            {
                var response = client.SendAsync(request).Result;
                papiResponse.Response = new HttpResponse(response);
                if (response.IsSuccessStatusCode)
                {
                    if (typeof(T) == typeof(string))
                    {
                        papiResponse.Data = (T)Convert.ChangeType(response.Content.ReadAsStringAsync().Result, typeof(T));
                    }
                    else
                    {
                        papiResponse.Data = (T) new XmlSerializer(typeof(T)).Deserialize(response.Content.ReadAsStreamAsync().Result);
                    }
                }
            }
            catch (Exception ex)
            {
                papiResponse.Exception = ex;
                papiResponse.Response  = new HttpResponse()
                {
                    StatusCode = System.Net.HttpStatusCode.NotFound, ReasonPhrase = ex.InnerException != null ? ex.InnerException.InnerException != null ? ex.InnerException.InnerException.Message : ex.InnerException.Message : ex.Message
                };
            }
            return(papiResponse);
        }