
csharp.ApiClient.mustache Maven / Gradle / Ivy
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;
using System.IO;
using System.Web;
using System.Linq;
using System.Net;
using System.Text;
using Newtonsoft.Json;
using RestSharp;
using RestSharp.Extensions;
namespace {{packageName}}.Client
{
///
/// API client is mainly responible for making the HTTP call to the API backend.
///
public class ApiClient
{
private readonly Dictionary _defaultHeaderMap = new Dictionary();
///
/// Initializes a new instance of the class.
///
/// The base path.
public ApiClient(String basePath="{{basePath}}")
{
BasePath = basePath;
RestClient = new RestClient(BasePath);
}
///
/// Gets or sets the base path.
///
/// The base path
public string BasePath { get; set; }
///
/// Gets or sets the RestClient.
///
/// An instance of the RestClient
public RestClient RestClient { get; set; }
///
/// Gets the default header.
///
public Dictionary DefaultHeader
{
get { return _defaultHeaderMap; }
}
// Creates and sets up a RestRequest prior to a call.
private RestRequest PrepareRequest(
String path, RestSharp.Method method, Dictionary queryParams, String postBody,
Dictionary headerParams, Dictionary formParams,
Dictionary fileParams, Dictionary pathParams, String[] authSettings)
{
var request = new RestRequest(path, method);
UpdateParamsForAuth(queryParams, headerParams, authSettings);
// add default header, if any
foreach(var defaultHeader in _defaultHeaderMap)
request.AddHeader(defaultHeader.Key, defaultHeader.Value);
// add path parameter, if any
foreach(var param in pathParams)
request.AddParameter(param.Key, param.Value, ParameterType.UrlSegment);
// add header parameter, if any
foreach(var param in headerParams)
request.AddHeader(param.Key, param.Value);
// add query parameter, if any
foreach(var param in queryParams)
request.AddQueryParameter(param.Key, param.Value);
// add form parameter, if any
foreach(var param in formParams)
request.AddParameter(param.Key, param.Value);
// add file parameter, if any
foreach(var param in fileParams)
request.AddFile(param.Value.Name, param.Value.Writer, param.Value.FileName, param.Value.ContentType);
if (postBody != null) // http body (model) parameter
request.AddParameter("application/json", postBody, ParameterType.RequestBody);
return request;
}
///
/// Makes the HTTP request (Sync).
///
/// URL path.
/// HTTP method.
/// Query parameters.
/// HTTP body (POST request).
/// Header parameters.
/// Form parameters.
/// File parameters.
/// Path parameters.
/// Authentication settings.
/// Object
public Object CallApi(
String path, RestSharp.Method method, Dictionary queryParams, String postBody,
Dictionary headerParams, Dictionary formParams,
Dictionary fileParams, Dictionary pathParams, String[] authSettings)
{
var request = PrepareRequest(
path, method, queryParams, postBody, headerParams, formParams, fileParams, pathParams, authSettings);
return (Object)RestClient.Execute(request);
}
///
/// Makes the asynchronous HTTP request.
///
/// URL path.
/// HTTP method.
/// Query parameters.
/// HTTP body (POST request).
/// Header parameters.
/// Form parameters.
/// File parameters.
/// Path parameters.
/// Authentication settings.
/// The Task instance.
public async System.Threading.Tasks.Task
© 2015 - 2025 Weber Informatics LLC | Privacy Policy