ESET Online Help

Search
Select the topic

Code analysis

The sample code from the previous chapter is analyzed below. Each segment of the example code is described and explained.


note

We are not attempting to teach C# programming or provide a detailed explanation of functions and algorithms here. This code analysis is a high-level overview of the sample. There are multiple ways to write this code that would achieve the same or similar results.

Initialization

Initialization of the required libraries; these may differ in other development environments. Initialized libraries are used to execute an API call.

Classes declaration

In this segment of code, we are creating a class to use for creating objects in API functions. You can create your class to fit the functions you intend to use. However, we strongly recommend to use a logical naming convention.

The communication function

We are creating a new object of HttpClient calls called client. We are also creating a variable BaseURL to save the URL where we want to send the API requests. Change the URL if your environment uses a different one.

Execution of calls

In the Main section of the code, we are executing routines to log in to the ESET MSP Administrator 2 API and request two reports. These routines are mentioned later in the code. The authorization process is analogous to the manual process a user executes in Swagger UI.

The following lines :

write the first name and user ID to the console and waits for the user to press the Enter key. You can modify this output command to write any data from the userDetails object. The data fields FirstName and Id are defined by the class UserDetailsReponse.

Login process

In the AuthenticationResponse routine, the user enters their credentials to obtain the access token. Replace the ***** by the credentials you want to use. If the call is not successful, the line Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); is written with the information to the console.


note

The log in process here does not check for the access token validity or renewal of the token. Each execution of the program requests a new access token and uses it immediately for calls in the Main function.

GetUserDetails process

In the UserDetailsResponse routine, the user calls report: /api/User/Current, and the contents are returned. If the call is not successful, the line Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); is written with the information to the console.

GetUsageReport process

In the UserDetailsResponse routine, the user calls report: /api/UsageReport/AllCompanies/products, and the contents are returned. In the { From = new DateTime(2019, 1, 1), To = new DateTime(2019, 1, 18), Skip = 0, Take = 100 } section of the code, the user sets the parameter values.

If the call is not successful, the line Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); is written with the information to the console.