several more methods
This commit is contained in:
parent
290092df5a
commit
1480efdd82
15
Program.cs
15
Program.cs
@ -9,8 +9,21 @@ namespace ttrss_co_client
|
|||||||
var conf = Configure();
|
var conf = Configure();
|
||||||
var ttrssClient = new ttrss.ApiClient(conf.BaseURI);
|
var ttrssClient = new ttrss.ApiClient(conf.BaseURI);
|
||||||
await ttrssClient.Login(conf.Username, conf.Password);
|
await ttrssClient.Login(conf.Username, conf.Password);
|
||||||
|
|
||||||
|
var loggedin = await ttrssClient.IsLoggedIn();
|
||||||
|
Console.WriteLine($"logged in: {loggedin}");
|
||||||
|
|
||||||
var apiLevel = await ttrssClient.GetApiLevel();
|
var apiLevel = await ttrssClient.GetApiLevel();
|
||||||
Console.WriteLine(apiLevel);
|
Console.WriteLine($"api level: {apiLevel}");
|
||||||
|
|
||||||
|
var unread = await ttrssClient.GetUnread();
|
||||||
|
Console.WriteLine($"{unread} unread article{(unread == 1 ? "" : "s")}");
|
||||||
|
|
||||||
|
var loggedout = await ttrssClient.Logout();
|
||||||
|
Console.WriteLine($"logged out: {loggedout}");
|
||||||
|
|
||||||
|
loggedin = await ttrssClient.IsLoggedIn();
|
||||||
|
Console.WriteLine($"logged in: {loggedin}");
|
||||||
}
|
}
|
||||||
static Configuration Configure(string configurationPath = "appsettings.json")
|
static Configuration Configure(string configurationPath = "appsettings.json")
|
||||||
{
|
{
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System.Net.Http.Json;
|
using System.Net.Http.Json;
|
||||||
|
|
||||||
@ -11,7 +12,7 @@ namespace ttrss_co_client.ttrss
|
|||||||
public Uri BaseURI { get; private set; }
|
public Uri BaseURI { get; private set; }
|
||||||
private HttpClient httpClient { get; set; }
|
private HttpClient httpClient { get; set; }
|
||||||
private string SessionId { get; set; } = null;
|
private string SessionId { get; set; } = null;
|
||||||
private int api_level{get;set;}
|
private int api_level { get; set; }
|
||||||
|
|
||||||
public ApiClient(Uri baseUri)
|
public ApiClient(Uri baseUri)
|
||||||
{
|
{
|
||||||
@ -45,36 +46,26 @@ namespace ttrss_co_client.ttrss
|
|||||||
public async Task<int> GetApiLevel()
|
public async Task<int> GetApiLevel()
|
||||||
{
|
{
|
||||||
assertInitialized();
|
assertInitialized();
|
||||||
|
return await oneValueGet<int>("getApiLevel", "level");
|
||||||
var json =JsonContent.Create(new
|
|
||||||
{
|
|
||||||
op = "getApiLevel",
|
|
||||||
sid = this.SessionId
|
|
||||||
});
|
|
||||||
var response = await (await httpClient.PostAsync(BaseURI, json)).Content.ReadAsStringAsync();
|
|
||||||
var apiResult = JsonConvert.DeserializeObject<ttrss.messages.GenericApiResponse>(response);
|
|
||||||
int level = 0;
|
|
||||||
if(int.TryParse(apiResult.Content["level"], out level))
|
|
||||||
return level;
|
|
||||||
else
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> Logout()
|
public async Task<bool> Logout()
|
||||||
{
|
{
|
||||||
assertInitialized();
|
assertInitialized();
|
||||||
throw new NotImplementedException();
|
|
||||||
|
return (await oneValueGet<string>("logout", "status"))?.ToLower() == "ok";
|
||||||
}
|
}
|
||||||
public async Task<bool> IsLoggedIn()
|
public async Task<bool> IsLoggedIn()
|
||||||
{
|
{
|
||||||
assertInitialized();
|
//assertInitialized();
|
||||||
throw new NotImplementedException();
|
return (await oneValueGet<bool>("isLoggedIn", "status"));
|
||||||
}
|
}
|
||||||
public async Task<int> GetUnread()
|
public async Task<int> GetUnread()
|
||||||
{
|
{
|
||||||
assertInitialized();
|
assertInitialized();
|
||||||
throw new NotImplementedException();
|
return await oneValueGet<int>("getUnread", "unread");
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task GetFeeds(int cat_id, bool unread_only, int limit, int offset, bool include_nested)
|
public async Task GetFeeds(int cat_id, bool unread_only, int limit, int offset, bool include_nested)
|
||||||
{
|
{
|
||||||
assertInitialized();
|
assertInitialized();
|
||||||
@ -155,7 +146,7 @@ namespace ttrss_co_client.ttrss
|
|||||||
public async Task GetLabels(int? article_id)
|
public async Task GetLabels(int? article_id)
|
||||||
{
|
{
|
||||||
assertInitialized();
|
assertInitialized();
|
||||||
if(article_id != null)
|
if (article_id != null)
|
||||||
{
|
{
|
||||||
assertApiLevel(5);
|
assertApiLevel(5);
|
||||||
}
|
}
|
||||||
@ -171,7 +162,7 @@ namespace ttrss_co_client.ttrss
|
|||||||
{
|
{
|
||||||
assertInitialized();
|
assertInitialized();
|
||||||
assertApiLevel(4);
|
assertApiLevel(4);
|
||||||
if(!sanitize)
|
if (!sanitize)
|
||||||
assertApiLevel(20);
|
assertApiLevel(20);
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
@ -206,10 +197,37 @@ namespace ttrss_co_client.ttrss
|
|||||||
}
|
}
|
||||||
private void assertApiLevel(int ApiLevel)
|
private void assertApiLevel(int ApiLevel)
|
||||||
{
|
{
|
||||||
if(ApiLevel > this.api_level)
|
if (ApiLevel > this.api_level)
|
||||||
{
|
{
|
||||||
throw new NotSupportedException($"method requires api level {ApiLevel}, have {this.api_level}");
|
throw new NotSupportedException($"method requires api level {ApiLevel}, have {this.api_level}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private async Task<T> oneValueGet<T>(string op, string key)
|
||||||
|
{
|
||||||
|
//mostly you post {"op": "aThingToDo", "sid": "sessionId", "some other param": "some other value"}
|
||||||
|
//and get back something like {"seq": 0, "status", "content": {"the value you asked for": 0}}
|
||||||
|
var json = JsonContent.Create(new
|
||||||
|
{
|
||||||
|
op = op,
|
||||||
|
sid = this.SessionId
|
||||||
|
});
|
||||||
|
var response = await (await httpClient.PostAsync(BaseURI, json)).Content.ReadAsStringAsync();
|
||||||
|
var apiResult = JsonConvert.DeserializeObject<ttrss.messages.GenericApiResponse>(response);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var converter = TypeDescriptor.GetConverter(typeof(T));
|
||||||
|
if (converter != null)
|
||||||
|
{
|
||||||
|
return (T)converter.ConvertFromString(apiResult.Content[key]);
|
||||||
|
}
|
||||||
|
return default(T);
|
||||||
|
}
|
||||||
|
catch (NotSupportedException)
|
||||||
|
{
|
||||||
|
return default(T);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user