34 lines
1.1 KiB
C#
34 lines
1.1 KiB
C#
using Demo.Common.Dtos;
|
|
using Newtonsoft.Json.Linq;
|
|
using RestSharp;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using DataFormat = RestSharp.DataFormat;
|
|
|
|
namespace Demo.Common.Extensions
|
|
{
|
|
public static class RestClientExtension
|
|
{
|
|
public static async Task<TResult> PostAsync<TResult, TBody>(this RestClient client, TBody body) where TResult : class where TBody : class
|
|
{
|
|
RestRequest request = new RestRequest();
|
|
request.Method = Method.Post;
|
|
request.RequestFormat = DataFormat.Json;
|
|
request.AddOrUpdateHeader("Authorization", $"Bearer {Application.Current.Properties["token"]}");
|
|
request.AddJsonBody(body);
|
|
var result = await client.PostAsync<BaseApiResult<TResult>>(request);
|
|
if (result.code != 200)
|
|
{
|
|
return result.Data;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|