//  home   //  advanced search   //  news   //  categories   //  sql build chart   //  downloads   //  statistics
 ASP FAQ 
Home
ASP FAQ Tutorials

   8000XXXX Errors
   Alerts
   ASP.NET 2.0
      .NET Provider Pattern
      Basic Language Constructs
      Themes & Skins (web)
      Tips & Techniques
      XML Serialization
   Classic ASP 1.0
   Databases
   General Concepts
   Search Engine Optimization (SEO)

Contact Us
Site Map

Search

Web
aspfaq.com
tutorials.aspfaq.com
asp.net2.aspfaq.com

ASP FAQ Tutorials :: ASP.NET 2.0 :: FogBugz CSharp API Wrapper


FogBugz CSharp API Wrapper

FogBugz is a great defect tracking system.  It provides an API to manage the defects programatically, but it is an http API which obviously requires some "wrapping" before it can be used natively in CSharp.

Using the FogBugz sample APITesting project from the FogCreek blog, I wrapped the functionality in a new class called FBApi which handles all of the internals of the API and let's you use the following syntax.  Say you wanted to get a list of projects, you would do this:

CSharp

FBApi api = new FBApi("username", "password");
string result = api.ListProjects();
// or
XmlDocument xresult = api.XListProjects();

VB

Dim api As FBApi =  New FBApi("username","password") 
Dim result As String =  api.ListProjects() 
' or
Dim xresult As XmlDocument =  api.XListProjects()

This would result in the following XML being returned:

<?xml version="1.0" encoding="UTF-8"?>
<response>
  <projects>
    <project>
      <ixProject>3</ixProject>
      <sProject><![CDATA[Project 1]]></sProject>
      <ixPersonOwner>2</ixPersonOwner>
      <sPersonOwner><![CDATA[John Doe]]></sPersonOwner>
      <sEmail><![CDATA[email@domain.com]]></sEmail>
      <sPhone><![CDATA[212-555-1234]]></sPhone>
      <fInbox>false</fInbox>
      <ixGroup>1</ixGroup>
      <iType>1</iType>
      <sGroup><![CDATA[Internal]]></sGroup>
    </project>
    <project>
      <ixProject>2</ixProject>
      <sProject><![CDATA[Inbox]]></sProject>
      <ixPersonOwner>2</ixPersonOwner>
      <sPersonOwner><![CDATA[John Doe]]></sPersonOwner>
      <sEmail><![CDATA[email@domain.com]]></sEmail>
      <sPhone><![CDATA[212-555-1234]]></sPhone>
      <fInbox>true</fInbox>
      <ixGroup>1</ixGroup>
      <iType>1</iType>
      <sGroup><![CDATA[Internal]]></sGroup>
    </project>
  </projects>
</response>

This is the interface for the FBApi class.

CSharp

    interface IFBApi
    {
        event FBApi.ApiEvent ApiCalled;
        void Login(string email, string password);
        void Logout();
        string Cmd(string cmd);
        string Cmd(string cmd, Dictionary<string, string> args);
        string Cmd(string cmd, Dictionary<string, string> args, Dictionary<string, 
            byte[]>[] files);
        string ListCategories();
        string ListFixFors(int ixProject);
        string ListIntervals(DateTime dtStart, DateTime dtEnd);
        string ListProjects(bool onlyWritable);
        string ListProjects();
        string NewInterval(int ixBug, DateTime dtStart, DateTime dtEnd);
        string Search(string q, string cols);
        string Search(string q, string cols, int max);
        string Search(string q);
        string StartWork(int ixBug);
        string StopWork();
        string Subscribe(int ixBug, int ixWikiPage);
        string Unsubscribe(int ixBug, int ixWikiPage);
        string View(int ixBug);
        XmlDocument XCmd(string cmd);
        XmlNodeList XCmd(string cmd, string xPath);
        XmlNodeList XCmd(string cmd, Dictionary<string, string> args, string xPath);
        XmlDocument XCmd(string cmd, Dictionary<string, string> args, 
            Dictionary<string, byte[]>[] files);
        XmlDocument XCmd(string cmd, Dictionary<string, string> args);
        XmlNodeList XCmd(string cmd, Dictionary<string, string> args, 
            Dictionary<string, byte[]>[] files, string xPath);
        XmlNodeList XListCategories();
        XmlNodeList XListFixFors(int ixProject);
        XmlNodeList XListIntervals(DateTime dtStart, DateTime dtEnd, int ixBug);
        XmlNodeList XListIntervals(DateTime dtStart, DateTime dtEnd);
        XmlNodeList XListProjects();
        XmlNodeList XListProjects(bool onlyWritable);
        XmlDocument XNewInterval(int ixBug, DateTime dtStart, DateTime dtEnd);
        XmlNodeList XSearch(string q, string cols, int max);
        XmlNodeList XSearch(string q);
        XmlNodeList XSearch(string q, string cols);
        XmlDocument XStartWork(int ixBug);
        XmlDocument XStopWork();
        XmlDocument XSubscribe(int ixBug, int ixWikiPage);
        XmlDocument XUnsubscribe(int ixBug, int ixWikiPage);
        XmlDocument XView(int ixBug);
    }

VB

'Error: Converting Declarations and Fields 


    Interface IFBApi
        event FBApi.ApiEvent ApiCalled
        void Login(String email, String password)
        void Logout()
        String Cmd(String cmd)
        String Cmd(String cmd, Dictionary<string, string> args)
        String Cmd(String cmd, Dictionary<string, string> args, Dictionary<string, 
            byte()>() files)
        String ListCategories()
        String ListFixFors(Integer ixProject)
        String ListIntervals(DateTime dtStart, DateTime dtEnd)
        String ListProjects(Boolean onlyWritable)
        String ListProjects()
        String NewInterval(Integer ixBug, DateTime dtStart, DateTime dtEnd)
        String Search(String q, String cols)
        String Search(String q, String cols, Integer max)
        String Search(String q)
        String StartWork(Integer ixBug)
        String StopWork()
        String Subscribe(Integer ixBug, Integer ixWikiPage)
        String Unsubscribe(Integer ixBug, Integer ixWikiPage)
        String View(Integer ixBug)
        XmlDocument XCmd(String cmd)
        XmlNodeList XCmd(String cmd, String xPath)
        XmlNodeList XCmd(String cmd, Dictionary<string, string> args, String xPath)
        XmlDocument XCmd(String cmd, Dictionary<string, string> args, 
            Dictionary<string, Byte()>() files)
        XmlDocument XCmd(String cmd, Dictionary<string, string> args)
        XmlNodeList XCmd(String cmd, Dictionary<string, string> args, 
            Dictionary<string, Byte()>() files, String xPath)
        XmlNodeList XListCategories()
        XmlNodeList XListFixFors(Integer ixProject)
        XmlNodeList XListIntervals(DateTime dtStart, DateTime dtEnd, int ixBug)
        XmlNodeList XListIntervals(DateTime dtStart, DateTime dtEnd)
        XmlNodeList XListProjects()
        XmlNodeList XListProjects(Boolean onlyWritable)
        XmlDocument XNewInterval(Integer ixBug, DateTime dtStart, DateTime dtEnd)
        XmlNodeList XSearch(String q, String cols, Integer max)
        XmlNodeList XSearch(String q)
        XmlNodeList XSearch(String q, String cols)
        XmlDocument XStartWork(Integer ixBug)
        XmlDocument XStopWork()
        XmlDocument XSubscribe(Integer ixBug, Integer ixWikiPage)
        XmlDocument XUnsubscribe(Integer ixBug, Integer ixWikiPage)
        XmlDocument XView(Integer ixBug)
    End Interface

As you can see, there are strongly typed API methods for many of the common things that youmight want to do with the API, or, if there is not a predefined API, you can always use one of he various overloaded Cmd and XCmd methods to call any of the commands in the FogBugz API as follows:

CSharp

FBApi api = new FBApi("username", "password");

Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("ixBug", "245");

string results = api.Cmd("listCheckins", args);
// or
XmlDocument xresults = api.XCmd("listCheckins", args);

VB

'Error: Converting Declarations and Fields 

FBApi api = New FBApi("username", "password")
 
Dictionary<string, string> args = New Dictionary<string, string>()
args.Add("ixBug", "245")
 
String results = api.Cmd("listCheckins", args)
' or
XmlDocument xresults = api.XCmd("listCheckins", args)

Using this functionality, the following code get's simplified...

CSharp

private void cmdListIntervals_Click(object sender, EventArgs e)
{
    Dictionary<string, string> args = new Dictionary<string, string>();
    args.Add("cmd", "listIntervals");
    args.Add("token", token);

    if (txtdtstart.Text.Length > 0)
        args.Add("dtStart", txtdtstart.Text);
    else
        args.Add("dtStart", dateTimePickerStart.Value.ToUniversalTime().ToString("s") + "Z");

    if (txtdtend.Text.Length > 0)
        args.Add("dtEnd", txtdtend.Text);
    else
        args.Add("dtEnd", dateTimePickerEnd.Value.ToUniversalTime().ToString("s") + "Z");

    CallRESTAPIFiles(txtURL.Text, args, null);
}

VB

'Error: Converting Declarations and Fields 

Private  Sub cmdListIntervals_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dictionary<string, string> args = New Dictionary<string, string>()
    args.Add("cmd", "listIntervals")
    args.Add("token", token)
 
    If txtdtstart.Text.Length > 0 Then
        args.Add("dtStart", txtdtstart.Text)
    Else 
        args.Add("dtStart", dateTimePickerStart.Value.ToUniversalTime().ToString("s") + "Z")
    End If
 
    If txtdtend.Text.Length > 0 Then
        args.Add("dtEnd", txtdtend.Text)
    Else 
        args.Add("dtEnd", dateTimePickerEnd.Value.ToUniversalTime().ToString("s") + "Z")
    End If
 
    CallRESTAPIFiles(txtURL.Text, args, Nothing)
End Sub

 Using the FBApi, this code now looks like this:

CSharp

private void cmdListIntervals_Click(object sender, EventArgs e)
{
    DateTime dtStart = dateTimePickerStart.Value;
    if (!String.IsNullOrEmpty(txtdtstart.Text)) 
        dtStart = DateTime.Parse(txtdtstart.Text);

    DateTime dtEnd = dateTimePickerEnd.Value;
    if (!String.IsNullOrEmpty(txtdtend.Text)) 
        dtEnd = DateTime.Parse(txtdtend.Text);

    Api.ListIntervals(dtStart, dtStop);
}

VB


Private  Sub cmdListIntervals_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim dtStart As DateTime =  dateTimePickerStart.Value 
    If Not String.IsNullOrEmpty(txtdtstart.Text) Then
        dtStart = DateTime.Parse(txtdtstart.Text)
    End If
 
    Dim dtEnd As DateTime =  dateTimePickerEnd.Value 
    If Not String.IsNullOrEmpty(txtdtend.Text) Then
        dtEnd = DateTime.Parse(txtdtend.Text)
    End If
 
    Api.ListIntervals(dtStart, dtStop)
End Sub

Download the new APITester and integrated FogBugz API CSharp wrapper class (with source code).

Please send any comments or suggestions about this article to auto1 -at- kalliopi dot com.

'----------------------------------------------------------------
' Converted from C# to VB .NET using CSharpToVBConverter(1.2).
' Developed by: Kamal Patel (http://www.KamalPatel.net)
'----------------------------------------------------------------

Related Articles

.NET Provider Pattern
.NET Streams
Basic Language Constructs
OnTime 2008 API C# Wrapper
Themes & Skins (web)
Tips & Techniques
XML Serialization

 

 


Created: 6/11/2008 | Last Updated: 6/11/2008 | broken links | helpful | not helpful | statistics
© Copyright 2006, UBR, Inc. All Rights Reserved. (793)

 

Copyright 1999-2006, All rights reserved.
Finding content
Finding content.  An error has occured...