Consuming the Service - a C# .NET example based on xml streaming

0 Likes
over 3 years ago

A small simple C# test

ServerProxy sp = new ServerProxy();

sp.Logon("Administrator", "Administrator");

UserProxy up = sp.UserProxy;

Dictionary<String, String>[] allUsers = up.GetUsers();

sp.Logoff();

 

 

The code for this example is found below. it comprises a small proxy representing the (Micro Focus Hub) REST server, a proxy representing the User interface, and an xml streaming helper. A logged on proxy manages the session GUID for the user.

Each User object returned is described as a Dictionary of string key's to string values.

The keys are property names of the user schema on the StarTeam platform, their values are String representations of their matching underlying metadata types.

Note that every Dictionary contains a generic key value pair called a "ResourceID".

ResourceIDs are unique server wide descriptors of the underlying artifacts they represent.

So, a User ResourceID is the StarTeam UserID, a Project ResourceID is a projectID, a ChangeRequest ResourceID is a ViewMemberID, etc...

The combination tuple {ResourceID, TypeID} can pretty much locate any starteam artifact in the system.

 

A proxy class that abstracts out the Micro Focus Hub REST Server

 

using System;
using System.Collections.Generic;

namespace MicroFocusHubREST.Client
{
public class ServerProxy
{
private string m_host = "localhost";
private int m_port = 8080;

string m_sessionGUID = null;

public string Host
{
get
{
return m_host;
}
set
{
m_host = value;
}
}

public int Port
{
get
{
return m_port;
}
set
{
m_port = value;
}
}

public string URL
{
get
{
return "http://" Host ":" Port "/MicroFocusHubRESTService/rest/";
}
}

public string GUID
{
get
{
return m_sessionGUID;
}
}

public bool Connected
{
get
{
return m_sessionGUID != null;
}
}

public void Logon(string userName, string password = "")
{
if (Connected)
return;
m_sessionGUID = Utilities.SendRequest(URL, "users/logon?username=" userName "&password=" password, "GET", null);
}

public void Logoff()
{
if (!Connected)
return;
Utilities.SendRequest(URL, "users/logoff;sessionid=" GUID, "GET", null);
m_sessionGUID = null;
}

public UserProxy UserProxy
{
get
{
if (!Connected)
return null;
return new UserProxy(this);
}
}


}

 

A Proxy class that abstract out User queries

using System;
using System.Xml.Serialization;
using System.Collections.Generic;

namespace MicroFocusHubREST.Client
{
public class UserProxy
{
private ServerProxy m_server;
internal UserProxy(ServerProxy sp)
{
m_server = sp;
}

public Dictionary<string, string>[] GetUsers()
{
string uri = "users;sessionid=" m_server.GUID;
string users = Utilities.SendRequest(m_server.URL, uri, "GET", null);
return Utilities.Transform(users, typeof(Resources));
}
}

}

 

 

 

and a utility class to abstract out client server XML streaming

using System;
using System.Net;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Text;
using System.Collections.Generic;

namespace MicroFocusHubREST.Client
{
class Utilities
{
public static string SendRequest(string basicURL, string request, string method, string body)
{
// Construct URL
Uri uri = new Uri(basicURL request);

WebRequest webRequest = WebRequest.Create(uri);
webRequest.Method = method;
webRequest.ContentType = "application/xml; charset=UTF-8";

if (body != null)
{
using (var streamWriter = new StreamWriter(webRequest.GetRequestStream()))
{
streamWriter.Write(body);
streamWriter.Flush();
streamWriter.Close();
}
}

WebResponse webResponse = null;
string retVal = null;
bool succeeded = true;
try
{
webResponse = webRequest.GetResponse();
}
catch (WebException ex)
{
succeeded = false;
webResponse = ex.Response;
}
finally
{
if (webResponse != null)
{
var responseStream = webResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(responseStream, encode);
retVal = readStream.ReadToEnd();
readStream.Close();
responseStream.Close();
webResponse.Close();
}
}
if (!succeeded)
throw new SystemException(retVal);
string header = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
if (retVal.StartsWith(header))
return retVal.Substring(header.Length);
return retVal;
}

public static string Transform(object o)
{
if (o == null)
return null;
MemoryStream ms = new MemoryStream();
XmlSerializer srlzr = null;
string s = null;
try
{
srlzr = new XmlSerializer(o.GetType());
using (StringWriter sww = new StringWriter())
using (XmlWriter writer = XmlWriter.Create(sww))
{
srlzr.Serialize(writer, o);
s = sww.ToString();
}
}
finally
{
ms.Close();
}
return s;
}

public static Dictionary<string, string>[] Transform(string s, System.Type t)
{
if (s == null || s.Length == 0)
return null;
MemoryStream ms = new MemoryStream();
var bytes = Encoding.UTF8.GetBytes(s);
ms.Write(bytes, 0, bytes.Length);
ms.Position = 0;

XmlSerializer srlzr = null;
object o = null;
try
{
srlzr = new XmlSerializer(t);
o = srlzr.Deserialize(ms);
}
finally
{
ms.Close();
}
if (o is Resources)
return ((Resources)o).Payload;
else if (o is Resource)
return new Dictionary<string, string>[1] {((Resource)o).Payload};
throw new System.Exception(o.ToString());
}
}


}

Labels:

How To-Best Practice
Comment List
Anonymous
Related Discussions
Recommended