A .Net wrapper for the Telegra.ph API
Install-Package Telegraph.Net
var client = new TelegraphClient();
To use Telegra.ph API methods that require access-token, call the .GetTokenClient(...) method of the TelegraphClient class, passing the access-token of the context account, viz:
ITokenClient tokenClient = client.GetTokenClient("access-token-from-a-save-location"); // Revoke an access token Account account = await tokenClient.RevokeAccessTokenAsync(); // Get account information Account account = await tokenClient.GetAccountInformationAsync( AccountFields.ShortName | AccountFields.AuthorUrl | AccountFields.AuthorUrl ); // Edit account information Account updatedAccount = await tokenClient.EditAccountInformationAsync( "new-short-name", "new-author-name", "new-author-url" ); // Create a new Page Page newPage = await tokenClient.CreatePageAsync( "page-title", content /* NodeElement[] */, returnContent: true ); // Edit a page Page editedPage = await tokenClient.EditPageAsync( "path-of-page-to-edit", "new-page-title", updateContent /* NodeElement[] */ ); // Get first 50 pages created by the account with the context access-token PageList pageList = await tokenClient.GetPageListAsync(offset: 0, limit: 50);
Methods that doesn't require access-token can be called directly on the TelegraphClient class, viz
// Retrieve the total number of views for a page int views = await client.GetViewsAsync("page-path", year: 2016, month: 12); // Get a page information Page page = await client.GetPageAsync("page-path", returnContent: true); // Create a new Telegra.ph Account Account newAccount = await client.CreateAccountAync("Sandbox", "Anonymous", "http://sandbox.net");
According to Telegraph API page: A Node represents a DOM Node. It can be a String which represents a DOM text node or a NodeElement object.
To simplify working with this concept while using this library, all Nodes is a NodeElement. This means that, a string is a NodeElement; thus, a text content like "Hello World!" can thus be expressed as:
NodeElement nodeElement = new NodeElement { Tag = "_text", Attributes = new Dictionary<string, string> { { "value", "Hello World!" } } };
OR preferably (and the recommended way), simply as:
NodeElement nodeElement = "Hello World!";
This shorthand takes advantage of implicit operator overloading for string<-->NodeElement.
Thus, the following json content:
[
{
"tag": "p",
"children": [
"Hello, world!",
{
"tag": "p",
"children": ["This is the second line to first paragraph."]
}
]
}
]could be constructed as:
var nodes = new List<NodeElement>(); nodes.Add( new NodeElement("p", null /* no attribute */, "Hello World!", new NodeElement("p", null /* again no attribute */, "This is the second line to first paragraph." ) ) ); ... NodeElement[] nodesArray = nodes.ToArray();
This library is completely covered via Test (all passing)!