This commit is contained in:
2025-07-17 06:42:00 +03:30
parent 3f878d15ca
commit 6178c9d4e6
6 changed files with 455 additions and 127 deletions
+69 -61
View File
@@ -1,197 +1,205 @@
using xExceptions.Attributes;
namespace xIdentityService.Constants {
public enum XApiAccountEndpoint {
namespace xIdentityService.Constants
{
public enum XApiAccountEndpoint
{
//
#region Authentication ...
[StringValue ("Account/DiscoveryDocument")]
[StringValue("Account/DiscoveryDocument")]
DiscoveryDocument,
[StringValue ("Account/RequestScopeAccessToken")]
[StringValue("Account/RequestScopeAccessToken")]
RequestScopeAccessToken,
[StringValue ("Account/Authenticate")]
[StringValue("Account/Authenticate")]
Authenticate,
[StringValue ("Account/Login")]
[StringValue("Account/Login")]
Login,
[StringValue ("Account/RefreshTokens")]
[StringValue("Account/RefreshTokens")]
RefreshTokens,
[StringValue ("Account/ChangePassword")]
[StringValue("Account/ChangePassword")]
ChangePassword,
[StringValue ("Account/ResetPassword")]
[StringValue("Account/ResetPassword")]
ResetPassword,
#endregion
//
#region Proile ...
[StringValue ("Account/Profile/{ids}/GetNames")]
[StringValue("Account/Profile/{ids}/GetNames")]
GetNames,
[StringValue ("Account/Profile/GetNameIds")]
[StringValue("Account/Profile/GetNameIds")]
GetNameIds,
[StringValue ("Account/Profile/{userSelectByParam}")]
[StringValue("Account/Profile/{userSelectByParam}")]
GetProfile,
[StringValue ("Account/Profile/Query")]
[StringValue("Account/Profile/Query")]
QueryProfiles,
[StringValue ("Account/Profile/Avatars/Query/{userSelectByParam}")]
[StringValue("Account/Profile/Avatars/Query/{userSelectByParam}")]
QueryAvatars,
[StringValue ("Account/Profile/Update/{userSelectByParam}")]
[StringValue("Account/Profile/Update/{userSelectByParam}")]
UpdateProfile,
[StringValue ("Account/Profile/{userSelectByParam}")]
[StringValue("Account/Profile/{userSelectByParam}")]
FullUpdateProfile,
[StringValue ("Account/Profile/Avatar")]
[StringValue("Account/Profile/Avatar")]
AddAvatar,
[StringValue ("Account/Profile/Avatars")]
[StringValue("Account/Profile/Avatars")]
AddAvatars,
[StringValue ("Account/Profile/Avatars/{ids}")]
[StringValue("Account/Profile/Avatars/{ids}")]
RemoveAvatars,
[StringValue ("Account/Profile/Avatars/{id}/Set")]
[StringValue("Account/Profile/Avatars/{id}/Set")]
SetAvatar,
#endregion
//
#region Registration ...
[StringValue ("Account/CanRegisterUserName/{userName}")]
[StringValue("Account/CanRegisterUserName/{userName}")]
CanRegisterUserName,
[StringValue ("Account/CanRegisterMobileNumber/{mobileNumber}")]
[StringValue("Account/CanRegisterMobileNumber/{mobileNumber}")]
CanRegisterMobileNumber,
[StringValue ("Account/CanRegisterEmail/{email}")]
[StringValue("Account/CanRegisterEmail/{email}")]
CanRegisterEmail,
[StringValue ("Account/IsConfirmedEmail")]
[StringValue("Account/IsConfirmedEmail")]
IsConfirmedEmail,
[StringValue ("Account/IsConfirmedMobile")]
[StringValue("Account/IsConfirmedMobile")]
IsConfirmedMobile,
[StringValue ("Account/ConfirmRegistration")]
[StringValue("Account/ConfirmRegistration")]
ConfirmRegistration,
[StringValue ("Account/ConfirmMobileNumber")]
[StringValue("Account/ConfirmMobileNumber")]
ConfirmMobileNumber,
[StringValue ("Account/ConfirmEmailAddress")]
[StringValue("Account/ConfirmEmailAddress")]
ConfirmEmailAddress,
[StringValue ("Account/RequestConfirmMobile")]
[StringValue("Account/RequestConfirmMobile")]
RequestConfirmMobile,
[StringValue ("Account/RequestConfirmEmail")]
[StringValue("Account/RequestConfirmEmail")]
RequestConfirmEmail,
[StringValue ("Account/RequestResetPassword")]
[StringValue("Account/RequestResetPassword")]
RequestResetPassword,
[StringValue ("Account/RequestConfirmRegistration")]
[StringValue("Account/RequestConfirmRegistration")]
RequestConfirmRegistration,
[StringValue ("Account/InviteUser")]
[StringValue("Account/InviteUser")]
InviteUser,
[StringValue ("Account/RequestRegistration")]
[StringValue("Account/RequestRegistration")]
RequestRegistration,
[StringValue ("Account/AddAccountInfo")]
[StringValue("Account/AddAccountInfo")]
AddAccountInfo,
[StringValue ("Account/AttachProfileImage")]
[StringValue("Account/AttachProfileImage")]
AttachProfileImage,
[StringValue ("Account/FinishRegistration")]
[StringValue("Account/FinishRegistration")]
FinishRegistration,
#endregion
//
#region Friendship ...
[StringValue ("Account/Friendship/{destUser}/Follow")]
[StringValue("Account/Friendship/{destUser}/Follow")]
Follow,
[StringValue ("Account/Friendship/{destUser}/Cancel")]
[StringValue("Account/Friendship/{destUser}/Cancel")]
Cancel,
[StringValue ("Account/Friendship/{destUser}/UnFollowFollower")]
[StringValue("Account/Friendship/{destUser}/UnFollowFollower")]
UnFollowFollower,
[StringValue ("Account/Friendship/{destUser}/UnFollowFollowing")]
[StringValue("Account/Friendship/{destUser}/UnFollowFollowing")]
UnFollowFollowing,
[StringValue ("Account/Friendship/{destUser}/Block")]
[StringValue("Account/Friendship/{destUser}/Block")]
Block,
[StringValue ("Account/Friendship/{destUser}/UnBlock")]
[StringValue("Account/Friendship/{destUser}/UnBlock")]
UnBlock,
[StringValue ("Account/Friendship/{destUser}/AcceptRequest")]
[StringValue("Account/Friendship/{destUser}/AcceptRequest")]
AcceptRequest,
[StringValue ("Account/Friendship/{destUser}/RejectRequest")]
[StringValue("Account/Friendship/{destUser}/RejectRequest")]
RejectRequest,
[StringValue ("Account/Friendship/{destUser}/IsFollower")]
[StringValue("Account/Friendship/{destUser}/IsFollower")]
IsFollower,
[StringValue ("Account/Friendship/{destUser}/FollowerState")]
[StringValue("Account/Friendship/{destUser}/FollowerState")]
FollowerState,
[StringValue ("Account/Friendship/{destUser}/Follower")]
[StringValue("Account/Friendship/{destUser}/Follower")]
Follower,
[StringValue ("Account/Friendship/Followers")]
[StringValue("Account/Friendship/Followers")]
Followers,
[StringValue ("Account/Friendship/AllFollowers")]
[StringValue("Account/Friendship/AllFollowers")]
AllFollowers,
[StringValue ("Account/Friendship/{destUser}/IsFollowing")]
[StringValue("Account/Friendship/{destUser}/IsFollowing")]
IsFollowing,
[StringValue ("Account/Friendship/{destUser}/FollowingState")]
[StringValue("Account/Friendship/{destUser}/FollowingState")]
FollowingState,
[StringValue ("Account/Friendship/{destUser}/Following")]
[StringValue("Account/Friendship/{destUser}/Following")]
Following,
[StringValue ("Account/Friendship/Followings")]
[StringValue("Account/Friendship/Followings")]
Followings,
[StringValue ("Account/Friendship/AllFollowings")]
[StringValue("Account/Friendship/AllFollowings")]
AllFollowings,
[StringValue ("Account/Friendship/FollowingsList")]
[StringValue("Account/Friendship/FollowingsList")]
FollowingsList,
[StringValue ("Account/Friendship/FollowersList")]
[StringValue("Account/Friendship/FollowersList")]
FollowersList,
[StringValue ("Account/Friendship/{destUser}/FriendshipInfo")]
[StringValue("Account/Friendship/{destUser}/FriendshipInfo")]
FriendshipInfo,
#endregion
//
#region Admin ...
[StringValue ("Account/Ban")]
[StringValue("Account/Ban")]
Ban,
[StringValue ("Account/UnBan")]
[StringValue("Account/UnBan")]
UnBan,
[StringValue ("Account/IsBanned/{userSelectByParam}")]
[StringValue("Account/IsBanned/{userSelectByParam}")]
IsBanned,
#endregion
//
#region Open Actions ...
[StringValue("Account/GetUserInfo")]
GetUserInfo,
#endregion
}
}
+3
View File
@@ -50,6 +50,9 @@ namespace xIdentityService.Constants {
[StringValue ("actionToken")]
XActionToken,
[StringValue ("request")]
XRequest,
[StringValue ("scope")]
XScope,
}
+80 -64
View File
@@ -10,15 +10,18 @@ using xIdentityModels.Models;
using xIdentityModels.Navigations;
using xModels.Dtos;
namespace xIdentityService.Interfaces {
public partial interface IXIdentityProvider {
namespace xIdentityService.Interfaces
{
public partial interface IXIdentityProvider
{
//
string BaseUrl { get; }
string RevisionSecretKey { get; }
IXSecurityProvider SecurityProvider { get; }
//
#region Tools ...
string GetUserSelectByParam (
string GetUserSelectByParam(
XActionRequest model,
bool forceNotNull = true,
ICollection<XUserSelectBy> excludes = null
@@ -27,7 +30,7 @@ namespace xIdentityService.Interfaces {
//
#region Tools ...
Task<T> RunRestRequest<T> (
Task<T> RunRestRequest<T>(
RestClient client,
RestRequest request,
XHttpMethod method,
@@ -37,15 +40,15 @@ namespace xIdentityService.Interfaces {
//
#region Authentication ...
Task<DiscoveryDocumentResponse> RequestDiscoveryDocument ();
Task<XTokenResponse> RequestScopeAccessToken (string scope);
Task<TokenIntrospectionResponse> Introspection (XTokenResponse model);
Task<XLoginResponse> Login (XLoginRequest model);
Task Logout (XTokenResponse model);
Task<XTokenResponse> Authenticate (XLoginRequest model);
Task<XTokenResponse> RefreshTokens (XTokenResponse model);
Task<DiscoveryDocumentResponse> RequestDiscoveryDocument();
Task<XTokenResponse> RequestScopeAccessToken(string scope);
Task<TokenIntrospectionResponse> Introspection(XTokenResponse model);
Task<XLoginResponse> Login(XLoginRequest model);
Task Logout(XTokenResponse model);
Task<XTokenResponse> Authenticate(XLoginRequest model);
Task<XTokenResponse> RefreshTokens(XTokenResponse model);
Task ChangePassword (
Task ChangePassword(
XTokenResponse tokens,
XActionRequest model
);
@@ -53,67 +56,67 @@ namespace xIdentityService.Interfaces {
//
#region Profile ...
Task<IEnumerable<string>> GetUserNames (
Task<IEnumerable<string>> GetUserNames(
XTokenResponse tokens,
string userIds
);
Task<IEnumerable<XUserNameIdResponse>> GetUserNameIds (
Task<IEnumerable<XUserNameIdResponse>> GetUserNameIds(
XTokenResponse tokens,
XUserNameIdRequest model
);
Task<XUserProfileDto> GetUserProfile (
Task<XUserProfileDto> GetUserProfile(
XTokenResponse tokens,
string userSelectByParam
);
Task<XQueryResult<XUserProfileDto>> QueryUsers (
Task<XQueryResult<XUserProfileDto>> QueryUsers(
XTokenResponse tokens,
XQuery query
);
Task<XQueryResult<XUserProfileDto>> QueryInRoleUsers (
Task<XQueryResult<XUserProfileDto>> QueryInRoleUsers(
XTokenResponse tokens,
string role,
XQuery query,
bool forceRole = false
);
Task<XQueryResult<XProfileImage>> QueryAvatars (
Task<XQueryResult<XProfileImage>> QueryAvatars(
XTokenResponse tokens,
string userSelectByParam,
XQuery query
);
Task<XUserProfileDto> ProfileUpdateAsync (
Task<XUserProfileDto> ProfileUpdateAsync(
XTokenResponse tokens,
string userSelectByParam,
XProfileUpdateRequest model
);
Task<XUserProfileDto> FullProfileUpdateAsync (
Task<XUserProfileDto> FullProfileUpdateAsync(
XTokenResponse tokens,
string userSelectByParam,
XProfileUpdateRequest request
);
Task<XUserProfileDto> AddAvatar (
Task<XUserProfileDto> AddAvatar(
XTokenResponse tokens,
IFormFile file
);
Task<XUserProfileDto> AddAvatars (
Task<XUserProfileDto> AddAvatars(
XTokenResponse tokens,
IFormFileCollection files
);
Task<XUserProfileDto> SetAvatar (
Task<XUserProfileDto> SetAvatar(
XTokenResponse tokens,
int avatarId
);
Task<XUserProfileDto> RemoveAvatars (
Task<XUserProfileDto> RemoveAvatars(
XTokenResponse tokens,
string avatarIds
);
@@ -121,142 +124,142 @@ namespace xIdentityService.Interfaces {
//
#region Registration ...
Task<bool> CanRegister (string userSelectByParam);
Task<bool> CanRegister(string userSelectByParam);
Task<XActionResponse> InviteUser (
Task<XActionResponse> InviteUser(
XTokenResponse tokens,
XActionRequest model
);
Task<XActionResponse> RequestRegistration (
Task<XActionResponse> RequestRegistration(
XActionRequest model
);
Task<XActionResponse> AddAccountInfo (
Task<XActionResponse> AddAccountInfo(
XActionRequest model
);
Task<XActionResponse> AttachProfileImage (
Task<XActionResponse> AttachProfileImage(
string actionToken,
IFormFile file
);
Task FinishRegistration (XActionRequest model);
Task FinishRegistration(XActionRequest model);
Task<bool> IsConfirmedEmail (
Task<bool> IsConfirmedEmail(
XTokenResponse tokens,
string userSelectByParam
);
Task<bool> IsConfirmedMobile (
Task<bool> IsConfirmedMobile(
XTokenResponse tokens,
string userSelectByParam
);
Task RequestConfirmRegistration (XActionRequest model);
Task RequestConfirmRegistration(XActionRequest model);
Task ConfirmRegistration (
Task ConfirmRegistration(
XActionRequest model
);
Task<XActionResponse> RequestConfirmMobile (XActionRequest model);
Task<XActionResponse> RequestConfirmMobile(XActionRequest model);
Task<XActionResponse> ConfirmMobileNumber (XActionRequest model);
Task<XActionResponse> ConfirmMobileNumber(XActionRequest model);
Task<XActionResponse> RequestConfirmEmail (XActionRequest model);
Task<XActionResponse> RequestConfirmEmail(XActionRequest model);
Task<XActionResponse> ConfirmEmailAddress (XActionRequest model);
Task<XActionResponse> ConfirmEmailAddress(XActionRequest model);
Task<XActionResponse> RequestResetPassword (XActionRequest model);
Task<XActionResponse> RequestResetPassword(XActionRequest model);
Task ResetPassword (XActionRequest model);
Task ResetPassword(XActionRequest model);
#endregion
//
#region Friendship ...
Task<XFriendshipFollowing> Follow (
Task<XFriendshipFollowing> Follow(
XTokenResponse tokens,
string destUser
);
Task<bool> Cancel (
Task<bool> Cancel(
XTokenResponse tokens,
string destUser
);
Task UnFollowFollower (
Task UnFollowFollower(
XTokenResponse tokens,
string destUser
);
Task UnFollowFollowing (
Task UnFollowFollowing(
XTokenResponse tokens,
string destUser
);
Task<XFriendshipFollowing> Block (
Task<XFriendshipFollowing> Block(
XTokenResponse tokens,
string destUser
);
Task<XFriendshipFollowing> UnBlock (
Task<XFriendshipFollowing> UnBlock(
XTokenResponse tokens,
string destUser
);
Task<XFriendshipFollower> AcceptRequest (
Task<XFriendshipFollower> AcceptRequest(
XTokenResponse tokens,
string destUser
);
Task<XFriendshipFollower> RejectRequest (
Task<XFriendshipFollower> RejectRequest(
XTokenResponse tokens,
string destUser
);
Task<bool> IsFollower (
Task<bool> IsFollower(
XTokenResponse tokens,
string destUser
);
Task<XFriendshipFollower> GetFollower (
Task<XFriendshipFollower> GetFollower(
XTokenResponse tokens,
string destUser
);
Task<XFriendshipState> GetFollowerState (
Task<XFriendshipState> GetFollowerState(
XTokenResponse tokens,
string destUser
);
Task<IEnumerable<XFriendshipFollower>> GetFollowers (XTokenResponse tokens);
Task<IEnumerable<XFriendshipFollower>> GetFollowers(XTokenResponse tokens);
Task<IEnumerable<XFriendshipFollower>> GetAllFollowers (XTokenResponse tokens);
Task<IEnumerable<XFriendshipFollower>> GetAllFollowers(XTokenResponse tokens);
Task<bool> IsFollowing (
Task<bool> IsFollowing(
XTokenResponse tokens,
string destUser
);
Task<XFriendshipFollowing> GetFollowing (
Task<XFriendshipFollowing> GetFollowing(
XTokenResponse tokens,
string destUser
);
Task<XFriendshipState> GetFollowingState (
Task<XFriendshipState> GetFollowingState(
XTokenResponse tokens,
string destUser
);
Task<IEnumerable<XFriendshipFollowing>> GetFollowings (XTokenResponse tokens);
Task<IEnumerable<XFriendshipFollowing>> GetFollowings(XTokenResponse tokens);
Task<IEnumerable<XFriendshipFollowing>> GetAllFollowings (XTokenResponse tokens);
Task<IEnumerable<XFriendshipFollowing>> GetAllFollowings(XTokenResponse tokens);
Task<IEnumerable<string>> GetFollowingList (XTokenResponse tokens);
Task<IEnumerable<string>> GetFollowingList(XTokenResponse tokens);
Task<IEnumerable<string>> GetFollowersList (XTokenResponse tokens);
Task<IEnumerable<string>> GetFollowersList(XTokenResponse tokens);
Task<XFriendshipInfoDto> GetFriendshipInfo (
Task<XFriendshipInfoDto> GetFriendshipInfo(
XTokenResponse tokens,
string destUser
);
@@ -264,20 +267,33 @@ namespace xIdentityService.Interfaces {
//
#region Admin ...
Task<IEnumerable<string>> Ban (
Task<IEnumerable<string>> Ban(
XTokenResponse tokens,
XUserNameIdRequest model
);
Task<IEnumerable<string>> UnBan (
Task<IEnumerable<string>> UnBan(
XTokenResponse tokens,
XUserNameIdRequest model
);
Task<bool> IsBanned (
Task<bool> IsBanned(
XTokenResponse tokens,
string userSelectByParam
);
#endregion
//
#region Open Actions ...
Task<XOpenActionUserInfoDto> GetUserInfo(
XDeviceDto device,
string userSelectByParam
);
Task<XOpenActionUserInfoDto> GetUserInfo(XOpenActionInnerDto dto);
Task<XOpenActionUserInfoDto> GetUserInfo(
string token,
string request
);
#endregion
}
}
+10
View File
@@ -0,0 +1,10 @@
using RestSharp;
namespace xIdentityService.Models
{
public class XBaseRestResponse<T>
{
public T Model { get; set; }
public IRestResponse Response { get; set; }
}
}
+144
View File
@@ -0,0 +1,144 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MongoDB.Bson;
using xCommons.Constants;
using xCommons.Extensions;
using xExceptions.Constants;
using xHttpService.Constants;
using xIdentityModels.Dtos;
using xIdentityService.Constants;
using xModels.Dtos;
using xModels.Extensions;
namespace xIdentityService.Providers
{
public partial class XIdentityProvider
{
public async Task<XOpenActionUserInfoDto> GetUserInfo(
XDeviceDto device,
string userSelectByParam
)
{
//
// Validate Args ...
validationProvider.NotNull(device);
validationProvider.NotEmpty(userSelectByParam);
//
// Prepare Open Action Model ...
var opaModel = new XOpenActionRequestDto
{
Device = device,
Payload = userSelectByParam,
Action = XOpenActions.GetUserInfo,
Timestamp = DateTime.UtcNow.AddMinutes(5)
};
if (!opaModel.Validate())
{
XException.InvalidArgs.Throw();
}
//
// Fill Checksum Filed ...
var token = opaModel.ToOpenActionToken();
opaModel.Checksum = token;
//
// Prepare Request ...
var opaJson = opaModel.ToJson();
var requestString = SecurityProvider.Encrypt(opaJson);
//
return await GetUserInfo(
token: token,
request: requestString
);
}
public async Task<XOpenActionUserInfoDto> GetUserInfo(XOpenActionInnerDto dto)
{
//
return await GetUserInfo(
token: dto.Token,
request: dto.Payload
);
}
public async Task<XOpenActionUserInfoDto> GetUserInfo(
string token,
string request
)
{
//
// Validate Args ...
validationProvider.NotEmpty(token);
validationProvider.NotEmpty(request);
//
// Retrieve Rest Client ...
var client = GetRestClient();
//
// Prepare Rest Request ...
var requestU = GetRestRequet(
XApiAccountEndpoint.GetUserInfo,
@params: new Dictionary<string, string>
{
{ XParam.XRequest.GetStringValue(), request }
},
headers: new Dictionary<string, string>
{
{ XAuthentication.TOKEN, token}
}
);
//
var response = await RunRestRequestByResponse<string>(
client,
requestU,
XHttpMethod.GET
);
//
// Now we Have to Validate the recieved response ...
//
// Read token from Headers ...
//
var responseToken = response.Response.Headers.First(h => h.Name == XAuthentication.TOKEN).ToString();
if (responseToken.IsNullOrEmpty())
{
XException.ActionFailed.Throw();
}
//
var responseJson = SecurityProvider.Decrypt(response.Model);
if (responseJson.IsNullOrEmpty())
{
XException.ActionFailed.Throw();
}
//
// Retrieve Model and Validate it ...
var responseModel = responseJson.FromJSON<XOpenActionRequestDto>();
if (responseModel.IsNull() ||
responseModel.Timestamp.IsExPired() ||
responseModel.Payload.IsNullOrEmpty() ||
// !responseModel.Device.IsSameAs(device) ||
responseToken != responseModel.Checksum)
{
XException.ActionFailed.Throw();
}
//
// Try to Retrieve Model ...
var result = responseModel.Payload.FromJSON<XOpenActionUserInfoDto>();
//
return result;
}
}
}
+149 -2
View File
@@ -21,6 +21,7 @@ using xIdentityModels.Models;
using xIdentityService.Configuration;
using xIdentityService.Constants;
using xIdentityService.Interfaces;
using xIdentityService.Models;
namespace xIdentityService.Providers {
public partial class XIdentityProvider : IXIdentityProvider {
@@ -30,16 +31,18 @@ namespace xIdentityService.Providers {
private readonly XValidationProvider validationProvider;
private readonly XIdentityServiceConfiguration identityConfiguration;
public ILogger<XIdentityProvider> Logger { get; }
public IXSecurityProvider SecurityProvider { get; }
public string BaseUrl { get; }
public string RevisionSecretKey { get; }
public bool ReadResponseAsString { get; set; }
public ILogger<XIdentityProvider> Logger { get; }
public XIdentityProvider (
IXTokenProvider tokenProvider,
IHostingEnvironment environment,
ILogger<XIdentityProvider> logger,
IXSecurityProvider securityProvider,
XIdentityServiceConfiguration identityConfiguration,
XValidationProvider validationProvider
) {
@@ -50,6 +53,7 @@ namespace xIdentityService.Providers {
//
this.environment = environment;
Logger = logger;
SecurityProvider = securityProvider;
//
this.identityConfiguration = identityConfiguration;
@@ -345,6 +349,149 @@ namespace xIdentityService.Providers {
//
return response;
}
public async Task<XBaseRestResponse<T>> RunRestRequestByResponse<T> (
RestClient client,
RestRequest request,
XHttpMethod method,
bool supportRefreshingTokens = true
) {
//
var response = await Task.Run (async () => {
//
IRestResponse resp = null;
//
// Call Endpoint Service using Specified Method ...
switch (method) {
//
// Get ...
case XHttpMethod.GET:
resp = client.Get (request);
break;
//
// Put ...
case XHttpMethod.PUT:
resp = client.Put (request);
break;
//
// Post ...
case XHttpMethod.POST:
resp = client.Post (request);
break;
//
// Delete ...
case XHttpMethod.DELETE:
resp = client.Delete (request);
break;
}
//
if (!resp.IsSuccessful) {
//
// Log Exception ...
Logger.LogError ($"Error: {resp.ErrorMessage}");
Logger.LogError ($"Exception: {resp.ErrorException.ToJSON()}");
if (!resp.ErrorMessage.IsNullOrEmpty () &&
resp.ErrorMessage.Contains ("timed out")) {
XException.Timeout.Throw ();
}
//
// TODO: Check UnAuthorized Status here for
// Refresh Tokens or Issue Correct Exception ...
if (supportRefreshingTokens &&
resp.StatusCode == System.Net.HttpStatusCode.Unauthorized) {
//
var isRefreshed = request.Parameters.Any (rp =>
rp.Name == XParam.XIsRefreshed.GetStringValue () &&
rp.Value.ToString ().ToNormalString () == true.ToJSON ());
//
Logger.LogInformation ($"isRefreshed: {isRefreshed}");
//
// Get Access Token ...
XTokenResponse tokens = null;
var accessToken = request.Parameters.FirstOrDefault (rp =>
rp.Name.ToNormalString () == XAuthorization.Header.ToNormalString ()).Value.ToString ();
if (!accessToken.IsNullOrEmpty ()) {
//
accessToken = accessToken.Replace (XAuthorization.TokenIdentifier, "");
//
if (!accessToken.IsNullOrEmpty ()) {
tokens = await tokenProvider.RetrieveToken (accessToken);
}
}
//
// Refresh Token and then re do request ...
if (!isRefreshed &&
!accessToken.IsNullOrEmpty () &&
!tokens.IsNull () &&
tokens.IsRefreshable ()) {
//
request.AddHeader (XParam.XIsRefreshed.GetStringValue (), true.ToJSON ());
//
// Refreshe Tokens ...
var refreshedTokens = await RefreshTokens (tokens);
//
// Renew Rest Client with Refreshed Tokens ...
var timeout = client.Timeout;
client = GetRestClient (refreshedTokens);
client.Timeout = Timeout.Infinite;
//
// Do Request Again using renew Client ...
return await RunRestRequestByResponse<T> (
client: client,
request: request,
method: method
);
}
}
//
var ex = resp.Content.ToXError ();
if (!ex.IsNull ()) {
throw ex.ToException ();
}
//
// UnAuthorized ...
if (resp.StatusCode == System.Net.HttpStatusCode.Unauthorized) {
XException.NotAuthorized.Throw ();
}
//
// NotFound ...
if (resp.StatusCode == System.Net.HttpStatusCode.NotFound) {
XException.NotFound.Throw ();
}
//
XException.ActionFailed.Throw ();
}
//
var result = new XBaseRestResponse<T>
{
Response = resp,
Model = resp.Content.FromJSON<T> ()
};
//
return result;
});
//
return response;
}
/// <summary>
/// Get User SelectBy Param