Created
December 1, 2011 17:42
-
-
Save davidlaym/1418491 to your computer and use it in GitHub Desktop.
Create a project in Youtrack via C# using REST API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Net; | |
using System.Collections.Specialized; | |
using System.Text; | |
using System.IO; | |
public class YouTrackCreateProject | |
{ | |
public class YouTrackClient { | |
private readonly string _baseUrl; | |
private readonly CookieContainer _cookies = new CookieContainer(); | |
public YouTrackClient(string baseUrl, string userName, string password) { | |
_baseUrl = baseUrl; | |
WL("Authenticating user:"+userName); | |
Authenticate(userName, password); | |
} | |
private void Authenticate(string username, string password){ | |
string resource = "/rest/user/login"; | |
string dataTemplate = "login={0}&password={1}"; | |
Request("POST",resource,string.Format(dataTemplate,username,password)); | |
} | |
private void Request(string method, string resource, string data) | |
{ | |
HttpWebRequest HttpWReq; | |
HttpWebResponse HttpWResp; | |
HttpWReq = (HttpWebRequest)WebRequest.Create(_baseUrl+resource); | |
HttpWReq.Method = method; | |
HttpWReq.CookieContainer = _cookies; | |
if(!string.IsNullOrEmpty(data)) | |
SendData(HttpWReq,data); | |
HttpWResp = (HttpWebResponse)HttpWReq.GetResponse(); | |
WL("Status Code: "+HttpWResp.StatusCode.ToString()); | |
WL("Response Text: "+ParseResult(HttpWResp)); | |
} | |
private void SendData(HttpWebRequest req, string postData) { | |
ASCIIEncoding encoding=new ASCIIEncoding(); | |
byte[] data = encoding.GetBytes(postData); | |
req.ContentType="application/x-www-form-urlencoded"; | |
req.ContentLength = data.Length; | |
Stream rs= req.GetRequestStream(); | |
rs.Write(data,0,data.Length); | |
rs.Close(); | |
} | |
private string ParseResult(HttpWebResponse resp){ | |
StringBuilder response = new StringBuilder(); | |
using(StreamReader reader = new StreamReader(resp.GetResponseStream())) | |
{ | |
while (reader.Peek() >= 0) | |
{ | |
response.AppendLine(reader.ReadLine()); | |
} | |
} | |
return response.ToString(); | |
} | |
public void CreateProject(string Id, string name,string desc, string owner) | |
{ | |
string dataTemplate = "projectName={0}&startingNumber=1&projectLeadLogin={1}&description={2}"; | |
string resource = "/rest/admin/project/"+Id+"?"+string.Format(dataTemplate,name,owner,desc); | |
Request("PUT",resource,string.Empty); | |
} | |
} | |
public static void Main() | |
{ | |
try | |
{ | |
YouTrackClient client = new YouTrackClient("http://url:port","root","secretpassword"); | |
client.CreateProject("TEST","TEST+PROJECT","TEST","root"); | |
} | |
catch (Exception e) | |
{ | |
string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString()); | |
Console.WriteLine(error); | |
} | |
finally | |
{ | |
Console.Write("Press any key to continue..."); | |
Console.ReadKey(); | |
} | |
} | |
private static void WL(object text, params object[] args) | |
{ | |
Console.WriteLine(text.ToString(), args); | |
} | |
private static void RL() | |
{ | |
Console.ReadLine(); | |
} | |
private static void Break() | |
{ | |
System.Diagnostics.Debugger.Break(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment