Skip to content

Commit 400bd1b

Browse files
committed
Fix some issues, remove hardcoded client id - generated a new one. Hardcode query to 'programming' for now
1 parent 4028df0 commit 400bd1b

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

BlogHelper9000/Imager/UnsplashClient.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,39 @@ namespace BlogHelper9000.Imager;
88

99
public class UnsplashClient(ILogger logger) : IDisposable, IUnsplashClient
1010
{
11-
private const string UnsplashApiUrl = "https://api.unsplash.com/photos/random?client_id=Ey17V904djPIM7J-KeFxmxVShTF_NCx5hFXu2lxkPeE&query=";
11+
private const string UnsplashApiUrl = "https://api.unsplash.com/photos/random";
1212
private const string UnsplashRequestVersion = "v1";
1313
private readonly HttpClient _httpClient = new();
1414

1515
public async Task<Stream> LoadImageAsync(string query)
1616
{
17-
var client = CreateUnsplashClient();
17+
var (client, clientId) = CreateUnsplashClient();
1818
if (client != null)
1919
{
20-
var request = UnsplashApiUrl + query;
20+
var queryUrl = AddQueryTooUrl(query);
21+
var fullUrl = AddClientIdToUrl(queryUrl, clientId);
2122
logger.LogInformation("Loading random Unsplash image for the query '{ImageQuery}'", query);
22-
var unsplashData = await _httpClient.GetFromJsonAsync<UnsplashData>(request);
23+
var unsplashData = await _httpClient.GetFromJsonAsync<UnsplashData>(fullUrl);
2324
if (unsplashData != null)
2425
{
25-
var imageUrl = unsplashData.Urls.Regular;
26+
var imageUrl = $"{unsplashData.Urls.Raw}&w=1280&h=720&fit=min";
2627
var imageStream = await _httpClient.GetStreamAsync(imageUrl);
2728
return imageStream;
2829
}
2930
}
3031

3132
logger.LogError("Could not load Unsplash image because credentials are missing");
3233
return Stream.Null;
34+
35+
string AddQueryTooUrl(string query)
36+
{
37+
return $"{UnsplashApiUrl}?query=programming";
38+
}
39+
40+
string AddClientIdToUrl(string url, string clientId)
41+
{
42+
return $"{url}&client_id={clientId}";
43+
}
3344
}
3445

3546
public void Dispose()
@@ -38,7 +49,7 @@ public void Dispose()
3849
_httpClient.Dispose();
3950
}
4051

41-
private HttpClient? CreateUnsplashClient()
52+
private (HttpClient? httpClient, string clientId) CreateUnsplashClient()
4253
{
4354
var credentials = LoadCredentials();
4455
if (credentials != null)
@@ -48,13 +59,11 @@ public void Dispose()
4859
var client = new HttpClient();
4960
client.BaseAddress = new Uri(UnsplashApiUrl);
5061
client.DefaultRequestHeaders.Add("Accept-Version", UnsplashRequestVersion);
51-
//client.DefaultRequestHeaders.Add("Authorization", $"Client-ID {credentialParts[0]}");
52-
client.DefaultRequestHeaders.Authorization = new("Client-ID", clientId);
53-
return client;
62+
return (client, clientId);
5463
}
5564

5665
logger.LogError("Could not create Unsplash client because credentials are missing");
57-
return null;
66+
return (null, string.Empty);
5867
}
5968

6069
private AppDataModel? LoadCredentials()

0 commit comments

Comments
 (0)