Exemple #1
0
        /// <summary>
        /// Execute the request, and parse the response with parser.
        /// </summary>
        /// <typeparam name="TRequest"></typeparam>
        /// <typeparam name="TResponse"></typeparam>
        /// <param name="request">the request of the API</param>
        /// <param name="parser">if it's NULL, will get parser according to the Format of ApiClient.</param>
        /// <returns></returns>
        public virtual TResponse Execute <TRequest, TResponse>(TRequest request, IApiParser parser = null)
            where TRequest : ApiRequest
            where TResponse : ApiResponse <TRequest>, new()
        {
            parser = parser ?? GetDefaultParser();
            if (parser == null)
            {
                throw new ApiException("Sorry, can't get the correct parser from parameter and request.");
            }
            var responseBody = default(string);

            try
            {
                var accept = Format == FORMAT_JSON ? "application/json" : "application/xml";

                responseBody = DoExecuteInternal(request, accept);
                return(parser.Parse <TResponse>(responseBody));
            }
            catch (Exception e)
            {
                if (ReportServerURLError)
                {
                    return(new TResponse
                    {
                        ResponseBody = responseBody,
                        ErrorCode = ApiErrorCode.SERVERURL_ERROR,
                        ErrorMessage = "sorry, your API Server URL might be wrong."
                    });
                }
                else
                {
                    throw e;
                }
            }
        }
 public QueueingKCVDBClient(
     IApiParser apiParser,
     IApiDataSender dataSender
     )
 {
     DataSender = dataSender;
     ApiParser  = apiParser;
 }
 public SearchController(ISearchRepository repo, IApiParser parser)
 {
     this.parser = parser;
     this.repo   = repo;
 }
Exemple #4
0
 public virtual async Task <TResponse> ExecuteAsync <TRequest, TResponse>(TRequest request, IApiParser parser = null)
     where TRequest : ApiRequest
     where TResponse : ApiResponse <TRequest>, new()
 {
     return(await Task.FromResult(Execute <TRequest, TResponse>(request, parser)));
 }