Skip to content

Commit

Permalink
add runnable example
Browse files Browse the repository at this point in the history
  • Loading branch information
blaugold committed Jan 24, 2020
1 parent 6ce8fe1 commit 7bac740
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 38 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
3 changes: 3 additions & 0 deletions .idea/unsplash_client.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 1 addition & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
72 changes: 72 additions & 0 deletions example/lib/main.dart
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);
}
5 changes: 5 additions & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: example

dependencies:
unsplash_client:
path: ../

0 comments on commit 7bac740

Please # to comment.