Exemple #1
0
        private T callRemoteApi <T, U>(U request, HotelApiPaths.HotelApiPathsBase path, List <Tuple <string, string> > param)
        {
            try
            {
                T response = default(T);

                using (var client = new HttpClient())
                {
                    if (request == null && (path.GetType() != typeof(HotelApiPaths.STATUS) &&
                                            path.GetType() != typeof(HotelApiPaths.BOOKING_CANCEL) && path.GetType() != typeof(HotelApiPaths.BOOKING_DETAIL) && path.GetType() != typeof(HotelApiPaths.BOOKING_LIST)))
                    {
                        throw new Exception("Object request can't be null");
                    }

                    client.BaseAddress = new Uri(path.getUrl(this.basePath, this.version));
                    client.DefaultRequestHeaders.Clear();
                    client.Timeout = new TimeSpan(0, 0, this.readTimeout);
                    client.DefaultRequestHeaders.Add("Api-Key", this.apiKey);
                    client.DefaultRequestHeaders.Add("Accept-Enconding", "Gzip");
                    client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
                    client.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/json");

                    long   ts         = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds / 1000;
                    SHA256 hashstring = SHA256Managed.Create();
                    byte[] hash       = hashstring.ComputeHash(Encoding.UTF8.GetBytes(this.apiKey + this.sharedSecret + ts));
                    string signature  = BitConverter.ToString(hash).Replace("-", "");
                    client.DefaultRequestHeaders.Add("X-Signature", signature.ToString());

                    // GET Method
                    if (path.getHttpMethod() == HttpMethod.Get)
                    {
                        string Uri = path.getEndPoint();

                        if (param != null)
                        {
                            Uri = path.getEndPoint(param);
                        }

                        HttpResponseMessage resp = client.GetAsync(Uri).Result;
                        response = resp.Content.ReadAsAsync <T>().Result;
                        return(response);
                    }

                    // DELETE Method
                    if (path.getHttpMethod() == HttpMethod.Delete)
                    {
                        string Uri = path.getEndPoint();

                        if (param != null)
                        {
                            Uri = path.getEndPoint(param);
                        }

                        HttpResponseMessage resp = client.DeleteAsync(Uri).Result;
                        response = resp.Content.ReadAsAsync <T>().Result;
                        return(response);
                    }

                    StringContent contentToSend = null;
                    if (request != null)
                    {
                        string objectSerialized = JsonConvert.SerializeObject(request, Formatting.Indented, new JsonSerializerSettings()
                        {
                            DefaultValueHandling = DefaultValueHandling.Ignore
                        });
                        contentToSend = new StringContent(objectSerialized, Encoding.UTF8, "application/json");
                    }

                    if (path.getHttpMethod() == HttpMethod.Post)
                    {
                        HttpResponseMessage resp = null;
                        if (param == null)
                        {
                            resp = client.PostAsync(path.getEndPoint(), contentToSend).Result;
                        }
                        else
                        {
                            resp = client.PostAsync(path.getEndPoint(param), contentToSend).Result;
                        }

                        response = resp.Content.ReadAsAsync <T>().Result;
                    }
                }

                return(response);
            }
            catch (HotelSDKException e)
            {
                throw e;
            }
        }
Exemple #2
0
 private T callRemoteApi <T, U>(U request, HotelApiPaths.HotelApiPathsBase path, List <Tuple <string, string> > param, HotelApiVersion version)
 {
     return(callRemoteApi <T, U>(request, path, param, version, this.basePath));
 }