public override UploadResult UploadText(string text, string fileName) { UploadResult ur = new UploadResult(); if (!string.IsNullOrEmpty(text) && !string.IsNullOrEmpty(fileName)) { string url; if (!string.IsNullOrEmpty(CustomURLAPI)) { url = CustomURLAPI; } else { url = URLAPI; } url = URLHelpers.CombineURL(url, "gists"); GistUpload gistUpload = new GistUpload() { description = "", @public = PublicUpload, files = new Dictionary <string, GistUploadFileInfo>() { { fileName, new GistUploadFileInfo() { content = text } } } }; string json = JsonConvert.SerializeObject(gistUpload); NameValueCollection headers = new NameValueCollection(); headers.Add("Authorization", "token " + AuthInfo.Token.access_token); string response = SendRequest(HttpMethod.POST, url, json, RequestHelpers.ContentTypeJSON, null, headers); GistResponse gistResponse = JsonConvert.DeserializeObject <GistResponse>(response); if (response != null) { if (RawURL) { ur.URL = gistResponse.files.First().Value.raw_url; } else { ur.URL = gistResponse.html_url; } } } return(ur); }
public static object ToMetaData(this GistResponse response, int maxDescriptionLength) { return(new { response.Id, Description = response.Description.Substring(0, maxDescriptionLength < 1 ? response.Description.Length : maxDescriptionLength), response.Created, response.Updated }); }
public override UploadResult UploadText(string text, string fileName) { UploadResult ur = new UploadResult(); if (!string.IsNullOrEmpty(text) && !string.IsNullOrEmpty(fileName)) { var gistUploadObject = new { @public = PublicUpload, files = new Dictionary <string, object> { { fileName, new { content = text } } } }; string url; if (!string.IsNullOrEmpty(CustomURLAPI)) { url = CustomURLAPI; } else { url = URLAPI; } url = URLHelpers.CombineURL(url, "gists"); string json = JsonConvert.SerializeObject(gistUploadObject); Dictionary <string, string> args = new Dictionary <string, string>(); args.Add("access_token", AuthInfo.Token.access_token); string response = SendRequest(HttpMethod.POST, url, json, UploadHelpers.ContentTypeJSON, args); GistResponse gistResponse = JsonConvert.DeserializeObject <GistResponse>(response); if (response != null) { if (RawURL) { ur.URL = gistResponse.files.First().Value.raw_url; } else { ur.URL = gistResponse.html_url; } } } return(ur); }
public static void SaveFiles(this GistResponse response, string destination) { const string message = "Unable to parse {0} from gist response"; Directory.CreateDirectory(destination); foreach (var(_, value) in response.Files) { var filename = (value.SelectToken("filename") ?? throw new InvalidOperationException(string.Format(message, "filename"))).Value <string>(); var content = (value.SelectToken("content") ?? throw new InvalidOperationException(string.Format(message, "content"))).Value <string>(); var path = Path.Combine(destination, filename); File.WriteAllText(path, content); } }