예제 #1
0
        /// <inheritdoc />
        public async Task <ResultOrError <RoomReservation> > GetVenueBookingRoomReservationAsync(
            int venueId,
            int bookingId,
            int reservationId)
        {
            var listResult = await GetVenueBookingRoomReservationListAsync(
                venueId, 1, 0, bookingId, new Dictionary <string, object>() {
                { "id", reservationId }
            }
                );

            var newResult = new ResultOrError <RoomReservation>()
            {
                ErrorCode         = listResult.ErrorCode,
                ErrorCodeSpecific = listResult.ErrorCodeSpecific,
                ErrorMessage      = listResult.ErrorMessage
            };

            if (listResult.IsSuccess())
            {
                if (listResult.Result.Meta.Count == 1)
                {
                    newResult.Result = listResult.Result.Results[0];
                }
                else
                {
                    newResult.ErrorCode    = libErrorCode;
                    newResult.ErrorMessage = "The room reservation does not exist";
                }
            }
            return(newResult);
        }
예제 #2
0
        /// <summary>
        /// Calls a method of the iVvy api. Encapsulates the header and
        /// signing requirements.
        /// </summary>
        public async Task <ResultOrError <T> > CallAsync <T>(
            string apiNamespace, string action, object requestData) where T : new()
        {
            if (ApiVersion != "1.0")
            {
                throw new ArgumentException("ApiVersion is not a valid version number");
            }
            var postData = "";

            if (requestData != null)
            {
                postData = serializer.Serialize(requestData);
            }
            var method              = "POST";
            var contentType         = "application/json; charset=utf-8";
            var contentMD5          = Utils.GetMD5Hash(postData);
            var date                = "";
            var requestUri          = "/api/" + ApiVersion + "/" + apiNamespace + "?action=" + action;
            var ivvyDate            = DateTime.Now.ToUniversalTime().ToString(Utils.DateTimeFormat);
            var headersToSign       = "IVVYDate=" + ivvyDate;
            var initialStringToSign = method + contentMD5 + contentType + date + requestUri + ApiVersion + headersToSign;
            var stringToSign        = initialStringToSign.ToLower();
            var signature           = Utils.SignString(stringToSign, ApiSecret);

            var message = new HttpRequestMessage(HttpMethod.Post, BaseUrl + requestUri);

            message.Headers.Clear();
            message.Content = new StringContent(postData, Encoding.UTF8, "application/json");
            message.Content.Headers.Add("Content-MD5", contentMD5);
            message.Content.Headers.Add("IVVY-Date", ivvyDate);
            message.Content.Headers.Add("X-Api-Authorization", "IWS " + ApiKey + ":" + signature);

            HttpResponseMessage httpResponse = null;
            ResultOrError <T>   result       = null;

            try
            {
                var httpClient = httpClientWrapper.GetHttpClient();
                httpResponse = await httpClient.SendAsync(message);

                var data = await httpResponse.Content.ReadAsStringAsync();

                await Events.AfterApiCalledAsync(new ApiCallDetails(
                                                     apiNamespace,
                                                     action,
                                                     message.Headers,
                                                     message.Content?.Headers,
                                                     postData,
                                                     httpResponse.StatusCode,
                                                     httpResponse.Headers,
                                                     data
                                                     ));

                result = deserializer.Deserialize <T>(data);
                if (result == null)
                {
                    result = new ResultOrError <T>()
                    {
                        ErrorCode         = libErrorCode,
                        ErrorCodeSpecific = "CallAsync",
                        ErrorMessage      = "Received invalid response.",
                    };
                }
            }
            catch (Exception ex)
            {
                result = new ResultOrError <T>()
                {
                    ErrorCode         = libErrorCode,
                    ErrorCodeSpecific = "CallAsync",
                    ErrorMessage      = ex.Message,
                };
            }
            finally
            {
                if (httpResponse != null)
                {
                    httpResponse.Dispose();
                }
            }
            return(result);
        }