Skip to content

adebisi-fa/telegra.ph.net

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Telegra.ph .Net

A .Net wrapper for the Telegra.ph API

Installation

Install-Package Telegraph.Net

Usage

var client = new TelegraphClient();

Access-Token based Methods

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);

Non Token-Based Methods

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");

Working with NodeElements

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();

Test Coverage

This library is completely covered via Test (all passing)!

About

A .Net wrapper for the Telegra.ph API

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages