Michael Sanford
1 post
|
Are there any samples showing best practices for using the REST API from a .net application? Is it possible to use XML-RPC.net?
Thanks!
|
Joshua Frappier
Administrator
241 post(s)
|
Michael,
We recently added an example in C# provided to us by Lance Sun. You can find the example here:
http://unfuddle.com/docs/api/code_examples
It is a fairly basic example showing how to establish a connection and retrieve a resource in XML format. As with all our examples, if anyone wants to provide more robust versions, we would certainly be glad to post them.
I hope that helps and have a Happy New Year!
|
John Newcombe
2 post(s)
|
A little late I know but here’s a snippet to get you going… hope it helps.
John Newcombe
http://www.waymex.co.uk
using System;
using System.IO;
using System.Xml;
using System.Net;
using System.Text;
public partial class Ticket : System.Web.UI.Page
{
private const string API_URL = "http://<domain>.unfuddle.com/api/v1/";
private const string API_USER = "user";
private const string API_PASSWORD = "password";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//get the collection of projects
GetProjects();
//get the tikets for a given project ID
GetTickets(25128);
//create a new ticket
SetTicket(25128, "Test API Ticket", "This is a test API Ticket for web users", 5);
}
public void GetProjects()
{
string method = "projects.xml";
string result = Get(API_USER, API_PASSWORD, API_URL + method);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(result);
xmlDoc.Save(@"e:\AJAXEnabledWebSite1\projects.xml");
}
public void GetTickets(int projectId)
{
string method = @"projects/" + projectId.ToString() + "/tickets.xml";
string result = Get(API_USER, API_PASSWORD, API_URL + method);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(result);
xmlDoc.Save(@"e:\AJAXEnabledWebSite1\tickets.xml");
}
public void SetTicket(int projectId,string summary, string description, int priority)
{
string method = @"projects/" + projectId.ToString() + "/tickets.xml";
//create the xml
string xml = String.Format(@"<ticket>
<description>{0}</description>
<priority>{1}</priority>
<project-id>{2}</project-id>
<summary>{3}</summary>
</ticket>",
description,
priority.ToString(),
projectId.ToString(),
summary);
string result = Post(API_USER, API_PASSWORD, API_URL + method, xml);
if (result == string.Empty)
{
//all OK
}
else
{
//a problem has occured
}
}
private string Post(string username, string password, string url, string xml)
{
string basicAuth = Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Format("{0}:{1}", username, password)));
//create the web request
WebRequest request = WebRequest.Create(url);
request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
request.Headers.Add("Authorization", "Basic " + basicAuth);
request.ContentType = "application/xml";
request.ContentLength = xml.Length;
request.Method = "POST";
//get the request stream
Stream stream = request.GetRequestStream();
//convert the xml to an array of bytes
System.Text.Encoding enc = System.Text.Encoding.ASCII;
byte[] xmlArray = enc.GetBytes(xml);
//write the array to the request straem
stream.Write(xmlArray, 0, xmlArray.Length);
//get the response
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
return reader.ReadToEnd();
}
private string Get(string username, string password, string url)
{
string basicAuth = Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Format("{0}:{1}", username, password)));
WebRequest request = WebRequest.Create(url);
request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
request.Headers.Add("Authorization", "Basic " + basicAuth);
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
return reader.ReadToEnd();
}
}
|
John Newcombe
2 post(s)
|
I guess this is mentioned elsewhere. However, whilst finding my feet with this stuff, I realised that it is best to put the description and summary text in a CDATA sections.
|
LogicalGeekBoy
1 post
|
Take a look at Unfuddle.NET that I’m putting together. Comments welcome.
|