This article covers the journey of building a Random Place Generator, from inception to implementation, highlighting key concepts and technologies used along the way.
The Inspiration
My habit of saving places on maps without visiting them led me to develop a tool that selects a random location from my saved list, making the decision-making process easier and more spontaneous. While initially, I explored the idea of web scraping to retrieve saved places but encountered roadblocks. Thus, I came up with this alternative approach.
Tools Involved:
- IntelliJ
- Docker
- Postman
- MongoDB Compass
Explore “Building a NoSQL database” for a beginner’s project with MongoDB and learn “ How to set up MongoDB with docker” to seamlessly access MongoDB from your IntelliJ environment
Approach
- Receive user input via the endpoint
/add/location
. - Forward the input to the Google Places API.
- Manage the response from the Google Places API.
- Persist the response in MongoDB with the place as the key.
- Invoke the
/get/random
API to retrieve a random place, querying the MongoDB for selection.
Concepts Involved
- Utilize the Google Places API: I began by integrating the Google Places API into my application, learning how to form requests and process responses to retrieve location data based on a provided name.
- Calling the API with RestTemplate: Using RestTemplate, I made HTTP requests to the Google Places API, fetching diverse locations based on user input.
- Processing JSON Responses with Jackson ObjectMapper: I employed Jackson ObjectMapper to parse JSON response objects from the API, extracting relevant information such as location details.
- Handling Responses and Exceptions: Implementing an Enum for response codes and comprehensive exception handling ensured smooth operation even in error scenarios.
- Configuration Management: Storing variable names in YAML configuration files enhanced manageability and flexibility in the application, ensuring that it is extendable in case of accessing other Google APIs.
- Persistence with MongoDB: I stored fetched location information in MongoDB, providing a reliable and scalable storage solution.
- Random Place Selection: With MongoDB, I implemented functionality to retrieve a random entry.
- Comprehensive Testing: Test cases were crucial, covering all aspects of the application — use of MockMVC to test the controller layer, use of WireMock to stub the Google API and use of TestContainers to provide a lightweight docker container that serves as a DB instead of connecting to the original MongoDB or stubbing the DB retrieve functions.
- Handle Exception Scenarios
Notes
API Key
Ensure you have an API key and have enabled the location API in your Google Console. Failure to complete this step will result in the following error: {"error_message”:”This API project is not authorized to use this API.”,”result”:[],”status”:”REQUEST_DENIED”}
Endpoint
It’s worth noting that the “findplacefromtext” endpoint used in the URL that I called typically returns a single result. For multiple results, consider using the “nearbysearch” or “textsearch” endpoints instead https://maps.googleapis.com/maps/api/place/findplacefromtext/json?fields=formatted_address,name,rating,opening_hours&input={location}&inputtype=textquery&key={apiKey}
Approach to fetch a random element
There are two approaches to obtaining a random place:
1) Fetch the entire list from DB, shuffle it, and then use the findAny method (from Java 8 Streams API)
2) Leverage the $sample
aggregation operator from MongoDB to retrieve a random document from MongoDB
The 2nd approach avoids the need to load the entire dataset into memory, which can be advantageous for large datasets. However, it relies on the efficiency of the database query and may require appropriate indexing for optimal performance.
Conclusion
Check out my source code here. Hope that this project served as a simple way to get started with the Google Places API!