unsplash_client 0.2.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 77

An unofficial client for the unsplash api.

The client is platform independent, since it uses http to make requests.

This is a work in progress:

  • [ ] Get the user’s profile

  • [ ] Update the current user’s profile

  • [x] Get a user’s public profile

  • [x] Get a user’s portfolio link

  • [x] List a user’s photos

  • [x] List a user’s liked photos

  • [x] List a user’s collections

  • [x] Get a user’s statistics

  • [x] List photos

  • [x] Get a photo

  • [x] Get random photos

  • [x] Get a photo’s statistics

  • [x] Track a photo download

  • [ ] Update a photo

  • [ ] Like a photo

  • [ ] Unlike a photo

  • [x] Search photos

  • [x] Search collections

  • [x] Search users

  • [x] List collections

  • [x] List featured collections

  • [x] Get a collection

  • [x] Get a collection’s photos

  • [x] List a collection’s related collections

  • [ ] Create a new collection

  • [ ] Update an existing collection

  • [ ] Delete a collection

  • [ ] Add a photo to a collection

  • [ ] Remove a photo from a collection

  • [x] Stats.total

  • [x] Stats.month

Usage #

See examples tab.

0.2.0 #

  • feat(): improve client

    • handle non json body
    • add Response.get to get Response.data or throw an error
  • feat(): add various filtering options

    • Photos.random: contentFilter
    • Search.photos: color, orderBy, contentFilter
    • Users.photos: orientation
    • Users.likedPhotos: orientation
    • PhotoOrder.relevant
  • feat(): add collections api (public actions only)

  • fix(): include User.portfolio in json representation

0.1.2 #

  • fix(): downgrade version of collection to ^1.14.11

0.1.1 #

  • add runnable example
  • update package description

0.1.0 #

  • Initial release

example/lib/main.dart

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);
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  unsplash_client: ^0.2.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:unsplash_client/unsplash_client.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
54
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
77
Learn more about scoring.

We analyzed this package on May 26, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.1
  • pana: 0.13.8-dev

Health suggestions

Fix lib/src/client.dart. (-0.50 points)

Analysis of lib/src/client.dart reported 1 hint:

line 163 col 17: Avoid empty catch blocks.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
collection ^1.14.11 1.14.12
http ^0.12.0+4 0.12.1
meta ^1.1.8 1.1.8
Transitive dependencies
charcode 1.1.3
http_parser 3.1.4
pedantic 1.9.0
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
effective_dart ^1.2.0
matcher ^0.12.6
path ^1.6.4 1.7.0
test ^1.11.0