Created
November 3, 2022 14:41
-
-
Save farukcan/d3f7253fde1948adfd4a3d72a90bdc74 to your computer and use it in GitHub Desktop.
Fast DotNetCore Notion Integration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Notion.Client; | |
namespace NotionQuickBlog.Data | |
{ | |
public static class Notion | |
{ | |
public static string? Token => Environment.GetEnvironmentVariable("NOTION_TOKEN"); | |
static NotionInstance? instance; | |
public static NotionInstance Instance => instance!=null ? instance : (instance=new NotionInstance()); | |
public static NotionClient Client => Instance.client; | |
public static T? GetProp<T>(this Page page, string propName) where T : PropertyValue { | |
var prop = page.Properties.ContainsKey(propName) ? page.Properties[propName] : null; | |
if(prop==null) return null; | |
return (T)prop; | |
} | |
public static string GetEmail(this Page page, string property="Email"){ | |
var prop = page.GetProp<EmailPropertyValue>(property); | |
return prop!=null ? prop.Email : string.Empty; | |
} | |
public static string GetText(this Page page, string property="Text"){ | |
var prop = page.GetProp<RichTextPropertyValue>(property); | |
if(prop==null || prop.RichText.Count==0){ | |
return string.Empty; | |
} | |
return prop.RichText.First().PlainText; | |
} | |
public static string GetUrl(this Page page, string property="URL"){ | |
var prop = page.GetProp<UrlPropertyValue>(property); | |
return prop!=null ? prop.Url : string.Empty; | |
} | |
public static string GetTitle(this Page page, string property="Name"){ | |
var prop = page.GetProp<TitlePropertyValue>(property); | |
if(prop==null || prop.Title.Count==0){ | |
return string.Empty; | |
} | |
return prop.Title.First().PlainText; | |
} | |
public static int GetInteger(this Page page, string property="Number"){ | |
var prop = page.GetProp<NumberPropertyValue>(property); | |
return prop!=null ? Convert.ToInt32(prop.Number) : 0; | |
} | |
public static T? GetEnum<T>(this Page page, string property="Select") where T : Enum { | |
var prop = page.GetProp<SelectPropertyValue>(property); | |
if(prop==null || prop.Select==null || prop.Select.Name==null){ | |
return default(T); | |
} | |
return (T)Enum.Parse(typeof(T), prop.Select.Name); | |
} | |
public static string BaseToString(this RichTextBase text){ | |
if(string.IsNullOrEmpty(text.PlainText) && text is RichTextText){ | |
return ((RichTextText)text).Text.Content; | |
} | |
return text.PlainText; | |
} | |
public static TitlePropertyValue ToTitlePropertyValue(this string? text){ | |
return new TitlePropertyValue(){ | |
Title = new List<RichTextBase>(){ | |
new RichTextText { Text = new Text { Content = text } } | |
} | |
}; | |
} | |
public static RichTextPropertyValue ToRichTextPropertyValue(this string? text){ | |
if(string.IsNullOrEmpty(text)) return new RichTextPropertyValue(){ | |
RichText = new List<RichTextBase>() | |
}; | |
return new RichTextPropertyValue(){ | |
RichText = new List<RichTextBase>(){ | |
new RichTextText { Text = new Text { Content = text } } | |
} | |
}; | |
} | |
public static UrlPropertyValue ToUrlPropertyValue(this string? text){ | |
return new UrlPropertyValue(){ | |
Url = text | |
}; | |
} | |
public static EmailPropertyValue ToEmailPropertyValue(this string? text){ | |
return new EmailPropertyValue(){ | |
Email = text | |
}; | |
} | |
public static NumberPropertyValue ToNumberPropertyValue(this int number){ | |
return new NumberPropertyValue(){ | |
Number = number | |
}; | |
} | |
public static SelectPropertyValue ToSelectPropertyValue<T>(this T value) where T : Enum { | |
return new SelectPropertyValue(){ | |
Select = new SelectOption(){ | |
Name = value.ToString() | |
} | |
}; | |
} | |
} | |
public class NotionInstance{ | |
public NotionClient client; | |
public NotionInstance() { | |
client = NotionClientFactory.Create(new ClientOptions{ | |
AuthToken = Notion.Token | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment