Skip to content

pierre3/GistsApi

Repository files navigation

Gists API C# library

This is Gists API v3 library for .Net.

Install

from nuget gallery

PM> Install-Package GistsApi

GistClient class

public class GistClient
{
    public async Task Authorize(string authCode);

    //POST https://api.github.com/gists
    public async Task<GistObject> CreateAGist(string description, bool isPublic, IEnumerable<Tuple<string, string>> fileContentCollection);
    
    //PATCH https://api.github.com/gists/:id
    public async Task<GistObject> EditAGist(string id, string description, string targetFilename, string content);

    //PATCH https://api.github.com/gists/:id (Change filename)
    public async Task<GistObject> EditAGist(string id, string description, string oldFilename, string newFilename, string content);
    
    //PATCH https://api.github.com/gists/:id (Set "null" to filename)
    public async Task<GistObject> DeleteAFile(string id, string description, string filename);
    
    //DELETE https://api.github.com/gists/:id
    public async Task DeleteAGist(string id);
    
    //GET https://api.github.com/gists (List authenticated users gist)
    public async Task<IEnumerable<GistObject>> ListGists();

    //GET https://api.github.com/users/:user/gists
    public async Task<IEnumerable<GistObject>> ListGists(string user);

    //GET https://api.github.com/users/:user/gists?since=:date_time
    public async Task<IEnumerable<GistObject>> ListGists(string user, DateTime since);
    
    //List PublicGists, UsersGists, AuthenticatedUserGists or AuthenticatedUserStarredGists
    public async Task<IEnumerable<GistObject>> ListGists(ListMode mode);
    
    //GET https://api.github.com/gists/:id
    public async Task<GistObject> GetSingleGist(string id);
    
    //POST https://api.github.com/gists/:id/forks
    public async Task<GistObject> ForkAGist(string id);
    
    //PUT https://api.github.com/gists/:id/star
    public async Task StarAGist(string id);
    
    //DELETE https://api.github.com/gists/:id/star
    public async Task UnstarAGist(string id);

    //GET row_url (from API response)
    public async Task<string> DownloadRawText(Uri rawUrl);
    
    //Cancel all pending requests
    public void Cancel();
    
    //List mode for ListGists method 
    public enum ListMode
    {
        PublicGists,
        UsersGists,
        AuthenticatedUserGists,
        AuthenticatedUserStarredGists
    }
}

Usage

OAuth 2.0 flow (for WPF)

xaml

<WebBrowser Name="webBrowser" LoadCompleted="webBrowser_LoadCompleted"/>

xaml.cs

//Constructor
public MainWindow()
{
    InitializeComponent();
    
    gistClient = new GistClient("clientID", "clientSecret", "userAgent");
    
    //navigate to "https://github.com/login/oauth/authorize" 
    webBrowser.Navigate(gistClient.AuthorizeUrl);
}

private async void webBrowser_LoadCompleted(object sender, NavigationEventArgs e) 
{
    if (e.Uri == null)
    { return; }

    if (e.Uri.AbsoluteUri.Contains("code="))
    {
        var authCode = Regex.Split(result.Uri.AbsoluteUri, "code=")[1];
        
        //get access token
        await gistClient.Authorize(authCode)
   }
}

Exsamples

private GistClient gistClient;

public async void ListGists()
{
    try
    {
    
        ShowMessage("List gists...");

        var gists = await gistClient.ListGists();
    
        var myGist = gists.First(gist => gist.files.Any(file => file.filename == "MyGist_File1"));
        var rawUrl = myGist.files.First(file => file.filename == "MyGist_File1").raw_url;

        //Download content text of "MyGist_File1".
        var downloadText = await gistClient.DownloadRawText(rawUrl);

        ShowMessage("Completed.");
        ShowMessage(downloadText);
    }

    catch (System.Net.Http.HttpRequestException e)
    {
        ShowMessageMethod("Error." + e.Message);
    }
    catch (OperationCanceledException)
    {
        ShowMessageMethod("Canceled".);
    }
}

public async void CreateAGist()
{
    try
    {
        var description = "gist description";
        bool isPublic = true;
        var uploadFiles = new[]{
            Tuple.Create("file1.txt", "file content..."),
            Tuple.Create("file2.cs", "using system; ..."),
            Tuple.Create("file2.md", "# Readme Gists API ...")
        };
        
       ShowMessage("Create a gist")
       
       await gistClient.CreateAGist(description,isPublic,uploadFiles);
       
       ShowMessage("Completed.");
       
    } 
    catch (System.Net.Http.HttpRequestException e)
    {
        ShowMessage = "Error. " + e.Message;
    }
    catch (OperationCanceledException)
    {
        ShowMessage = "Canceled.";
    }
}

//
// Cancel pending requests.
//
public void Cancel()
{
    //Throw OperationCanceledException
    gistClient.Cancel();
}

See WpfGists source code for details.

WPFGists

GistsAPI client GUI for Windows.

download => [release v0.5.1.0] (https://github.com/pierre3/GistsApi/releases)

sample window

WpfGists -Features

  • Upload a new gist (create a single-file gist, and multi-files gists)
  • Display and edit a content of downloaded gist.
  • List Gists (My gists, All public gists, User's gists, My starred gist)
  • Fork and Star a gist.

WpfGists Document

License

Microsoft Public License (MS-PL)

About

Gists API v3 Library for .Net

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages