Upload files to 'ApiClient.cs'
This commit is contained in:
parent
6476cc1ed3
commit
e5cbedc4b7
|
@ -0,0 +1,120 @@
|
||||||
|
using System.Text;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace ApiClient
|
||||||
|
{
|
||||||
|
internal class Program
|
||||||
|
{
|
||||||
|
static readonly HttpClient _client = new();
|
||||||
|
|
||||||
|
static readonly string _hostname = "demo.credoid.com";
|
||||||
|
|
||||||
|
static async Task Main(string[] args)
|
||||||
|
{
|
||||||
|
var token = await LoginAsync();
|
||||||
|
|
||||||
|
_client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token.access_token}");
|
||||||
|
|
||||||
|
await GetUsersAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task<LoginResponse> LoginAsync()
|
||||||
|
{
|
||||||
|
var content = new StringContent(JsonConvert.SerializeObject(new { Username = "admin", Password = "admin" }),
|
||||||
|
Encoding.UTF8, "application/json");
|
||||||
|
|
||||||
|
var response = await _client.PostAsync($"https://{_hostname}/api/login", content);
|
||||||
|
|
||||||
|
var result = await response.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
|
var loginResponse = JsonConvert.DeserializeObject<LoginResponse>(result);
|
||||||
|
|
||||||
|
return loginResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task GetUsersAsync()
|
||||||
|
{
|
||||||
|
var response = await _client.GetAsync($"https://{_hostname}/api/users");
|
||||||
|
|
||||||
|
var result = await response.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
|
var users = JsonConvert.DeserializeObject<IEnumerable<User>>(result);
|
||||||
|
|
||||||
|
foreach (var user in users)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"{user.FirstName} {user.LastName} {user.EmployeeNumber}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class LoginResponse
|
||||||
|
{
|
||||||
|
public string access_token { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class User
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Id
|
||||||
|
/// </summary>
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// First name
|
||||||
|
/// </summary>
|
||||||
|
public string FirstName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Last name
|
||||||
|
/// </summary>
|
||||||
|
public string LastName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Company name
|
||||||
|
/// </summary>
|
||||||
|
public string CompanyName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Department name
|
||||||
|
/// </summary>
|
||||||
|
public string DepartmentName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// User title name
|
||||||
|
/// </summary>
|
||||||
|
public string UserTitleName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Employee number
|
||||||
|
/// </summary>
|
||||||
|
public string EmployeeNumber { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Email
|
||||||
|
/// </summary>
|
||||||
|
public string Email { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Phone number
|
||||||
|
/// </summary>
|
||||||
|
public string PhoneNumber { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Version
|
||||||
|
/// </summary>
|
||||||
|
public string Version { get; set; }
|
||||||
|
|
||||||
|
public int? CompanyId { get; set; }
|
||||||
|
public int? DepartmentId { get; set; }
|
||||||
|
public int? TitleId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Activation date time
|
||||||
|
/// </summary>
|
||||||
|
public DateTime ActivationDate { get; set; }
|
||||||
|
|
||||||
|
public DateTime? ExpirationDate { get; set; }
|
||||||
|
|
||||||
|
public bool HasImage { get; set; }
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue