Can you see what's off?
Thanks.
First, the interface declarations:
using System.Xml;
namespace EVE_API.Interfaces
{
public interface IAPIResponse
{
IAPIResponse ParseResponse(XmlDocument response);
}
}
using System.Collections.Generic;
using System.Xml;
namespace EVE_API.Interfaces
{
public interface IEveAPI
{
string ApiName { get; }
Dictionary<string,string> Arguments { get; set; }
}
}
Now, the response class:
using System;
using System.Xml;
using EVE_API.Interfaces;
namespace EVE_API.APIs.Miscelleneous
{
public class ServerStatusResponse : IAPIResponse
{
private string _version;
private DateTime _currentTime;
private bool _open;
private int _onlinePlayers;
private DateTime _cachedUntil;
public string Version { get { return _version; } }
public DateTime CurrentTime { get { return _currentTime; } }
public bool Open { get { return _open; } }
public int OnlinePlayers { get { return _onlinePlayers; } }
public DateTime CachedUntil { get { return _cachedUntil; } }
private ServerStatusResponse(XmlDocument responseDocument)
{
_version = responseDocument.SelectSingleNode("/eveapi").Attributes["version"].Value;
DateTime dtTry = DateTime.MinValue;
DateTime.TryParse(responseDocument.SelectSingleNode("/eveapi/currentTime").Value, out dtTry);
if (dtTry != DateTime.MinValue)
{
_currentTime = dtTry;
}
else
{
throw new MalformedResponseException("Could not find currentTime in response from SeverStatus API.");
}
bool boolTry;
bool.TryParse(responseDocument.SelectSingleNode("/eveapi/result/open").Value, out boolTry);
_open = boolTry;
int intTry;
int.TryParse(responseDocument.SelectSingleNode("/eveapie/result/onlinePlayers").Value, out intTry);
if (intTry > int.MinValue)
{
_onlinePlayers = intTry;
}
else
{
throw new MalformedResponseException("Could not find onlinePlayers in response from SeverStatus API.");
}
dtTry = DateTime.MinValue;
DateTime.TryParse(responseDocument.SelectSingleNode("/eveapi/result/cachedUntil").Value, out dtTry);
if (dtTry != DateTime.MinValue)
{
_cachedUntil = dtTry;
}
else
{
throw new MalformedResponseException("Could not find cachedUntil in response from SeverStatus API.");
}
}
public IAPIResponse ParseResponse(XmlDocument response)
{
return new ServerStatusResponse(response);
}
}
}
And now the class trying to wrap all the above together
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Xml;
using EVE_API.Interfaces;
namespace EVE_API
{
public class APICaller<T> where T : IAPIResponse
{
public APICaller()
{
}
public T ExecuteAPICall<T>(IEveAPI api)
{
List<string> urlParts = new List<string>();
urlParts.Add(api.ApiName);
if (api.Arguments != null)
{
urlParts.Add("?");
var keys = api.Arguments.Keys;
foreach (string key in keys)
{
urlParts.Add(key);
urlParts.Add("=");
urlParts.Add(api.Arguments[key]);
urlParts.Add("&");
}
urlParts.RemoveAt(urlParts.Count - 1);
}
string completeUrl = urlParts.ToString();
WebRequest request = WebRequest.Create(completeUrl);
WebResponse response = request.GetResponse();
XmlDocument document = new XmlDocument();
document.Load(response.GetResponseStream());
IAPIResponse apiResponse = new (IAPIResponse)T();
return apiResponse.ParseResponse(document);
}
}
}