getContents method

Future<RepositoryContents> getContents(
  1. RepositorySlug slug,
  2. String path, {
  3. String? ref,
})

Fetches content in a repository at the specified path.

When the path references a file, the returned RepositoryContents contains the metadata AND content of a single file.

When the path references a directory, the returned RepositoryContents contains the metadata of all the files and/or subdirectories.

Use RepositoryContents.isFile or RepositoryContents.isDirectory to distinguish between both result types.

The name of the commit/branch/tag may be specified with ref. If no ref is defined, the repository's default branch is used (usually master).

API docs: https://developer.github.com/v3/repos/contents/#get-contents

Implementation

Future<RepositoryContents> getContents(RepositorySlug slug, String path,
    {String? ref}) async {
  ArgumentError.checkNotNull(slug);
  ArgumentError.checkNotNull(path);
  var url = '/repos/${slug.fullName}/contents/$path';

  if (ref != null) {
    url += '?ref=$ref';
  }

  return github.getJSON(
    url,
    convert: (dynamic input) {
      final contents = RepositoryContents();
      if (input is Map) {
        // Weird one-off. If the content of `input` is JSON w/ a message
        // it was likely a 404 – but we don't have the status code here
        // But we can guess an the JSON content
        if (input.containsKey('message')) {
          throw GitHubError(github, input['message'],
              apiUrl: input['documentation_url']);
        }
        contents.file = GitHubFile.fromJson(input as Map<String, dynamic>);
      } else {
        contents.tree =
            (input as List).map((it) => GitHubFile.fromJson(it)).toList();
      }
      return contents;
    },
  );
}