157 lines
4.5 KiB
C#
157 lines
4.5 KiB
C#
using System.ComponentModel.DataAnnotations;
|
|
using AutoMapper.Mappers;
|
|
using RestSharp;
|
|
using xCommons.Constants;
|
|
using xCommons.Extensions;
|
|
using xModels.Dtos;
|
|
|
|
namespace xHttpService.Extensions
|
|
{
|
|
public static partial class RestSharpExtensions
|
|
{
|
|
/// <summary>
|
|
/// Attach an instance of XQuery to a RestRequest object
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="query"></param>
|
|
/// <returns></returns>
|
|
public static RestRequest AttachXQuery(
|
|
this RestRequest source,
|
|
XQuery query
|
|
)
|
|
{
|
|
//
|
|
if (query.IsNull())
|
|
{
|
|
return source;
|
|
}
|
|
|
|
//
|
|
// ContainsDetail ...
|
|
//
|
|
var containsDetailKey = System.Uri.EscapeDataString(nameof(query.ContainsDetail));
|
|
var containsDetailValue = query.ContainsDetail.AsHttpParamString();
|
|
source.AddQueryParameter(containsDetailKey, containsDetailValue);
|
|
|
|
//
|
|
// IsAscending ...
|
|
var isAscendingKey = System.Uri.EscapeDataString(nameof(query.IsAscending));
|
|
var isAscendingValue = query.IsAscending.AsHttpParamString();
|
|
source.AddQueryParameter(isAscendingKey, isAscendingValue);
|
|
|
|
//
|
|
// Filter ...
|
|
if (!query.Filter.IsNullOrEmpty())
|
|
{
|
|
//
|
|
var key = System.Uri.EscapeDataString(nameof(query.Filter));
|
|
var value = query.Filter.AsHttpParamString();
|
|
source.AddQueryParameter(key, value);
|
|
}
|
|
|
|
//
|
|
// SortBy ...
|
|
if (!query.SortBy.IsNullOrEmpty())
|
|
{
|
|
//
|
|
var key = System.Uri.EscapeDataString(nameof(query.SortBy));
|
|
var value = query.SortBy.AsHttpParamString();
|
|
source.AddQueryParameter(key, value);
|
|
}
|
|
|
|
//
|
|
// Page ...
|
|
if (query.Page > 0)
|
|
{
|
|
//
|
|
var key = System.Uri.EscapeDataString(nameof(query.Page));
|
|
var value = query.Page.AsHttpParamString();
|
|
source.AddQueryParameter(key, value);
|
|
}
|
|
|
|
//
|
|
// PageSize ...
|
|
if (query.PageSize > 0)
|
|
{
|
|
//
|
|
var key = System.Uri.EscapeDataString(nameof(query.PageSize));
|
|
var value = query.PageSize.AsHttpParamString();
|
|
source.AddQueryParameter(key, value);
|
|
}
|
|
|
|
//
|
|
return source;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add Dest User to Query Strings ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="destUser"></param>
|
|
/// <returns></returns>
|
|
public static RestRequest AttachDestUser(
|
|
this RestRequest source,
|
|
string destUser
|
|
)
|
|
{
|
|
//
|
|
if (destUser.IsNullOrEmpty())
|
|
{
|
|
return source;
|
|
}
|
|
|
|
//
|
|
var destUserKey = System.Uri.EscapeDataString(nameof(destUser));
|
|
var destUserValue = destUser.AsHttpParamString();
|
|
source.AddQueryParameter(destUserKey, destUserValue);
|
|
|
|
//
|
|
return source;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add XPoweredBy Header to specific RestRequest Headers ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="poweredByValue"></param>
|
|
/// <returns></returns>
|
|
public static RestRequest AddXPoweredBy(
|
|
this RestRequest source,
|
|
string poweredByValue
|
|
)
|
|
{
|
|
//
|
|
source.AddHeader(
|
|
XAuthorization.XPoweredBy,
|
|
poweredByValue
|
|
);
|
|
|
|
//
|
|
return source;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add XAccessToken Header to specific RestRequest Headers ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="poweredByValue"></param>
|
|
/// <returns></returns>
|
|
public static RestRequest AddXAccessToken(
|
|
this RestRequest source,
|
|
string token
|
|
)
|
|
{
|
|
//
|
|
token = $"{XAuthentication.BEARER} {token}";
|
|
|
|
//
|
|
source.AddHeader(
|
|
XAuthorization.Header,
|
|
token
|
|
);
|
|
|
|
//
|
|
return source;
|
|
}
|
|
}
|
|
} |