Overview
We publish an extensive documentation page for the Spira 6.0 REST API that describes the resources, methods, requests, bodies and sample JSON/XML representations.
However many customers have asked for some C# / .NET samples of the REST API in action. So we have created a simple repository of sample code for accessing the REST API in C#.
Sample Code
Here's a snippit of some sample code for connecting to the Spira REST API and authenticating:
public RemoteCredentials Connection_Authenticate(string userName, string apiKey, string plugInName)
{
RestRequest request = new RestRequest("Connection_Authenticate2");
request.Credential = new System.Net.NetworkCredential();
request.Credential.UserName = userName;
request.Credential.Password = apiKey;
request.Method = "GET";
request.Url = baseUrl + "/" + "users";
request.Headers.Add(new RestHeader() { Name = StandardHeaders.Content_Type, Value = "application/json" });
RestClient restClient = new RestClient(request);
RestResponse response = restClient.SendRequest();
if (!response.IsErrorStatus && response.Headers.FirstOrDefault(h => h.Name == Properties.Resources.Rest_StatusCode).Value == "200 OK" && !String.IsNullOrEmpty(response.Body))
{
RemoteUser remoteUser = JsonConvert.DeserializeObject<RemoteUser>(response.Body);
RemoteCredentials remoteCredentials = new RemoteCredentials();
remoteCredentials.UserId = remoteUser.UserId.Value;
remoteCredentials.UserName = userName;
remoteCredentials.PlugInName = plugInName;
remoteCredentials.IsSystemAdmin = remoteUser.Admin;
remoteCredentials.ApiKey = apiKey;
return remoteCredentials;
}
return null;
}
}
The full sample code GitHub repository can be found here: