tesses-cms/Tesses.CMS.Client/Class1.cs

86 lines
2.6 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Net.Http;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Web;
using Newtonsoft.Json;
namespace Tesses.CMS.Client
{
public class TessesCMSClient
{
private static HttpClient CreateHttpClient()
{
HttpClientHandler httpClientHandler=new HttpClientHandler();
httpClientHandler.AllowAutoRedirect=true;
httpClientHandler.UseCookies=true;
HttpClient client=new HttpClient(httpClientHandler);
return client;
}
internal HttpClient client;
internal string rooturl;
public HttpClient Client => client;
public string RootUrl => $"{rooturl}/";
public TessesCMSClient(string url,HttpClient client)
{
this.client = client;
rooturl = url.TrimEnd('/');
}
public TessesCMSClient(HttpClient client) : this("https://tessesstudios.com/",client)
{
}
public TessesCMSClient(string url) : this(url,CreateHttpClient())
{
}
public TessesCMSClient() : this(CreateHttpClient())
{
}
public MovieClient Movies => new MovieClient(this);
public UserClient Users => new UserClient(this);
}
public class UserClient
{
TessesCMSClient client;
internal UserClient(TessesCMSClient client)
{
this.client = client;
}
public async Task LogoutAsync()
{
await client.client.GetStringAsync($"{client.rooturl}/logout");
}
public async Task<bool> LoginAsync(string email,string password)
{
Dictionary<string,string> us=new Dictionary<string, string>();
us.Add("email",email);
us.Add("password",password);
using(var res=await client.client.PostAsync($"{client.rooturl}/login", new FormUrlEncodedContent(us)))
return res.StatusCode != System.Net.HttpStatusCode.Unauthorized;
}
}
public class MovieClient
{
TessesCMSClient client;
internal MovieClient(TessesCMSClient client)
{
this.client = client;
}
public async IAsyncEnumerable<Movie> GetMoviesAsync(string user)
{
foreach(var item in JsonConvert.DeserializeObject<List<Movie>>(await client.client.GetStringAsync($"{client.rooturl}/api/v1/GetMovies?user={HttpUtility.UrlDecode(user)}")))
{
yield return item;
}
}
}
}