https://github.com/bujosa/facade-design-pattern
See example of facade design pattern in go
https://github.com/bujosa/facade-design-pattern
design-patterns facade-pattern golang structural-patterns
Last synced: 11 months ago
JSON representation
See example of facade design pattern in go
- Host: GitHub
- URL: https://github.com/bujosa/facade-design-pattern
- Owner: bujosa
- License: mit
- Created: 2024-01-31T00:10:36.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-02-04T23:28:31.000Z (about 2 years ago)
- Last Synced: 2025-03-11T10:53:20.343Z (11 months ago)
- Topics: design-patterns, facade-pattern, golang, structural-patterns
- Language: Go
- Homepage:
- Size: 6.84 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Facade Design Pattern
The Facade design pattern provides a simplified interface to a complex subsystem. It involves a single wrapper class which contains a set of members required by the client. These members access the system on behalf of the facade client and hide the implementation details.
In the provided Go code, the facade pattern is used to simplify the process of retrieving weather data from the OpenWeatherMap API.
## Code Explanation
The code defines a `CurrentWeatherDataRetriever` interface with two methods: `GetByGeoCoordinates` and `GetByCityAndCountryCode`. These methods are used to retrieve weather data either by geographical coordinates or by city and country code.
The `CurrentWeatherData` struct implements this interface. It contains an `APIkey` field and uses it to make HTTP requests to the OpenWeatherMap API.
The `Weather` struct is a data structure that maps to the JSON response from the OpenWeatherMap API. It contains fields for various weather parameters like temperature, pressure, humidity, etc.
The `doRequest` method is a helper method that performs the actual HTTP request to the OpenWeatherMap API, checks the response status code, and parses the response body into a `Weather` struct.
The `responseParser` method is used to parse the HTTP response body into a `Weather` struct.
The `GetByGeoCoordinates` and `GetByCityAndCountryCode` methods use the `doRequest` method to retrieve weather data from the OpenWeatherMap API. They format the API URL with the appropriate parameters and pass it to the `doRequest` method.
In this way, the `CurrentWeatherData` struct acts as a facade, providing a simple interface for retrieving weather data and hiding the complexities of making HTTP requests and parsing JSON responses.
## Running the Tests
To run the tests, you will need to set your OpenWeatherMap API key and a flag to indicate whether to run integration tests as environment variables. You can do this by creating a `.env` file in your project root and adding your variables there. See the `.env.example` file for an example of how to set up your `.env` file.
Once you've set up your `.env` file, you can run your tests with the `go test` command. The tests will automatically use the variables from your `.env` file.
Here is an example of how to set up your `.env` file:
```bash
APIKEY=your_api_key
INTEGRATION=true
go test
```
## Documentation
You can find the official documentation for the OpenWeatherMap API [here](https://openweathermap.org/api).