query method

Future<OLSearchBase> query({
  1. String query = "",
  2. String title = "",
  3. String author = "",
  4. String subject = "",
  5. String place = "",
  6. String person = "",
  7. SearchLanguage language = SearchLanguage.none,
  8. String publisher = "",
  9. String isbn = "",
  10. String q = "",
  11. bool loadCover = true,
  12. CoverSize coverSize = CoverSize.S,
  13. int limit = 10,
})

To query the openlibrary.org/search.json api with all it's options Always check RuntimeType OLSearchBase type is -> OLSearch / OLSearchError Use title to give search words for the title and/or author for the author name and/or isbn in a 10 or 13 ISBN digits format, if less it is filled with * to 10 digits and/or subject to search for books subject content and/or place to find places named in the books and/or person to find fictive or real persons in the book and/or language if you want to find special published languages and/or publisher to find books publishers as a filter or use q only to find any given content use loadCover indicates if the cover should be loaded, default = true and coverSite to define the loaded size, M,S,L from enum CoverSize, default = S, if you want to limit the search, it returns max docs set by limit and just load limit covers, default = 10

Implementation

Future<OLSearchBase> query({
  String query = "",
  String title = "",
  String author = "",
  String subject = "",
  String place = "",
  String person = "",
  SearchLanguage language = SearchLanguage.none,
  String publisher = "",
  String isbn = "",
  String q = "",
  bool loadCover = true,
  CoverSize coverSize = CoverSize.S,
  int limit = 10,
}) async {
  // pattern builder
  final StringBuffer patterns = StringBuffer();

  // render query (pattern)
  if (q.trim().isNotEmpty) {
    patterns.write(q.toJSONPattern(pattern: SearchPatterns.q));
  } else {
    if (title.trim().isNotEmpty) {
      patterns.write(title.toJSONPattern(pattern: SearchPatterns.title));
    }
    if (author.trim().isNotEmpty) {
      if (patterns.isNotEmpty) {
        patterns.write("&");
      }
      patterns.write(author.toJSONPattern(pattern: SearchPatterns.author));
    }
    if (subject.trim().isNotEmpty) {
      if (patterns.isNotEmpty) {
        patterns.write("&");
      }
      patterns.write(subject.toJSONPattern(pattern: SearchPatterns.subject));
    }
    if (place.trim().isNotEmpty) {
      if (patterns.isNotEmpty) {
        patterns.write("&");
      }
      patterns.write(place.toJSONPattern(pattern: SearchPatterns.place));
    }
    if (person.trim().isNotEmpty) {
      if (patterns.isNotEmpty) {
        patterns.write("&");
      }
      patterns.write(person.toJSONPattern(pattern: SearchPatterns.person));
    }
    if (language != SearchLanguage.none) {
      if (patterns.isNotEmpty) {
        patterns.write("&");
      }
      patterns.write(
          language.name.toJSONPattern(pattern: SearchPatterns.language));
    }
    if (publisher.trim().isNotEmpty) {
      if (patterns.isNotEmpty) {
        patterns.write("&");
      }
      patterns
          .write(publisher.toJSONPattern(pattern: SearchPatterns.publisher));
    }
    if (isbn.trim().isNotEmpty) {
      if (patterns.isNotEmpty) {
        patterns.write("&");
      }
      patterns.write(isbn.toJSONPattern(pattern: SearchPatterns.isbn));
    }
  }
  // if we have valid search patterns we run the search
  if (patterns.isNotEmpty) {
    final List<OLSearchDoc> docs = [];
    final APISearchBase search =
        await _openLibraryApiService.query(pattern: patterns.toString());
    if (search is APISearch) {
      // render all search result docs (books)
      if (search.docs.isNotEmpty) {
        // if the load needs to load covers iterate through all docs
        await Future.forEach(search.docs, (APISearchDoc doc) async {
          // limit the results
          if (docs.length < limit) {
            final List<Uint8List> covers = [];
            final List<OLAuthor> authors = [];
            // load cover if wanted and exists
            //log("authors:${doc.author_name}");
            if (doc.cover_i > 0 && loadCover) {
              final Uint8List cover =
                  await getCover(coverId: doc.cover_i, coverSize: coverSize);
              covers.add(cover);
            }
            // render Authors
            for (var authorName in doc.author_name) {
              final OLAuthor author = OLAuthor(
                name: authorName,
              );
              authors.add(author);
            }

            final OLSearchDoc olSearchDoc = _olSearchDocFromAPISearchDoc(
                searchDoc: doc, authors: authors, covers: covers);
            docs.add(olSearchDoc);
          }
        });
      }

      final OLSearch olSearch = _olSearchFromAPISearch(
          search: search, docs: docs, patterns: patterns.toString());
      return olSearch;
    }
  }

  return const OLSearchError();
}