Mule 4 Unsplash Connector
Unsplash also has a REST API that provides a bunch of nice features on Image Search, Photo Actions, Collections, Topics, and Users.
Join the DZone community and get the full member experience.Join For Free
What is Unsplash?
Unsplash is a website dedicated to sharing stock photography under the Unsplash license. Since 2021, it has been owned by Getty Images. The website claims over 207,000 contributing photographers and generates more than 17 billion photo impressions per month on their growing library of over 2 million photos.
So basically Unsplash allows you to download images for free and use them under the Unsplash license on your web pages and projects.
Personally, I’m a big fan of this community, I’ve been uploading photographies for the last couple of years and it’s really amazing how many of the images uploaded have been used in a number of sites like BuzzFeed, Adobe Spark, and Trello, and also many people sharing in Twitter and email how they wanna use my images.
Unsplash also has a REST API that provides a bunch of nice features on Image Search, Photo Actions, Collections, Topics, and Users. Basically, you can use all these actions to create any kind of automated application.
This time I have created a new custom connector for Mule4 to use a couple of operations (Get Random Photo and Search photos).
Creating the Custom Connector
Creating a custom module (connector) is easier than you think, so now let me list the steps I followed
1. Executing the maven archetype command generate in your terminal.
Enter the name of the extension: Enter the extension's groupId: Enter the extension's artifactId: Enter the extension's version: 1.0.0 Enter the extension's main package:
3. Run the
mvn eclipse:eclipse command in the terminal, so this way when we can import our projects and let it be recognized by the IDE (in the case you are using Eclipse IDE, of course) personally I like IntelliJ IDEA.
4. Import the generated project into the IDE, but in my case, since I’m using the IntelliJ IDEA I select the Maven project. (It’s easier for the IDE to be in charge of pulling the Maven dependencies into the project.)
Finally, the connector is imported and we are able to start working on it:
- Now for this specific connector, we are going to use only two of the autogenerated Java classes: UnsplashConfiguration and UnsplashOperations.
- UnsplashConfiguration. Allows to set up any general parameter we might use, in this case, we needed to set the Access key Unsplash required for API calls.
- UnsplashOperations. Here we will specify our first operations in the connector, later we should be able to include more operations. For now, we will only have GetRandomPhoto and SearchPhoto.
- GetRandomPhoto. Will retrieve a random picture from the API.
- SearchPhoto. It will let you search based on a keyword and retrieve multiple picture information.
Get Random Photo
This operation will allow you to get a random set of photos from Unsplash and be able to specify some additional attributes like count (Number of images you want to retrieve), the keyword (“Specify the keyword for images you want to get from Unsplash”) and orientation (it can be a portrait, landscape or squarish). The connector will show the operations this way:
The search operation basically allows you to search photos on the Unsplash API specifying a few optional attributes like search terms (this will tell the search the photos you are looking for) page (specify the page you are) and per page (Tells how many records per page you want)
Installing the Mule Module
The installation is really easy. Basically, we just need to run the command:
mvn clean install
After running the command from terminal or IntelliJ IDEA, the new module will appear on the add modules palette:
Then we just need to drag and drop the module for our project. In this specific case, I have created a simple application that uses both operations. Search operation in this case contains a for each component that allows you to get the photo from Unsplash API and place the actual image in your local directory.
Here a sample of images downloaded from Search:
You can get both codes from the repositories:
Connector Implemented Code: https://github.com/emoran/unsplash-connector-implementation.
Hope you enjoy this project and hopefully, it will help with any type of project/integration you’re working on.
Opinions expressed by DZone contributors are their own.