hey adam... y'all ever heard of a generic object?

This commit is contained in:
Adam R Grey 2023-04-03 16:47:24 -04:00
parent e4cbf5e7d4
commit 794d03b424
9 changed files with 30 additions and 71 deletions

View File

@ -2,6 +2,7 @@ using System.Collections.Generic;
using System.ComponentModel;
using Newtonsoft.Json;
using System.Net.Http.Json;
using ttrss_co_client.ttrss.messages;
using ttrss_co_client.ttrss.datastructures;
// https://tt-rss.org/wiki/ApiReference
@ -32,17 +33,17 @@ namespace ttrss_co_client.ttrss
};
var content = JsonContent.Create(json);
var response = await (await httpClient.PostAsync(BaseURI, content)).Content.ReadAsStringAsync();
var loginResult = JsonConvert.DeserializeObject<ttrss.messages.LoginResponse>(response);
var loginResult = JsonConvert.DeserializeObject<ApiResponse<LoginResponseContent>>(response);
if (loginResult.status == 0)
{
SessionId = loginResult.content.session_id;
if(loginResult.content.api_level == null)
SessionId = loginResult.Content.session_id;
if(loginResult.Content.api_level == null)
{
throw new NotImplementedException($"api doesn't report an api level - unsupported. api level 1 is version 1.5.8, so {BaseURI} might be extremely old. (or maybe this library is extremely old and ttrss changed again?)");
}
else
{
api_level = loginResult.content.api_level.Value;
api_level = loginResult.Content.api_level.Value;
}
Console.WriteLine(SessionId);
@ -104,8 +105,8 @@ namespace ttrss_co_client.ttrss
output_mode = output_mode
});
var response = await (await httpClient.PostAsync(BaseURI, json)).Content.ReadAsStringAsync();
var apiResult = JsonConvert.DeserializeObject<ttrss.messages.CounterInfoResponse>(response);
return apiResult.content;
var apiResult = JsonConvert.DeserializeObject<ApiResponse<IEnumerable<CounterInfo>>>(response);
return apiResult.Content;
}
///<param name="limit">0 for all</param>
///<param name="offset">skip this amount first</param>
@ -135,8 +136,8 @@ namespace ttrss_co_client.ttrss
offset = offset
});
var response = await (await httpClient.PostAsync(BaseURI, json)).Content.ReadAsStringAsync();
var apiResult = JsonConvert.DeserializeObject<ttrss.messages.FeedsResponse>(response);
return apiResult.content;
var apiResult = JsonConvert.DeserializeObject<ApiResponse<IEnumerable<Feed>>>(response);
return apiResult.Content;
}
///<param name="enable_nested">nested mode (return only top level)</param>
public async Task<IEnumerable<Category>> GetCategories(bool unread_only = false, bool enable_nested = false, bool include_empty = false)
@ -152,8 +153,8 @@ namespace ttrss_co_client.ttrss
include_empty = include_empty
});
var response = await (await httpClient.PostAsync(BaseURI, json)).Content.ReadAsStringAsync();
var apiResult = JsonConvert.DeserializeObject<ttrss.messages.CategoriesResponse>(response);
return apiResult.content;
var apiResult = JsonConvert.DeserializeObject<ApiResponse<IEnumerable<Category>>>(response);
return apiResult.Content;
}
public enum VIEWMODE { All, Unread, Adaptive, Marked, Updated }
@ -296,8 +297,8 @@ namespace ttrss_co_client.ttrss
private async Task<IEnumerable<Headline>> getHeadlines(JsonContent parameters)
{
var response = await (await httpClient.PostAsync(BaseURI, parameters)).Content.ReadAsStringAsync();
var apiResult = JsonConvert.DeserializeObject<ttrss.messages.HeadlinesResponse>(response);
return apiResult.content;
var apiResult = JsonConvert.DeserializeObject<ApiResponse<IEnumerable<Headline>>>(response);
return apiResult.Content;
}
// public async Task<HeadlinesHeaderResponse> GetHeadlinesAndHeader(
// int feed_id,
@ -541,7 +542,7 @@ namespace ttrss_co_client.ttrss
sid = this.SessionId
});
var response = await (await httpClient.PostAsync(BaseURI, json)).Content.ReadAsStringAsync();
var apiResult = JsonConvert.DeserializeObject<ttrss.messages.GenericApiResponse>(response);
var apiResult = JsonConvert.DeserializeObject<ttrss.messages.ApiResponse<Dictionary<string, string>>>(response);
try
{
var converter = TypeDescriptor.GetConverter(typeof(T));

View File

@ -2,13 +2,10 @@ using System.Collections.Generic;
namespace ttrss_co_client.ttrss.messages
{
public abstract class ApiResponse
public class ApiResponse<T>
{
public int seq { get; set; }
public int status { get; set; }
}
public class GenericApiResponse : ApiResponse
{
public Dictionary<string, string> Content{get; set;}
public T Content {get; set;}
}
}

View File

@ -1,9 +0,0 @@
using ttrss_co_client.ttrss.datastructures;
namespace ttrss_co_client.ttrss.messages
{
public class CategoriesResponse : ApiResponse
{
public IEnumerable<Category> content { get; set; }
}
}

View File

@ -1,9 +0,0 @@
using ttrss_co_client.ttrss.datastructures;
namespace ttrss_co_client.ttrss.messages
{
public class CounterInfoResponse : ApiResponse
{
public IEnumerable<CounterInfo> content { get; set; }
}
}

View File

@ -1,9 +0,0 @@
using ttrss_co_client.ttrss.datastructures;
namespace ttrss_co_client.ttrss.messages
{
public class FeedsResponse : ApiResponse
{
public IEnumerable<Feed> content { get; set; }
}
}

View File

@ -2,8 +2,10 @@ using ttrss_co_client.ttrss.datastructures;
namespace ttrss_co_client.ttrss.messages
{
public class HeadlinesHeaderResponse : ApiResponse
public class HeadlinesHeaderContent
{
public IEnumerable<Headline> content { get; set; }
public int id { get; set; }
public int first_id { get; set; }
public bool is_cat { get; set; }
}
}

View File

@ -1,9 +0,0 @@
using ttrss_co_client.ttrss.datastructures;
namespace ttrss_co_client.ttrss.messages
{
public class HeadlinesResponse : ApiResponse
{
public IEnumerable<Headline> content { get; set; }
}
}

View File

@ -1,15 +0,0 @@
namespace ttrss_co_client.ttrss.messages
{
public class LoginResponse : ApiResponse
{
public Content content { get; set; }
public class Content
{
public string session_id { get; set; }
public Configuration config { get; set; }
public int? api_level { get; set; }
public string error { get; set; }
}
}
}

View File

@ -0,0 +1,10 @@
namespace ttrss_co_client.ttrss.messages
{
public class LoginResponseContent
{
public string session_id { get; set; }
public Configuration config { get; set; }
public int? api_level { get; set; }
public string error { get; set; }
}
}