-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
86 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:unsplash_client/unsplash_client.dart'; | ||
|
||
void main(List<String> args) async { | ||
// Load app credentials from environment variables or file. | ||
var appCredentials = loadAppCredentialsFromEnv(); | ||
|
||
if (appCredentials == null) { | ||
if (args.length != 1) { | ||
throw "Please provide a credentials file as the first and only argument."; | ||
} | ||
|
||
appCredentials = await loadAppCredentialsFromFile(args.first); | ||
} | ||
|
||
// Create a client. | ||
final client = UnsplashClient( | ||
settings: ClientSettings(credentials: appCredentials), | ||
); | ||
|
||
// Fetch 5 random photos. | ||
final response = await client.photos.random(count: 2).go(); | ||
|
||
// Check that the request was successful. | ||
if (!response.isOk) { | ||
throw 'Something is wrong: $response'; | ||
} | ||
|
||
// Do something with the photos. | ||
final photos = response.data; | ||
print("--- Photos"); | ||
print(photos); | ||
print("---\n"); | ||
|
||
// Create a dynamically resizing url. | ||
final resizedUrl = photos.first.urls.raw.resizePhoto( | ||
width: 400, | ||
height: 400, | ||
fit: ResizeFitMode.clamp, | ||
format: ImageFormat.webp, | ||
); | ||
print("--- Resized Url"); | ||
print(resizedUrl); | ||
} | ||
|
||
/// Loads [AppCredentials] from environment variables | ||
/// (`UNSPLASH_ACCESS_KEY`, `UNSPLASH_SECRET_KEY`). | ||
/// | ||
/// Returns `null` if the variables do not exist. | ||
AppCredentials loadAppCredentialsFromEnv() { | ||
final accessKey = Platform.environment['UNSPLASH_ACCESS_KEY']; | ||
final secretKey = Platform.environment['UNSPLASH_SECRET_KEY']; | ||
|
||
if (accessKey != null && secretKey != null) { | ||
return AppCredentials( | ||
accessKey: accessKey, | ||
secretKey: secretKey, | ||
); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/// Loads [AppCredentials] from a json file with the given [fileName]. | ||
Future<AppCredentials> loadAppCredentialsFromFile(String fileName) async { | ||
final file = File(fileName); | ||
final content = await file.readAsString(); | ||
final json = jsonDecode(content) as Map<String, dynamic>; | ||
return AppCredentials.fromJson(json); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
name: example | ||
|
||
dependencies: | ||
unsplash_client: | ||
path: ../ |