コード例 #1
0
 public BaseTestFixture()
 {
     _configuration  = new KillBillConfiguration("http://localhost:8080", _apiKey, "SyxSecret", "admin", "password");
     _client         = new KillBillClient(Configuration);
     _requestOptions = RequestOptions.Builder()
                       .WithRequestId(Guid.NewGuid().ToString())
                       .WithCreatedBy(CreatedBy)
                       .WithReason(Reason)
                       .WithUser(Configuration.HttpUser)
                       .WithPassword(Configuration.HttpPassword)
                       .WithTenantApiKey(Configuration.ApiKey)
                       .WithTenantApiSecret(Configuration.ApiSecret)
                       .WithComment("kill-bill-net-tests")
                       .Build();
 }
コード例 #2
0
        // SEND REQUEST MAYBE FOLLOW
        // -------------------------------------------------------------------------------
        private async Task <T> SendRequestAndMaybeFollowLocation <T>(Method method, string uri, object body, RequestOptions requestOptions)
            where T : class
        {
            var request = BuildRequestWithHeaderAndQuery(method, uri, requestOptions);

            if (method != Method.GET || method != Method.HEAD)
            {
                if (body != null)
                {
                    AddRequestBody(request, body, requestOptions.ContentType);
                }
            }

            // WHEN FollowLocation == TRUE
            if (requestOptions.FollowLocation == true)
            {
                var responseToFollow = await ExecuteRequest(request, requestOptions);

                var locationHeader = responseToFollow?.Headers.SingleOrDefault(
                    h => h.Type == ParameterType.HttpHeader && h.Name == "Location");

                if (locationHeader?.Value == null || locationHeader.Value.ToString() == string.Empty)
                {
                    return(default(T));
                }

                var locationUri = new Uri(locationHeader.Value.ToString());

                var optionsForFollow = RequestOptions.Builder()
                                       .WithUser(requestOptions.User)
                                       .WithPassword(requestOptions.Password)
                                       .WithTenantApiKey(requestOptions.TenantApiKey)
                                       .WithTenantApiSecret(requestOptions.TenantApiSecret)
                                       .WithRequestId(requestOptions.RequestId)
                                       .WithFollowLocation(false)
                                       .WithQueryParams(requestOptions.QueryParamsForFollow)
                                       .Build();

                return(await Get <T>(locationUri.PathAndQuery, optionsForFollow));
            }

            var response = await ExecuteRequest(request, requestOptions);

            // If there is no response (204) or if an object cannot be found (404), the code will return null (for single objects) or an empty list (for collections of objects).
            if (response.StatusCode == HttpStatusCode.NoContent || response.StatusCode == HttpStatusCode.NotFound)
            {
                // Return empty list for KillBillObjects instead of null for convenience
                return(typeof(T).IsAssignableFrom(typeof(KillBillObjects <>)) ? Activator.CreateInstance <T>() : default(T));
            }

            // If there we did not want to follow the location on a POST method, we return the default
            if (method == Method.POST && requestOptions.FollowLocation != true)
            {
                // Return empty list for KillBillObjects instead of null for convenience
                return(typeof(T).IsAssignableFrom(typeof(KillBillObjects <>)) ? Activator.CreateInstance <T>() : default(T));
            }

            var data = DeserializeResponse <T>(response);

            return(data);
        }