several more methods

This commit is contained in:
Adam R Grey 2023-04-03 00:36:53 -04:00
parent 290092df5a
commit 1480efdd82
2 changed files with 53 additions and 22 deletions

View File

@ -9,8 +9,21 @@ namespace ttrss_co_client
var conf = Configure();
var ttrssClient = new ttrss.ApiClient(conf.BaseURI);
await ttrssClient.Login(conf.Username, conf.Password);
var loggedin = await ttrssClient.IsLoggedIn();
Console.WriteLine($"logged in: {loggedin}");
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")
{

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.ComponentModel;
using Newtonsoft.Json;
using System.Net.Http.Json;
@ -11,7 +12,7 @@ namespace ttrss_co_client.ttrss
public Uri BaseURI { get; private set; }
private HttpClient httpClient { get; set; }
private string SessionId { get; set; } = null;
private int api_level{get;set;}
private int api_level { get; set; }
public ApiClient(Uri baseUri)
{
@ -45,36 +46,26 @@ namespace ttrss_co_client.ttrss
public async Task<int> GetApiLevel()
{
assertInitialized();
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;
return await oneValueGet<int>("getApiLevel", "level");
}
public async Task<bool> Logout()
{
assertInitialized();
throw new NotImplementedException();
return (await oneValueGet<string>("logout", "status"))?.ToLower() == "ok";
}
public async Task<bool> IsLoggedIn()
{
assertInitialized();
throw new NotImplementedException();
//assertInitialized();
return (await oneValueGet<bool>("isLoggedIn", "status"));
}
public async Task<int> GetUnread()
{
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)
{
assertInitialized();
@ -155,7 +146,7 @@ namespace ttrss_co_client.ttrss
public async Task GetLabels(int? article_id)
{
assertInitialized();
if(article_id != null)
if (article_id != null)
{
assertApiLevel(5);
}
@ -171,7 +162,7 @@ namespace ttrss_co_client.ttrss
{
assertInitialized();
assertApiLevel(4);
if(!sanitize)
if (!sanitize)
assertApiLevel(20);
throw new NotImplementedException();
}
@ -206,10 +197,37 @@ namespace ttrss_co_client.ttrss
}
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}");
}
}
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);
}
}
}
}