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 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 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 Search of images, Photo actions, Collections, Topics and Users, basically you can use all this actions to create any kind of automated application.
This time I have created a new custom connector for Mule4 to use a couple 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
- Executing the maven archetype command generate in your terminal.
- Then a few questions need to be answered related to the project
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:
- 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 case you are using Eclipse IDE of course) personally I like IntelliJ IDEA
- Import the generated project into the IDE, but in my case since I’m using the IntelliJ IDEA I select maven project (It’s easier the IDE be on charge of pulling the maven dependencies into the project).
Finally 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 migh 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. 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), keyword (“Specify the keyword for images you want to get from Unsplash”) and orientation (it can be 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 dropt the module for our project. In this specific case I have crated a simple ap;lication that uses both operations. Search operation in this case contains a foreach component that allows to get the photo from Unsplash API and place the actual image in you 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.