Skip to content

Commit

Permalink
Add types to everything
Browse files Browse the repository at this point in the history
  • Loading branch information
mjwsteenbergen committed Mar 15, 2024
1 parent e248123 commit dd9e9f3
Show file tree
Hide file tree
Showing 29 changed files with 152 additions and 41 deletions.
132 changes: 98 additions & 34 deletions ApiLibs/NotionRest/Block/Block.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;

namespace ApiLibs.NotionRest
{
Expand All @@ -13,16 +18,38 @@ public abstract class NotionBlock
public string Type { get; set; }

[JsonProperty("has_children")]
public bool HasChildren { get; set;}
public bool? HasChildren { get; set;}

[JsonProperty("object")]
public string Object { get; set;}

[JsonProperty("id")]
public Guid Id { get; set; }
public Guid? Id { get; set; }
}

public class ForceDefaultConverter : JsonConverter
{
public override bool CanRead => false;
public override bool CanWrite => false;

public override bool CanConvert(Type objectType)
{
throw new InvalidOperationException();
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new InvalidOperationException();
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new InvalidOperationException();
}
}

public class NotionBlockConverter : JsonConverter<NotionBlock>

{
class NotionPropertyImp : NotionBlock { }

Expand Down Expand Up @@ -80,42 +107,79 @@ public override NotionBlock ReadJson(JsonReader reader, Type objectType, NotionB

public override bool CanWrite => true;

public T Seria<T>(JsonWriter writer, JsonSerializer serializer, T value) {
serializer.Serialize(writer, value);
return value;
public void Serialize(PropertyInfo info, NotionBlock value, JsonWriter writer)
{
var val = info.GetValue(value);

if(val != null)
{
var customAttributes = (JsonPropertyAttribute[])info.GetCustomAttributes(typeof(JsonPropertyAttribute), true);
if (customAttributes.Length > 0)
{
var myAttribute = customAttributes[0];
string propName = myAttribute.PropertyName;

if(!string.IsNullOrEmpty(propName)) {
writer.WritePropertyName(propName);
} else {
writer.WritePropertyName(info.Name);
}
// TODO: Do something with the value
} else {
writer.WritePropertyName(info.Name);
}

writer.WriteRawValue(JsonConvert.SerializeObject(val, Formatting.None, new JsonSerializerSettings {
NullValueHandling = NullValueHandling.Ignore
}));
}
}

public override void WriteJson(JsonWriter writer, NotionBlock value, JsonSerializer serializer)
{
NotionBlock _ = value switch {
Paragraph paragraph => Seria(writer, serializer, paragraph),
Heading3 heading_3 => Seria(writer, serializer, heading_3),
Heading2 heading_2 => Seria(writer, serializer, heading_2),
Heading1 heading_1 => Seria(writer, serializer, heading_1),
BulletedListItem bulleted_list_item => Seria(writer, serializer, bulleted_list_item),
NumberedListItem numbered_list_item => Seria(writer, serializer, numbered_list_item),
ToDo to_do => Seria(writer, serializer, to_do),
Toggle toggle => Seria(writer, serializer, toggle),
ChildPage child_page => Seria(writer, serializer, child_page),
ChildDatabase child_database => Seria(writer, serializer, child_database),
Embed embed => Seria(writer, serializer, embed),
Image image => Seria(writer, serializer, image),
Video video => Seria(writer, serializer, video),
FileBlock file => Seria(writer, serializer, file),
Pdf pdf => Seria(writer, serializer, pdf),
Bookmark bookmark => Seria(writer, serializer, bookmark),
Callout callout => Seria(writer, serializer, callout),
Quote quote => Seria(writer, serializer, quote),
Equation equation => Seria(writer, serializer, equation),
Divider divider => Seria(writer, serializer, divider),
TableOfContents table_of_contents => Seria(writer, serializer, table_of_contents),
Column column => Seria(writer, serializer, column),
ColumnList column_list => Seria(writer, serializer, column_list),
LinkPreview link_preview => Seria(writer, serializer, link_preview),
Code code => Seria(writer, serializer, code),
Unsupported unsupported => Seria(writer, serializer, unsupported),
_ => throw new Exception("Invalid option")
};
// var serializeObject = new {
// type = value.Type
// };

// var serializeObject = new Dictionary<string, dynamic> {
// { "type" , value.Type + "" },
// { value.Type, value }
// };


writer.WriteStartObject();

var props = value.GetType().GetProperties();
foreach(PropertyInfo info in props.Where(i => i.DeclaringType.FullName == "ApiLibs.NotionRest.NotionBlock"))
{
Serialize(info, value, writer);
}

writer.WritePropertyName(value.Type);
writer.WriteStartObject();

foreach(PropertyInfo info in props.Where(i => i.DeclaringType.FullName != "ApiLibs.NotionRest.NotionBlock"))
{
Serialize(info, value, writer);
}

writer.WriteEndObject();
writer.WriteEndObject();

// writer.WritePropertyName("type");
// writer.WriteValue(value.Type);
// writer.WritePropertyName(value.Type);
// value.Type = null;



// var s = JsonConvert.SerializeObject(value, new ForceDefaultConverter());
// writer.WriteValue(new JRaw(s));
// JsonSerializer.CreateDefault().Serialize()
// JsonSerializer.Create().Serialize(writer, value);
// serializeObject[value.Type] = value;

// serializer.Serialize(writer, value);
}
}
}
1 change: 1 addition & 0 deletions ApiLibs/NotionRest/Block/Bookmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class Bookmark : NotionBlock
{
public Bookmark()
{
Type = "bookmark";
}

[JsonProperty("url")]
Expand Down
1 change: 1 addition & 0 deletions ApiLibs/NotionRest/Block/BulletedListItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class BulletedListItem : NotionBlock
{
public BulletedListItem()
{
Type = "bulleted_list_item";
}

[JsonProperty("text")]
Expand Down
1 change: 1 addition & 0 deletions ApiLibs/NotionRest/Block/Callout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class Callout : NotionBlock
{
public Callout()
{
Type = "callout";
}
}
}
1 change: 1 addition & 0 deletions ApiLibs/NotionRest/Block/ChildDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class ChildDatabase : NotionBlock
{
public ChildDatabase()
{
Type = "child_database";
}
}
}
1 change: 1 addition & 0 deletions ApiLibs/NotionRest/Block/ChildPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class ChildPage : NotionBlock
{
public ChildPage()
{
Type = "child_page";
}
}
}
5 changes: 5 additions & 0 deletions ApiLibs/NotionRest/Block/Code.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ namespace ApiLibs.NotionRest
{
public class Code : NotionBlock
{
public Code()
{
Type = "code";
}

[JsonProperty("text")]
public List<RichText> RichText { get; set; }

Expand Down
1 change: 1 addition & 0 deletions ApiLibs/NotionRest/Block/Column.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class Column : NotionBlock
{
public Column()
{
Type = "column";
}
}
}
1 change: 1 addition & 0 deletions ApiLibs/NotionRest/Block/ColumnList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class ColumnList : NotionBlock
{
public ColumnList()
{
Type = "column_list";
}
}
}
1 change: 1 addition & 0 deletions ApiLibs/NotionRest/Block/Divider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class Divider : NotionBlock
{
public Divider()
{
Type = "divider";
}
}
}
1 change: 1 addition & 0 deletions ApiLibs/NotionRest/Block/Embed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class Embed : NotionBlock
{
public Embed()
{
Type = "embed";
}
}
}
1 change: 1 addition & 0 deletions ApiLibs/NotionRest/Block/Equation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class Equation : NotionBlock
{
public Equation()
{
Type = "equation";
}
}
}
1 change: 1 addition & 0 deletions ApiLibs/NotionRest/Block/File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class FileBlock : NotionBlock
{
public FileBlock()
{
Type = "file";
}

[JsonProperty("file")]
Expand Down
3 changes: 2 additions & 1 deletion ApiLibs/NotionRest/Block/Heading1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ public class Heading1 : NotionBlock
{
public Heading1()
{
Type = "heading_1";
}

[JsonProperty("text")]
[JsonProperty("rich_text")]
public List<RichText> Text { get; set;}
}
}
1 change: 1 addition & 0 deletions ApiLibs/NotionRest/Block/Heading2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class Heading2 : Heading1
{
public Heading2()
{
Type = "heading_2";
}
}
}
1 change: 1 addition & 0 deletions ApiLibs/NotionRest/Block/Heading3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class Heading3 : Heading1
{
public Heading3()
{
Type = "heading_3";
}
}
}
15 changes: 15 additions & 0 deletions ApiLibs/NotionRest/Block/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ namespace ApiLibs.NotionRest
{
public class Image : NotionBlock
{
public Image()
{
Type = "image";
}

private File _imageFile;

[JsonProperty("image")]
Expand All @@ -24,10 +29,20 @@ public File ImageFile
}
}

[JsonProperty("type")]
public string ImageSourceType { get; set; }

[JsonProperty("caption")]
public List<RichText> Caption { get; set; }

[JsonProperty("file")]
public File File { get; set; }

[JsonProperty("external")]
public External External { get; set; }
}

public interface ImageSource {

}
}
1 change: 1 addition & 0 deletions ApiLibs/NotionRest/Block/LinkPreview.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class LinkPreview : NotionBlock
{
public LinkPreview()
{
Type = "link_preview";
}
}
}
1 change: 1 addition & 0 deletions ApiLibs/NotionRest/Block/NumberedListItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class NumberedListItem : NotionBlock
{
public NumberedListItem()
{
Type = "numbered_list_item";
}
}
}
3 changes: 2 additions & 1 deletion ApiLibs/NotionRest/Block/Paragraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ public class Paragraph : NotionBlock
{
public Paragraph()
{
Type = "paragraph";
}

[JsonProperty("text")]
[JsonProperty("rich_text")]
public List<RichText> Text { get; set;}

[JsonProperty("children")]
Expand Down
1 change: 1 addition & 0 deletions ApiLibs/NotionRest/Block/Pdf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class Pdf : NotionBlock
{
public Pdf()
{
Type = "pdf";
}
}
}
1 change: 1 addition & 0 deletions ApiLibs/NotionRest/Block/Quote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class Quote : NotionBlock
{
public Quote()
{
Type = "quote";
}

[JsonProperty("text")]
Expand Down
1 change: 1 addition & 0 deletions ApiLibs/NotionRest/Block/TableOfContents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class TableOfContents : NotionBlock
{
public TableOfContents()
{
Type = "table_of_contents";
}
}
}
Loading

0 comments on commit dd9e9f3

Please sign in to comment.