Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add api client #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ApiClient/ApiSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class ApiSettings
{
public string BaseUrl { get; set; }
public string BearerToken { get; set; }
}
23 changes: 23 additions & 0 deletions ApiClient/MyApiService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
public class MyApiService
{
private readonly ApiSettings _apiSettings;

public MyApiService(IOptions<ApiSettings> apiSettings)
{
_apiSettings = apiSettings.Value;
}

public async Task<string> GetApiDataAsync()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(_apiSettings.BaseUrl);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _apiSettings.BearerToken);

var response = await client.GetAsync("endpoint");
response.EnsureSuccessStatusCode();

return await response.Content.ReadAsStringAsync();
}
}
}
5 changes: 5 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.Configure<ApiSettings>(Configuration.GetSection("ApiSettings"));


var app = builder.Build();

// Configure the HTTP request pipeline.
Expand All @@ -22,6 +25,8 @@
app.UseSwaggerUI();
}



app.UseHttpsRedirection();

app.UseAuthorization();
Expand Down
6 changes: 5 additions & 1 deletion appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ApiSettings": {
"BaseUrl": "https://api.example.com",
"BearerToken": "your-bearer-token-here",
}
}
}