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 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")
{ {

View File

@ -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;
@ -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();
@ -211,5 +202,32 @@ namespace ttrss_co_client.ttrss
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);
}
}
} }
} }