Wiki

Case Status Kiln
Register Log In

Wiki

 
"How To" Guides»How To Access An External API
  • RSS Feed

Last modified on 5/2/2012 1:39 PM by User.

Tags:

How To Access An External API

One of the benefits of plugins is they can be used to connect FogBugz with other applications through webservices. As .NET assemblies, plugins can use the standard System.Net.WebRequest and System.Net.WebResponse classes to fetch data via HTTP. For example, a plugin can implement the IPluginBugCommit interface to perform a lookup in another application whenever a case is committed:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
using System;
using System.Collections.Generic;
using System.Text;

/* FogBugz namespaces-- make sure you add the neccessary assembly references to
* the following DLL files contained in C:\Program Files\FogBugz\Website\bin\
* FogBugz.dll, FogCreek.Plugins.dll, FogCreek.Plugins.InterfaceEvents.dll */
using FogCreek.FogBugz.Plugins;
using FogCreek.FogBugz.Plugins.Interfaces;
using FogCreek.FogBugz;

namespace FogCreek.Plugins.HelloWorld
{
    /* Class Declaration: Inherit from Plugin, implement interfaces
    * IPluginPageDisplay and IPluginExtrasMenu */
    public class PostEditsSomewhere : Plugin, IPluginBugCommit
    {
        /* Constructor: We'll just initialize the inherited Plugin class, which
        * takes the passed instance of CPluginApi and sets its "api" member variable. */
        public PostEditsSomewhere(CPluginApi api)
            : base(api)
        {
        }

        #region IPluginBugCommit Members

        public void BugCommitAfter(CBug bug, BugAction nBugAction, CBugEvent bugevent, bool fPublic)
        {
            return;
        }

        public void BugCommitBefore(CBug bug, BugAction nBugAction, CBugEvent bugevent, bool fPublic)
        {
            // the base URL of the api you'll access
            string url = "http://address.of.webservice/submit.asp";

            // load the Person object who commited this bugevent
            CPerson author = api.Person.GetPerson(bugevent.ixPerson);

            // build up a request to your external API
            string sRequestUrl = string.Format(@"{0}?ixBug={1}&sEvent={2}&sAuthor={3}",
            url,
            bug.ixBug.ToString(),
            bugevent.s,
            author.sFullName
            );
            // get the whole text of the response
            string sResponse = FullHtmlFromURL(sRequestUrl);
            // test for sucess, etc.
            if (sResponse != "1")
            {
                // do something
            }
        }

        public void BugCommitRollback(CBug bug, BugAction nBugAction, bool fPublic)
        {
            return;
        }

        #endregion

        public static string FullHtmlFromURL(string url)
        {
            System.Net.WebRequest httpRequest = System.Net.WebRequest.Create(url);
            System.Net.WebResponse httpResponse = httpRequest.GetResponse();
            System.IO.Stream streamResponse = httpResponse.GetResponseStream();
            System.IO.StreamReader reader = new System.IO.StreamReader(streamResponse);
            // get the whole text of the response
            return reader.ReadToEnd();
        }
    }
}