2020-09-17 10:23:26 +08:00
using Newtonsoft.Json ;
using OnlineUserPool.Model ;
using System.Collections.Generic ;
2020-10-27 10:20:47 +08:00
using System.Linq ;
2020-09-17 10:23:26 +08:00
using System.Net.Http ;
using System.Text ;
using System.Threading.Tasks ;
namespace OnlineUserPool.Unility
{
public class BaseApi
{
internal static System . Net . Http . HttpClient httpClient ;
static BaseApi ( )
{
httpClient = new System . Net . Http . HttpClient ( ) ;
2020-11-23 17:07:40 +08:00
httpClient . DefaultRequestHeaders . UserAgent . Add ( new System . Net . Http . Headers . ProductInfoHeaderValue ( "OnlineUserPool" , "1.0.0" ) ) ;
2020-09-17 10:23:26 +08:00
}
protected static async Task < T > PostAsync < T > ( string url , object data )
{
StringContent stringContent ;
if ( data ! = null )
{
stringContent = new StringContent ( JsonConvert . SerializeObject ( data ) , Encoding . UTF8 , "application/json" ) ;
}
else
{
stringContent = new StringContent ( "" , Encoding . UTF8 ) ;
}
var response = await httpClient . PostAsync ( ConfigHelp . Host + url , stringContent ) . ConfigureAwait ( false ) ;
var result = await response . Content . ReadAsStringAsync ( ) ;
return JsonConvert . DeserializeObject < T > ( result ) ;
}
protected static async Task < T > GetAsync < T > ( string url )
{
var response = await httpClient . GetAsync ( ConfigHelp . Host + url , HttpCompletionOption . ResponseContentRead ) . ConfigureAwait ( false ) ;
var result = await response . Content . ReadAsStringAsync ( ) ;
return JsonConvert . DeserializeObject < T > ( result ) ;
}
}
public class WebService : BaseApi
{
/// <summary>
/// 从服务端获取记录信息
/// </summary>
/// <returns></returns>
2020-11-23 17:07:40 +08:00
public static List < MapRecordRanking > GetRecordFileFromServer ( List < long > id )
2020-09-17 10:23:26 +08:00
{
2020-10-27 10:20:47 +08:00
if ( ! id . Any ( ) )
{
return new List < MapRecordRanking > ( ) ;
}
return PostAsync < JsonResult < List < MapRecordRanking > > > ( "MapRecord/GetRandomList" , id ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) . data ;
2020-09-17 10:23:26 +08:00
}
2020-10-30 17:39:19 +08:00
public static List < MapRouteAndUserQueryVM > GetMapRouteRandomRecord ( int top , IEnumerable < int > routeIds )
2020-09-17 10:23:26 +08:00
{
2020-10-27 10:20:47 +08:00
var routeIdsStr = "" ;
if ( routeIds ! = null )
{
routeIdsStr = string . Join ( "," , routeIds ) ;
}
return GetAsync < JsonResult < List < MapRouteAndUserQueryVM > > > ( $"MapRecord/GetRandomRankingUserRecord?top={ top }&routeIds={ routeIdsStr }" ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) . data ;
2020-09-17 10:23:26 +08:00
}
}
}