Sample code
In the box below is sample code that you can paste and edit in your development tool. You need to modify specific parts of the code before running it. The details for modifying specific parts of the code are in the next section.
using System; using System.Net.Http; using System.Net.Http.Headers;
namespace EMA2Test { public class AuthenticationData { public string Username { get; set; } public string Password { get; set; } }
public class AuthenticationResponse { public string AccessToken { get; set; } public string RefreshToken { get; set; } }
public class CompanyUserDetailsResponse { public string Id { get; set; } public string Name { get; set; } public int Type { get; set; } }
public class UserDetailsReponse { public string Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; } public string Description { get; set; } public string PhoneNumber { get; set; } public bool TwoFactorEnabled { get; set; } public int Status { get; set; } public CompanyUserDetailsResponse Company { get; set; } }
public class UsageReportRequest { public DateTime From { get; set; } public DateTime To { get; set; } public int Skip { get; set; } public int Take { get; set; } }
public class UsageReportProductResponse { public string ProductCode { get; set; } public string Name { get; set; } public int LicenseType { get; set; } public long SeatDays { get; set; } }
public class UsageReportCompanyResponse { public UsageReportProductResponse[] Products { get; set; } public string CompanyId { get; set; } public string CompanyName { get; set; } public long TotalUsage { get; set; } }
public class UsageReportResponse { public UsageReportCompanyResponse[] Companies { get; set; } }
internal static class Program { private static HttpClient client = new HttpClient(); private static string BaseURL = "https://mspapi.eset.com/";
static void Main(string[] args) { client.BaseAddress = new Uri(BaseURL); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json"));
AuthenticationResponse authenticationResponse = Login();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authenticationResponse.AccessToken);
UserDetailsReponse userDetails = GetUserDetails(); Console.WriteLine("User {0} is known under ID: {1}", userDetails.FirstName, userDetails.Id); Console.ReadLine();
UsageReportResponse usageReport = GetUsageReport(); client.Dispose(); }
static AuthenticationResponse Login() { var auth = new AuthenticationData { Username = "*****", Password = "*****" }; HttpResponseMessage response = client.PostAsJsonAsync("/api/Token/Get", auth).Result; if (response.IsSuccessStatusCode) { return response.Content.ReadAsAsync<AuthenticationResponse>().Result; } else { Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); } return null; }
static UserDetailsReponse GetUserDetails() { var response = client.GetAsync("/api/User/Current").Result; if (response.IsSuccessStatusCode) { return response.Content.ReadAsAsync<UserDetailsReponse>().Result; } else { Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); }
return null; }
static UsageReportResponse GetUsageReport() { var usagereportRequest = new UsageReportRequest { From = new DateTime(2019, 1, 1), To = new DateTime(2019, 1, 18), Skip = 0, Take = 100 }; var response = client.PostAsJsonAsync("/api/UsageReport/AllCompanies/products", usagereportRequest).Result; if(response.IsSuccessStatusCode) { return response.Content.ReadAsAsync<UsageReportResponse>().Result; } else { Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); } return null; } } }
|
Continue to the code analysis and necessary changes in the next chapter.