Hey there, fellow developer! Ready to dive into the world of Mercado Libre API integration? You're in for a treat. This guide will walk you through building a robust integration using Go, giving you the power to tap into Latin America's largest e-commerce ecosystem. Let's get cracking!
Before we jump in, make sure you've got:
Let's kick things off:
mkdir meli-integration && cd meli-integration go mod init github.com/yourusername/meli-integration
Now, let's grab the essentials:
go get github.com/go-resty/resty/v2
Alright, time to get that access token. Mercado Libre uses OAuth 2.0, so let's implement a quick flow:
import ( "github.com/go-resty/resty/v2" ) func getAccessToken(clientID, clientSecret, code string) (string, error) { client := resty.New() resp, err := client.R(). SetFormData(map[string]string{ "grant_type": "authorization_code", "client_id": clientID, "client_secret": clientSecret, "code": code, }). Post("https://api.mercadolibre.com/oauth/token") // Parse response and return token }
Now that we're authenticated, let's set up our client:
func newMeliClient(accessToken string) *resty.Client { return resty.New(). SetHeader("Authorization", "Bearer "+accessToken). SetBaseURL("https://api.mercadolibre.com") }
Let's tackle some core functionality:
func searchProducts(client *resty.Client, query string) ([]Product, error) { resp, err := client.R(). SetQueryParam("q", query). Get("/sites/MLB/search") // Parse response and return products } func getItemDetails(client *resty.Client, itemID string) (*Item, error) { resp, err := client.R(). Get("/items/" + itemID) // Parse response and return item details }
Don't forget to play nice with the API:
func makeRequest(client *resty.Client, method, url string) (*resty.Response, error) { resp, err := client.R().Execute(method, url) if err != nil { return nil, err } if resp.StatusCode() == 429 { // Implement backoff strategy } return resp, nil }
Let's make sense of those JSON responses:
type Product struct { ID string `json:"id"` Title string `json:"title"` Price float64 `json:"price"` } func parseProducts(data []byte) ([]Product, error) { var result struct { Results []Product `json:"results"` } err := json.Unmarshal(data, &result) return result.Results, err }
Time to make sure everything's working smoothly:
func TestSearchProducts(t *testing.T) { // Mock API response httpmock.Activate() defer httpmock.DeactivateAndReset() httpmock.RegisterResponder("GET", "https://api.mercadolibre.com/sites/MLB/search", httpmock.NewStringResponder(200, `{"results": [{"id": "123", "title": "Test Product", "price": 99.99}]}`)) client := newMeliClient("fake_token") products, err := searchProducts(client, "test") assert.NoError(t, err) assert.Len(t, products, 1) assert.Equal(t, "Test Product", products[0].Title) }
To squeeze out every bit of performance:
And there you have it! You've just built a solid foundation for your Mercado Libre API integration in Go. Remember, this is just the beginning – there's a whole world of possibilities to explore with the API. Keep experimenting, and don't hesitate to dive into the official docs for more advanced features.
Happy coding, and may your integration be ever scalable!