Hey there, fellow Go enthusiast! Ready to dive into the world of real estate data? We're about to embark on a journey to integrate the Apartments.com API into a Go application. This powerful API will let us tap into a vast database of apartment listings, making your next property search app a breeze to build.
Before we jump in, make sure you've got:
Let's kick things off by creating a new Go module:
mkdir apartments-api-integration cd apartments-api-integration go mod init github.com/yourusername/apartments-api-integration
Now, let's grab the HTTP client we'll need:
go get -u github.com/go-resty/resty/v2
Time to set up our API client. Create a new file called client.go
:
package main import ( "github.com/go-resty/resty/v2" ) type Client struct { httpClient *resty.Client apiKey string } func NewClient(apiKey string) *Client { return &Client{ httpClient: resty.New().SetBaseURL("https://api.apartments.com/v1"), apiKey: apiKey, } }
Let's add a method to search for apartments:
func (c *Client) SearchApartments(query string) ([]Apartment, error) { var response struct { Results []Apartment `json:"results"` } _, err := c.httpClient.R(). SetQueryParam("q", query). SetHeader("Authorization", "Bearer "+c.apiKey). SetResult(&response). Get("/search") return response.Results, err }
Now, let's add some retry logic and respect those rate limits:
func (c *Client) SearchApartments(query string) ([]Apartment, error) { var response struct { Results []Apartment `json:"results"` } _, err := c.httpClient.R(). SetQueryParam("q", query). SetHeader("Authorization", "Bearer "+c.apiKey). SetResult(&response). SetRetryCount(3). SetRetryWaitTime(1 * time.Second). Get("/search") return response.Results, err }
Let's define our Apartment
struct:
type Apartment struct { ID string `json:"id"` Name string `json:"name"` Address string `json:"address"` Price float64 `json:"price"` Bedrooms int `json:"bedrooms"` }
Time for a quick CLI to test our integration:
func main() { client := NewClient("your-api-key-here") fmt.Print("Enter search query: ") query := "" fmt.Scanln(&query) apartments, err := client.SearchApartments(query) if err != nil { log.Fatalf("Error searching apartments: %v", err) } for _, apt := range apartments { fmt.Printf("%s - %s - $%.2f\n", apt.Name, apt.Address, apt.Price) } }
To take things up a notch, let's add some caching:
var cache = make(map[string][]Apartment) func (c *Client) SearchApartments(query string) ([]Apartment, error) { if cached, ok := cache[query]; ok { return cached, nil } // ... perform API call ... cache[query] = response.Results return response.Results, err }
Don't forget to test! Create a client_test.go
file:
func TestSearchApartments(t *testing.T) { client := NewClient("test-api-key") apartments, err := client.SearchApartments("New York") assert.NoError(t, err) assert.NotEmpty(t, apartments) }
And there you have it! You've just built a solid foundation for integrating the Apartments.com API into your Go applications. From here, you could expand on this by adding more endpoints, implementing a full-fledged web service, or even building a complete property management system.
Remember, the real estate data world is your oyster now. Happy coding, and may your apartments always be well-priced and perfectly located!