diff --git a/.github/workflows/dart.yml b/.github/workflows/dart.yml
index f0ce212..f02af06 100644
--- a/.github/workflows/dart.yml
+++ b/.github/workflows/dart.yml
@@ -19,3 +19,8 @@ jobs:
env:
UNSPLASH_ACCESS_KEY: ${{ secrets.UNSPLASH_ACCESS_KEY }}
UNSPLASH_SECRET_KEY: ${{ secrets.UNSPLASH_SECRET_KEY }}
+ - name: Run example
+ run: dart example/lib/main.dart
+ env:
+ UNSPLASH_ACCESS_KEY: ${{ secrets.UNSPLASH_ACCESS_KEY }}
+ UNSPLASH_SECRET_KEY: ${{ secrets.UNSPLASH_SECRET_KEY }}
diff --git a/.idea/unsplash_client.iml b/.idea/unsplash_client.iml
index f79fe77..5eab505 100644
--- a/.idea/unsplash_client.iml
+++ b/.idea/unsplash_client.iml
@@ -7,6 +7,9 @@
+
+
+
diff --git a/README.md b/README.md
index 1066839..df492d7 100644
--- a/README.md
+++ b/README.md
@@ -45,41 +45,4 @@ This is a work in progress:
## Usage
-A simple usage example:
-
-```dart
-import 'package:unsplash_client/unsplash_client.dart';
-
-void main() async {
- // Create a client.
- final client = UnsplashClient(
- settings: Settings(
- // Use the credentials from the developer portal.
- credentials: AppCredentials(
- accessKey: '...',
- secretKey: '...',
- )
- ),
- );
-
- // Fetch 5 random photos.
- final response = await client.photos.random(count: 5).go();
-
- // Check that the request was successful.
- if (!response.isOk) {
- throw 'Something is wrong: $response';
- }
-
- // Do something with the photos.
- final photos = response.data;
-
- // Create a dynamically resizing url.
- final resizedUrl = photos.first.urls.raw.resize(
- width: 400,
- height: 400,
- fit: ResizeFitMode.cover,
- format: ImageFormat.webp,
- );
-
-}
-```
+See examples tab.
diff --git a/example/lib/main.dart b/example/lib/main.dart
new file mode 100644
index 0000000..5ea3fad
--- /dev/null
+++ b/example/lib/main.dart
@@ -0,0 +1,72 @@
+import 'dart:convert';
+import 'dart:io';
+
+import 'package:unsplash_client/unsplash_client.dart';
+
+void main(List 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 loadAppCredentialsFromFile(String fileName) async {
+ final file = File(fileName);
+ final content = await file.readAsString();
+ final json = jsonDecode(content) as Map;
+ return AppCredentials.fromJson(json);
+}
diff --git a/example/pubspec.yaml b/example/pubspec.yaml
new file mode 100644
index 0000000..7ac6f95
--- /dev/null
+++ b/example/pubspec.yaml
@@ -0,0 +1,5 @@
+name: example
+
+dependencies:
+ unsplash_client:
+ path: ../