This question is very similar to this one. That question asks if it's worth the trouble to create classes to deal with an api xml response. My question is the opposite: is it worth it to create classes to build an api request vs simply hard-coding the xml?
This is an example of the latter:
string browseService = "http://Mydomain/SearchSvc/CVWebService.svc/DoBrowse";
var browseRequest = string.Format("<databrowse_BrowseRequest opType=\"{0}\">" +
"<entity clientName=\"{1}\" applicationId=\"{2}\" clientId=\"{3}\" subclientId=\"{4}\" backupsetId=\"{5}\" instanceId=\"{6}\"/>" +
"<paths path=\"{7}\"/><options showDeletedFiles=\"1\" restoreIndex=\"1\"/><mode mode=\"2\"/><queries type=\"1\" queryId=\"countQuery\">" +
"<aggrParam aggrType=\"4\" field=\"0\"/></queries><queries type=\"0\" queryId=\"dataQuery\">" +
"<dataParam><paging firstNode=\"0\" skipNode=\"0\" pageSize=\"15\"/><sortParam ascending=\"1\">" +
"<sortBy val=\"38\"/><sortBy val=\"0\"/></sortParam></dataParam></queries></databrowse_BrowseRequest>",
"2", "testServer123", "29", "1234", "5678", "9123", "1", "/tmp/somefile.txt");
if (browseClientResp.StatusCode == HttpStatusCode.OK)
{
XmlDocument xmlDoc = new XmlDocument();
var test = browseClientResp.GetResponseStream();
xmlDoc.Load(test);
}
And here is an example of the classes I created for the former case:
[XmlRoot("databrowse_BrowseRequest")]
public class CommVaultBrowse
{
[XmlAttribute("opType")]
public int OpType { get; set; }
[XmlElement("session")]
public Session Session { get; set; }
[XmlElement("entity")]
public Entity Entity { get; set; }
[XmlElement("paths")]
public List<Paths> Paths { get; set; }
[XmlElement("timeRange")]
public TimeRange TimeRange { get; set; }
[XmlElement("options")]
public Options Options { get; set; }
[XmlElement("mode")]
public Mode Mode { get; set; }
[XmlElement("queries")]
public List<Queries> Queries { get; set; }
}
public class Session
{
[XmlAttribute("sessionId")]
public int SessionId { get; set; }
}
public class Entity
{
[XmlAttribute("clientName")]
public string ClientName { get; set; }
[XmlAttribute("applicationId")]
public int ApplicationId { get; set; }
[XmlAttribute("clientId")]
public int ClientId { get; set; }
[XmlAttribute("subclientId")]
public string SubclientId { get; set; }
[XmlAttribute("backupSetId")]
public int BackupSetId { get; set; }
[XmlAttribute("instanceId")]
public int InstanceId { get; set; }
}
public class Paths
{
[XmlAttribute("path")]
public string Path { get; set; }
}
public class TimeRange
{
[XmlAttribute("toTime")]
public int ToTime { get; set; }
[XmlAttribute("fromTime")]
public int FromTime { get; set; }
}
public class Options
{
[XmlAttribute("showDeletedFiles")]
public byte ShowDeletedFiles { get; set; }
[XmlAttribute("restoreIndex")]
public byte RestroeIndex { get; set; }
}
public class Mode
{
//the only valid value according to Commvault documentation is 2
[XmlAttribute("mode")]
public int ModeId { get; set; }
}
public class Queries
{
[XmlAttribute("type")]
public int Type { get; set; }
[XmlAttribute("queryId")]
public int QueryId { get; set; }
[XmlElement("aggrParam")]
public AggregateParams AggrParam { get; set; }
[XmlElement("dataParam")]
public DataParams DataParam { get; set; }
}
public class AggregateParams
{
[XmlAttribute("aggrType")]
public int AggrType { get; set; }
[XmlAttribute("field")]
public int Field { get; set; }
}
public class DataParams
{
[XmlElement("paging")]
public Paging Paging { get; set; }
[XmlElement("sortParam")]
public SortParam SortParam { get; set; }
}
public class Paging
{
[XmlAttribute("firstNode")]
public int FirstNode { get; set; }
[XmlAttribute("skipNode")]
public int SkipNode { get; set; }
[XmlAttribute("pageSize")]
public int PageSize { get; set; }
}
public class SortParam
{
[XmlAttribute("ascending")]
public int Ascending { get; set; }
[XmlElement("sortBy")]
public List<SortBy> SortBys { get; set; }
}
public class SortBy
{
[XmlAttribute("val")]
public int Val { get; set; }
}
I realized after the fact that instantiating an object of type CommVaultBrowse could get kinda complicated:
var test = new CommVaultBrowse
{
OpType = 2,
Session = new Session
{
SessionId = 1234
},
Entity = new Entity
{
ApplicationId = 29,
BackupSetId = 222,
ClientId = 333,
ClientName = "testServer123",
InstanceId = 1,
SubclientId = "subclient1"
},
Paths = new List<Paths>
{
new Paths
{
Path = "\\C:\\Users\\Administrator\\Desktop\\"
}
},
TimeRange = new TimeRange
{
FromTime = 1378180800,
ToTime = 1378439999
},
Options = new Options
{
RestroeIndex = 1,
ShowDeletedFiles = 0
},
Mode = new Mode
{
ModeId = 2
},
Queries = new List<Queries>
{
new Queries
{
Type = 0, QueryId = 1,
AggrParam = new AggregateParams
{
AggrType = 0,
Field = 1
}
},
new Queries
{
Type = 0,
QueryId = 1,
DataParam = new DataParams
{
Paging = new Paging
{
FirstNode = 1,
PageSize = 20,
SkipNode = 0
},
SortParam = new SortParam
{
Ascending = 1,
SortBys = new List<SortBy>
{
new SortBy
{
Val = 1
},
new SortBy
{
Val = 2
}
}
}
}
}
}
};
Is there a standard way of building these types of requests?