Simple Java library for Pixabay API
Pixabay-Lib provides easy access to all available methods of Pixabay API. Can be useful for grabbing some pictures and videos from different categories.
- Contains Java object wrappers for any API response
- Supports RxJava Observable responses
- Convenient request builders
Full list of methods and its params available in Pixabay API Documentation
In your pom.xml inside the <dependencies> tag
<dependencies>
...
<dependency>
<groupId>ru.blizzed</groupId>
<artifactId>pixabay-lib</artifactId>
<version>1.0.3</version>
</dependency>
</dependencies>
In your build.gradle file inside the dependencies section
- Gradle 3.0 and above
dependencies {
...
implementation 'ru.blizzed:pixabay-lib:1.0.3'
}
- Below 3.0
dependencies {
...
compile 'ru.blizzed:pixabay-lib:1.0.3'
}
- You don't care about language (
EN
is default)
Pixabay.initialize("your-api-key"));
- You need a specific language from available lang list
Pixabay.initialize("your-api-key", LangParam.Lang.RU));
Russian language as example, you can choose any other
All methods of Pixabay API are available in Pixabay
class after initialization. Use Pixabay.search()
for
usual case and Pixabay.rxSearch()
if you prefer to deal with RxJava and its Observables.
You can pass any params to the call – just take a look at static class PixabayParams
that
contains completed instances of all parameters.
Imagine that you need to find some pics with animals
Pixabay.search().image(PixabayParams.CATEGORY.of(Category.ANIMALS))
.execute()
.getHits()
.stream()
.map(PixabayImage::getLargeImageURL)
.forEach(System.out::println);
or the same but with a set of params (now you are looking for red pics with dog in cat. animals and horizontal orientation)
Pixabay.search().image(
"dog",
PixabayParams.CATEGORY.of(Category.ANIMALS),
PixabayParams.COLORS.of(Color.RED),
PixabayParams.ORIENTATION.of(Orientation.HORIZONTAL)
)
.execute()
.getHits()
.stream()
.map(PixabayImage::getLargeImageURL)
.forEach(System.out::println);
You can receive callbacks in two ways:
- Catching exceptions
try {
Pixabay.search().image().execute().getHits().forEach(System.out::println);
} catch (PixabayCallException | PixabayErrorException e) {
// Handle error
}
- With listener
Pixabay.search().image().execute(new PixabayCaller.Listener<PixabayResult<PixabayImage>>() {
@Override
public void onComplete(PixabayResult<PixabayImage> result, PixabayCaller<PixabayResult<PixabayImage>> apiCaller) {
// Handle result
}
@Override
public void onError(PixabayError error, PixabayCaller<PixabayResult<PixabayImage>> apiCaller) {
/* This method triggers you when API has been called but response contains an error */
// Handle Api Error
}
@Override
public void onFailure(PixabayCallException e, PixabayCaller<PixabayResult<PixabayImage>> apiCaller) {
/* This method triggers you when call to API cannot be established. E.g. no internet connection */
// Handle Failure
}
});
Tip: you can override not all callback methods
- Or by using RxJava Observables
Pixabay.rxSearch().image(PixabayParams.CATEGORY.of(Category.ANIMALS));
It goes without saying that you can also cancel request immediately if needs
caller.cancel();
Copyright (c) 2018 BlizzedRu (Ivan Vlasov)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.