Skip to content

Commit

Permalink
9.0.000.00.1162
Browse files Browse the repository at this point in the history
  • Loading branch information
maksimlysenko committed Jul 5, 2018
1 parent 890b212 commit 64b981a
Show file tree
Hide file tree
Showing 17 changed files with 466 additions and 721 deletions.
82 changes: 47 additions & 35 deletions src/Genesys.Internal.Authentication/Api/AuthenticationApi.cs

Large diffs are not rendered by default.

126 changes: 39 additions & 87 deletions src/Genesys.Internal.Authentication/Client/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ public partial class ApiClient

/// <summary>
/// Initializes a new instance of the <see cref="ApiClient" /> class
/// with default configuration.
/// with default configuration and base path (https://your-server.com/auth/v3).
/// </summary>
public ApiClient()
{
Configuration = Genesys.Internal.Authentication.Client.Configuration.Default;
Configuration = Configuration.Default;
RestClient = new RestClient("https://your-server.com/auth/v3");
}

Expand All @@ -61,11 +61,14 @@ public ApiClient()
/// with default base path (https://your-server.com/auth/v3).
/// </summary>
/// <param name="config">An instance of Configuration.</param>
public ApiClient(Configuration config)
public ApiClient(Configuration config = null)
{
Configuration = config ?? Genesys.Internal.Authentication.Client.Configuration.Default;
if (config == null)
Configuration = Configuration.Default;
else
Configuration = config;

RestClient = new RestClient(Configuration.BasePath);
RestClient = new RestClient("https://your-server.com/auth/v3");
}

/// <summary>
Expand All @@ -79,7 +82,7 @@ public ApiClient(String basePath = "https://your-server.com/auth/v3")
throw new ArgumentException("basePath cannot be empty");

RestClient = new RestClient(basePath);
Configuration = Client.Configuration.Default;
Configuration = Configuration.Default;
}

/// <summary>
Expand All @@ -90,15 +93,10 @@ public ApiClient(String basePath = "https://your-server.com/auth/v3")
public static ApiClient Default;

/// <summary>
/// Gets or sets an instance of the IReadableConfiguration.
/// Gets or sets the Configuration.
/// </summary>
/// <value>An instance of the IReadableConfiguration.</value>
/// <remarks>
/// <see cref="IReadableConfiguration"/> helps us to avoid modifying possibly global
/// configuration values from within a given client. It does not guarantee thread-safety
/// of the <see cref="Configuration"/> instance in any way.
/// </remarks>
public IReadableConfiguration Configuration { get; set; }
/// <value>An instance of the Configuration.</value>
public Configuration Configuration { get; set; }

/// <summary>
/// Gets or sets the RestClient.
Expand All @@ -108,7 +106,7 @@ public ApiClient(String basePath = "https://your-server.com/auth/v3")

// Creates and sets up a RestRequest prior to a call.
private RestRequest PrepareRequest(
String path, RestSharp.Method method, List<KeyValuePair<String, String>> queryParams, Object postBody,
String path, RestSharp.Method method, Dictionary<String, String> queryParams, Object postBody,
Dictionary<String, String> headerParams, Dictionary<String, String> formParams,
Dictionary<String, FileParameter> fileParams, Dictionary<String, String> pathParams,
String contentType)
Expand Down Expand Up @@ -139,7 +137,14 @@ private RestRequest PrepareRequest(

if (postBody != null) // http body (model or byte[]) parameter
{
request.AddParameter(contentType, postBody, ParameterType.RequestBody);
if (postBody.GetType() == typeof(String))
{
request.AddParameter("application/json", postBody, ParameterType.RequestBody);
}
else if (postBody.GetType() == typeof(byte[]))
{
request.AddParameter(contentType, postBody, ParameterType.RequestBody);
}
}

return request;
Expand All @@ -159,7 +164,7 @@ private RestRequest PrepareRequest(
/// <param name="contentType">Content Type of the request</param>
/// <returns>Object</returns>
public Object CallApi(
String path, RestSharp.Method method, List<KeyValuePair<String, String>> queryParams, Object postBody,
String path, RestSharp.Method method, Dictionary<String, String> queryParams, Object postBody,
Dictionary<String, String> headerParams, Dictionary<String, String> formParams,
Dictionary<String, FileParameter> fileParams, Dictionary<String, String> pathParams,
String contentType)
Expand All @@ -169,7 +174,6 @@ public Object CallApi(
pathParams, contentType);

// set timeout

RestClient.Timeout = Configuration.Timeout;
// set user agent
RestClient.UserAgent = Configuration.UserAgent;
Expand All @@ -194,7 +198,7 @@ public Object CallApi(
/// <param name="contentType">Content type.</param>
/// <returns>The Task instance.</returns>
public async System.Threading.Tasks.Task<Object> CallApiAsync(
String path, RestSharp.Method method, List<KeyValuePair<String, String>> queryParams, Object postBody,
String path, RestSharp.Method method, Dictionary<String, String> queryParams, Object postBody,
Dictionary<String, String> headerParams, Dictionary<String, String> formParams,
Dictionary<String, FileParameter> fileParams, Dictionary<String, String> pathParams,
String contentType)
Expand Down Expand Up @@ -282,7 +286,6 @@ public object Deserialize(IRestResponse response, Type type)
return response.RawBytes;
}

// TODO: ? if (type.IsAssignableFrom(typeof(Stream)))
if (type == typeof(Stream))
{
if (headers != null)
Expand Down Expand Up @@ -344,39 +347,20 @@ public String Serialize(object obj)
}
}

/// <summary>
///Check if the given MIME is a JSON MIME.
///JSON MIME examples:
/// application/json
/// application/json; charset=UTF8
/// APPLICATION/JSON
/// application/vnd.company+json
/// </summary>
/// <param name="mime">MIME</param>
/// <returns>Returns True if MIME type is json.</returns>
public bool IsJsonMime(String mime)
{
var jsonRegex = new Regex("(?i)^(application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(;.*)?$");
return mime != null && (jsonRegex.IsMatch(mime) || mime.Equals("application/json-patch+json"));
}

/// <summary>
/// Select the Content-Type header's value from the given content-type array:
/// if JSON type exists in the given array, use it;
/// if JSON exists in the given array, use it;
/// otherwise use the first one defined in 'consumes'
/// </summary>
/// <param name="contentTypes">The Content-Type array to select from.</param>
/// <returns>The Content-Type header to use.</returns>
public String SelectHeaderContentType(String[] contentTypes)
{
if (contentTypes.Length == 0)
return "application/json";
return null;

foreach (var contentType in contentTypes)
{
if (IsJsonMime(contentType.ToLower()))
return contentType;
}
if (contentTypes.Contains("application/json", StringComparer.OrdinalIgnoreCase))
return "application/json";

return contentTypes[0]; // use the first content type specified in 'consumes'
}
Expand Down Expand Up @@ -411,29 +395,31 @@ public static string Base64Encode(string text)

/// <summary>
/// Dynamically cast the object into target type.
/// Ref: http://stackoverflow.com/questions/4925718/c-dynamic-runtime-cast
/// </summary>
/// <param name="fromObject">Object to be casted</param>
/// <param name="toObject">Target type</param>
/// <param name="source">Object to be casted</param>
/// <param name="dest">Target type</param>
/// <returns>Casted object</returns>
public static dynamic ConvertType(dynamic fromObject, Type toObject)
public static dynamic ConvertType(dynamic source, Type dest)
{
return Convert.ChangeType(fromObject, toObject);
return Convert.ChangeType(source, dest);
}

/// <summary>
/// Convert stream to byte array
/// Credit/Ref: http://stackoverflow.com/a/221941/677735
/// </summary>
/// <param name="inputStream">Input stream to be converted</param>
/// <param name="input">Input stream to be converted</param>
/// <returns>Byte array</returns>
public static byte[] ReadAsBytes(Stream inputStream)
public static byte[] ReadAsBytes(Stream input)
{
byte[] buf = new byte[16*1024];
byte[] buffer = new byte[16*1024];
using (MemoryStream ms = new MemoryStream())
{
int count;
while ((count = inputStream.Read(buf, 0, buf.Length)) > 0)
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buf, 0, count);
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
Expand Down Expand Up @@ -492,39 +478,5 @@ public static string SanitizeFilename(string filename)
return filename;
}
}

/// <summary>
/// Convert params to key/value pairs.
/// Use collectionFormat to properly format lists and collections.
/// </summary>
/// <param name="name">Key name.</param>
/// <param name="value">Value object.</param>
/// <returns>A list of KeyValuePairs</returns>
public IEnumerable<KeyValuePair<string, string>> ParameterToKeyValuePairs(string collectionFormat, string name, object value)
{
var parameters = new List<KeyValuePair<string, string>>();

if (IsCollection(value) && collectionFormat == "multi")
{
var valueCollection = value as IEnumerable;
parameters.AddRange(from object item in valueCollection select new KeyValuePair<string, string>(name, ParameterToString(item)));
}
else
{
parameters.Add(new KeyValuePair<string, string>(name, ParameterToString(value)));
}

return parameters;
}

/// <summary>
/// Check if generic object is a collection.
/// </summary>
/// <param name="value"></param>
/// <returns>True if object is a collection type</returns>
private static bool IsCollection(object value)
{
return value is IList || value is ICollection;
}
}
}
Loading

0 comments on commit 64b981a

Please sign in to comment.