diff --git a/.gitignore b/.gitignore index 2f72fc4..d9dace5 100644 --- a/.gitignore +++ b/.gitignore @@ -154,3 +154,4 @@ $RECYCLE.BIN/ # Mac desktop service store files .DS_Store + diff --git a/Meduza.net/Api.cs b/Meduza.net/Api.cs index 1feb85e..3cbb0cf 100644 --- a/Meduza.net/Api.cs +++ b/Meduza.net/Api.cs @@ -1,15 +1,15 @@ -using System; -using System.ComponentModel; -using System.Diagnostics; -using System.IO; +using System.ComponentModel; using System.Net; using System.Net.Http; using System.Runtime.CompilerServices; -using System.Text; using System.Threading.Tasks; using Meduza.net.Annotations; +using Meduza.net.Models.Api; +using Meduza.net.Models.Api.Enum; using Meduza.net.Models.Api.Page; +using Meduza.net.Models.Api.Types; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; namespace Meduza.net { public sealed class Api : INotifyPropertyChanged { @@ -56,5 +56,10 @@ private void OnPropertyChanged([CallerMemberName] string propertyName = null) { var handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } + + public async Task LoadNewsAsync(string uri) { + var content = await _httpClient.GetStringAsync(uri); + return JObject.Parse(content).GetValue("root").ToObject(); + } } } diff --git a/Meduza.net/Helpers/UnixDateTimeConverter.cs b/Meduza.net/Helpers/UnixDateTimeConverter.cs index dcc0f52..7c93c2b 100644 --- a/Meduza.net/Helpers/UnixDateTimeConverter.cs +++ b/Meduza.net/Helpers/UnixDateTimeConverter.cs @@ -12,22 +12,22 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var intValue = reader.Value as int?; if (intValue.HasValue) { - return _unixStartDateTime.AddMilliseconds(intValue.Value); + return _unixStartDateTime.AddSeconds(intValue.Value); } var longValue = reader.Value as long?; if (longValue.HasValue) { - return _unixStartDateTime.AddMilliseconds(longValue.Value); + return _unixStartDateTime.AddSeconds(longValue.Value); } var doubleValue = reader.Value as double?; if (doubleValue.HasValue) { - return _unixStartDateTime.AddMilliseconds(doubleValue.Value); + return _unixStartDateTime.AddSeconds(doubleValue.Value); } var stringValue = reader.Value as string; if (!string.IsNullOrWhiteSpace(stringValue)) { - return _unixStartDateTime.AddMilliseconds(Double.Parse(stringValue)); + return _unixStartDateTime.AddSeconds(Double.Parse(stringValue)); } throw new ArgumentException(); diff --git a/Meduza.net/Meduza.net.csproj b/Meduza.net/Meduza.net.csproj index b3417ab..b38fd8a 100644 --- a/Meduza.net/Meduza.net.csproj +++ b/Meduza.net/Meduza.net.csproj @@ -40,6 +40,7 @@ + @@ -48,6 +49,7 @@ + diff --git a/Meduza.net/Models/Api/Content.cs b/Meduza.net/Models/Api/Content.cs new file mode 100644 index 0000000..a540595 --- /dev/null +++ b/Meduza.net/Models/Api/Content.cs @@ -0,0 +1,26 @@ +using Newtonsoft.Json; + +namespace Meduza.net.Models.Api { + public sealed class Content { + private readonly string _body; + private readonly string _layoutUri; + private readonly string _description; + public Content( + string body, + [JsonProperty(PropertyName = "layout_url")] string layoutUri, + string description) { + _body = body; + _layoutUri = layoutUri; + _description = description; + } + public string Body { + get { return _body; } + } + public string LayoutUri { + get { return _layoutUri; } + } + public string Description { + get { return _description; } + } + } +} diff --git a/Meduza.net/Models/Api/Document.cs b/Meduza.net/Models/Api/Document.cs index 83e11b1..6664115 100644 --- a/Meduza.net/Models/Api/Document.cs +++ b/Meduza.net/Models/Api/Document.cs @@ -7,7 +7,7 @@ using Newtonsoft.Json.Converters; namespace Meduza.net.Models.Api { - public sealed class Document { + public class Document { private readonly string _uri; private readonly string _title; private readonly string _secondTitle; @@ -108,6 +108,6 @@ public Image Image { public FunType FunType { get; set; } //Topic properties - public IReadOnlyList Content { get; set; } + public IReadOnlyList Content { get; set; } } } diff --git a/Meduza.net/Models/Api/Types/News.cs b/Meduza.net/Models/Api/Types/News.cs new file mode 100644 index 0000000..6caa378 --- /dev/null +++ b/Meduza.net/Models/Api/Types/News.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using Meduza.net.Helpers; +using Meduza.net.Models.Api.Enum; +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; + +namespace Meduza.net.Models.Api.Types { + public sealed class News : Document { + public News( + [JsonProperty(PropertyName = "url")] string uri, + string title, + [JsonProperty(PropertyName = "second_title")] string secondTitle, + IReadOnlyList> authors, + + [JsonProperty(PropertyName = "document_type")] + [JsonConverter(typeof(StringEnumConverter))] + DocumentType documentType, + + int version, + [JsonProperty(PropertyName = "published_at")] + [JsonConverter(typeof(UnixDateTimeConverter))] DateTime publishedAt, + + [JsonProperty(PropertyName = "updated_at")] + [JsonConverter(typeof(UnixDateTimeConverter))] DateTime updatedAt, + + [JsonProperty(PropertyName = "full")] bool isFull, + Source source, + Image image) + : base( + uri, + title, + secondTitle, + authors, + documentType, + version, + publishedAt, + updatedAt, + isFull, + source, + image) { } + public new Content Content { get; set; } + } +} diff --git a/Tests/Loading.cs b/Tests/Loading.cs index 92c0f83..3e60664 100644 --- a/Tests/Loading.cs +++ b/Tests/Loading.cs @@ -1,4 +1,5 @@ -using Meduza.net; +using System.Linq; +using Meduza.net; using NUnit.Framework; namespace Tests { @@ -18,5 +19,14 @@ public async void NonDefaultInitialization() { await api.InitializeAsync(); Assert.NotNull(api.Main); } + [Test] + public async void LoadNews() { + var api = new Api(); + + var document = api.Main.Documents.First(); + var fullDocument = await api.LoadNewsAsync(document.Value.Uri); + + Assert.NotNull(fullDocument); + } } }