/// <summary> /// Download a (single file) Gist from the github server. /// </summary> /// <param name="id">Id of Gist to download.</param> /// <returns>Gist object with contents set to Gist contents.</returns> internal static Gist Get(string id) { WebRequest request = WebRequest.Create(String.Format(GetUrl, id)); WebResponse response = request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); StringBuilder sb = new StringBuilder(); while (!reader.EndOfStream) { sb.AppendLine(reader.ReadLine()); } string contents = sb.ToString(); reader.Close(); if (contents.Length > 0) { Gist result = new Gist(); result.Contents = contents; return result; } else { return null; } }
/// <summary> /// This method is called when the user clicks on the /// Post button. If all information is present, then the Gist /// is sent to the server. /// </summary> /// <param name="sender">Event sender (ignored).</param> /// <param name="e">Event arguments (ignored).</param> private void ButtonPost_Click(object sender, EventArgs e) { Gist gist = new Gist(); gist.Filename = this.textFile.Text; gist.Contents = this.textContent.Text; gist.Private = this.checkPrivate.Checked; try { GistServer.Post(gist); this.DialogResult = DialogResult.OK; this.Close(); } catch (Exception ex) { MessageBox.Show( "Posting gist wasn't successful: " + ex.Message, "GistBoard", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Send a new Gist to the server. /// </summary> /// <param name="gist">Gist to post on server.</param> /// <exception cref="ArgumentException">If the login data is missing.</exception> internal static void Post(Gist gist) { string user = Config.Instance.User; string token = Config.Instance.Token; if (user == null || token == null) { throw new ArgumentException("Need login data!"); } byte[] data = PrepareFormData(user, token, gist); WebRequest request = WebRequest.Create(PostUrl); request.Method = "POST"; request.ContentLength = data.Length; request.ContentType = "application/x-www-form-urlencoded"; Stream dataStream = request.GetRequestStream(); dataStream.Write(data, 0, data.Length); dataStream.Close(); WebResponse response = request.GetResponse(); Console.WriteLine("Response code: " + ((HttpWebResponse)response).StatusCode); }
/// <summary> /// Prepares the urlencoded data sent to the server. /// </summary> /// <param name="user">Github user name.</param> /// <param name="token">Github API token.</param> /// <param name="gist">Object with Gist information.</param> /// <returns>Urlencoded data which will be sent to the server.</returns> private static byte[] PrepareFormData(string user, string token, Gist gist) { Dictionary<string, string> parameters = new Dictionary<string, string>(); parameters.Add("login", user); parameters.Add("token", token); parameters.Add("file_ext[gistfile1]", string.Empty); parameters.Add("file_name[gistfile1]", gist.Filename); parameters.Add("file_contents[gistfile1]", gist.Contents); if (gist.Private) { parameters.Add("private", "on"); } string[] tokens = new string[parameters.Keys.Count]; int idx = 0; foreach (string key in parameters.Keys) { tokens[idx] = key + "=" + HttpUtility.UrlEncode(parameters[key]); idx++; } string data = string.Join("&", tokens); return Encoding.ASCII.GetBytes(data); }