Skip to main content

API

The GravatarJavaClient API contains support for the comprehensive Gravatar API, that of both the avatar and profile requests. This library also follows Effective Java principles. The GravatarAvatarRequest handles all needs related to the avatar API whilst the GravatarProfileRequest handles all needs related to the profile API.

Avatars

The standard Gravatar API for getting an avatar:

// Define a reusable request
GravatarAvatarRequest request = GravatarAvatarRequest.fromEmail("your.email@email.com")
.setSize(800)
.setRating(GravatarRating.R)
.setDefaultImageType(GravatarDefaultImageType.ROBO_HASH);

// Get a BufferedImage of the avatar
BufferedImage bufferedImage = request.getBufferedImage();

File profileFile = new File("/path/to/your/avatar.png", ".png");
boolean wasSaved = request.saveTo(profileFile, "png");

You can also check how many images have been saved to the local file system during the current JVM session:

int count = GravatarRequestImageSaver.INSTANCE.getSavedCount();

Note, the GravatarRequestImageSaver is shared by GravatarAvatarRequest and GravatarQrCodeRequest and could technically be invoked by anything to save images. As such, the returned count should not be trusted as a reliable source of truth.

Profiles

When requesting profiles, there are two modes:

  • unauthenticated
  • authenticated

unauthenticated requests will not return certain fields whilst authenticated requests will return all available fields.

Authenticated requests require a valid API token to be provided which will be used in the authentication HTTP header.

// Define a reusable request
GravatarProfileRequest request = GravatarProfileRequest.fromEmail("your.email@email.com");

// Get the profile
GravatarProfile profile = request.getProfile();

// Or save the profile to a JSON file
File profileJsonFile = new File("/path/to/profile.json")
boolean wasSaved = request.writeToFile(profileJsonFile);

// Get a profile with authenticated fields present
GravatarProfile profileWithAuthenticatedFields = GravatarProfileRequest.fromEmail("your.email@email.com")
.setToken("myApiToken")
.getProfile();

You can also see how many unauthenticated and authenticated requests have been sent during the current JVM session:

const unauthenticatedCount = GravatarProfileRequestHandler.INSTANCE.getUnauthenticatedRequestCount();
const authenticatedCount = GravatarProfileRequestHandler.INSTANCE.getAuthenticatedRequestCount();

QR codes

You can generate a QR code for your Gravatar profile using a GravatarQrCodeRequest:

// Define a reusable request
GravatarQrCodeRequest request = GravatarQrCodeRequest.fromEmail("your.email@email.com")
.setSize(800)
.setImageType(GravatarQrImageType.USER)
.setVersion(GravatarQrImageVersion.THREE);

// Save the QR code to a file
File fileToSaveTo = new File("/path/to/your/qr_code_file.png")
boolean wasSaved = request.saveTo(fileToSaveTo);

Note, the Gravatar API returns a PNG for QR codes, presumably for lossless compression, which is why the GravatarJavaClient API does not allow for an encoding parameter in the way GravatarAvatarRequest does for the saveTo method.

You can also check how many QR codes have been saved to the local file system during the current JVM session:

int count = GravatarRequestImageSaver.INSTANCE.getSavedCount();

Note, the GravatarRequestImageSaver is shared by GravatarAvatarRequest and GravatarQrCodeRequest and could technically be invoked by anything to save images. As such, the returned count should not be trusted as a reliable source of truth.