Skip to content

Instantly share code, notes, and snippets.

@Ragzouken
Last active December 8, 2021 21:32
Show Gist options
  • Save Ragzouken/b0ba3b1d602383e02086 to your computer and use it in GitHub Desktop.
Save Ragzouken/b0ba3b1d602383e02086 to your computer and use it in GitHub Desktop.
simple gist thing for unity/c#
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Text;
namespace MakeGist
{
public class Gist
{
public static IEnumerator Make(string description,
Dictionary<string, string> files,
Action<string> onComplete)
{
var fileString = new StringBuilder();
fileString.AppendLine("{");
bool first = true;
foreach (var file in files)
{
if (!first) fileString.AppendLine(",");
fileString.AppendFormat("\"{0}\": {{ \"content\": \"{1}\" }}",
file.Key,
file.Value);
if (first) first = false;
}
fileString.AppendLine();
fileString.AppendLine("}");
var encoding = new UTF8Encoding();
var json = @"{
""description"":""" + description + @""",
""public"":true,
""files"": " + fileString.ToString() + @"
}";
var request = new WWW("https://api.github.com/gists",
encoding.GetBytes(json));
yield return request;
var response = SimpleJSON.JSON.Parse(request.text);
onComplete(response["id"]);
}
public static IEnumerator Take(string id,
Action<Dictionary<string, string>> onComplete)
{
var files = new Dictionary<string, string>();
var request = new WWW("https://api.github.com/gists/" + id);
yield return request;
var response = SimpleJSON.JSON.Parse(request.text);
foreach (string file in response["files"].Keys)
{
files.Add(file, response["files"][file]["content"]);
}
onComplete(files);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment