mercadolibre/golang-sdk

Name: golang-sdk

Owner: MercadoLibre

Description: MercadoLibre's Golang SDK

Created: 2016-09-29 12:00:33.0

Updated: 2017-12-13 21:56:15.0

Pushed: 2016-11-22 21:08:36.0

Homepage: null

Size: 45

Language: Go

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

MercadoLibre's Golang SDK

This is the official Golang SDK for MercadoLibre's Platform.

How do I install it?

You can download the latest build from here.

How do I install it using go:

Just run the following command within your $GOPATH

et github.com/mercadolibre/golang-sdk/sdk

And that's it!

How do I start using it?

You can start integrating with https://api.mercadolibre.com by doing the following:

nt, err := sdk.Meli(ClientID, UserCode, ClientSecret, RedirectURL)

ClientID: Is the id that was given to you when you registered your application by using Application Manager. For additional help you can access our Developer's Site.

ClientSecret: Is a secret which is created during the complition of the step above.

UserCode: Is a code which is asigned to a specific user when this one tryies to access a private mercadolibre API. It means that to get this code you will need to authenticate and authorize the user.

RedirectURL: This is the url where the user will be redirected once it is authorized by mercadolibre.

To obtain the UserCode named above you will have to redirect the user to a specific url. To build this url, you can use the following code:

:= sdk.GetAuthURL(ClientID, sdk.AuthURLMLA, "https://www.example.com")

As a result, you will need to somehow make the user to enter his/her credentials in that URL. Once mercadolibre api authenticates the user, a redirection url will be returned and the UserCode will come attached to it. (i.e https://www.example.com?code=TG-57f2b6c7e4b08aea0070353e-214509008)

Warning: This UserCode needs to be parsed and kept by your application in order to be used for later instantiate the Meli client.

Now you can instantiate another `Meli` object, but this time this object will allow you to access the private API and also will manage the token refreshing, so you do not need to worrie about this handshake**

There are some design considerations worth to mention.

This SDK is just a thin layer on top of an http client to handle all the OAuth WebServer flow for you.

Making GET calls to public API
serCode can be empty since neither authorization nor authentication is needed.
nt, err := sdk.Meli(ClientID, UserCode, ClientSecret, "www.example.com")
, err := client.Get("/users/me")

rr != nil {
log.Printf("Error %s\n", err.Error())

Info, _:= ioutil.ReadAll(resp.Body)
Printf("response:%s\n", userInfo)
Making GET calls to a private API
client *sdk.Meli

client, err := sdk.Meli(ClientID, "", ClientSecret, redirectURL); err != nil {
log.Printf("Error: ", err.Error())
return


response *http.Response
esponse, err = client.Get("/users/me"); err != nil {
log.Printf("Error: ", err.Error())
return


f the API requires authorization you need to redirect the user.
nce the user enters his/her credentials, you need to use the UserCode to instantiate a new client, but this time it will be able to query private APIs.

esponse.StatusCode == http.StatusForbidden {
url := sdk.GetAuthURL(ClientID, sdk.AuthURLMLA, "www.example.com")
log.Printf("Returning Authentication URL:%s\n", url)
http.Redirect(w, r, url, 301)


nce the user was redirected and a UserCode was received:
client, err := sdk.Meli(ClientID, CODE_JUST_OBTEINED, ClientSecret, redirectURL); err != nil {
log.Printf("Error: ", err.Error())
return

Making POST calls
nt, err := sdk.Meli(ClientID, ClientCode, ClientSecret, "https://www.example.com")

 := "{\"title\":\"Item de test - No Ofertar\",\"category_id\":\"MLA1912\",\"price\":10,\"currency_id\":\"ARS\",\"available_quantity\":1,\"buying_mode\":\"buy_it_now\",\"listing_type_id\":\"bronze\",\"condition\":\"new\",\"description\": \"Item:,  Ray-Ban WAYFARER Gloss Black RB2140 901  Model: RB2140. Size: 50mm. Name: WAYFARER. Color: Gloss Black. Includes Ray-Ban Carrying Case and Cleaning Cloth. New in Box\",\"video_id\": \"YOUTUBE_ID_HERE\",\"warranty\": \"12 months by Ray Ban\",\"pictures\":[{\"source\":\"http://upload.wikimedia.org/wikipedia/commons/f/fd/Ray_Ban_Original_Wayfarer.jpg\"},{\"source\":\"http://en.wikipedia.org/wiki/File:Teashades.gif\"}]}"

, err = client.Post("/items", body)

rr != nil {
log.Printf("Error %s\n", err.Error())

Info, _= ioutil.ReadAll(resp.Body)
Printf("response:%s\n", userInfo)
Making PUT calls
nt, err := sdk.Meli(ClientID, ClientCode, ClientSecret, "https://www.example.com")
ge := "{\"available_quantity\": 6}"

, err = client.Put("/items/" + item.Id, &change)

rr != nil {
log.Printf("Error %s\n", err.Error())

Info, _= ioutil.ReadAll(resp.Body)
Printf("response:%s\n", userInfo)
Making DELETE calls
nt, err := sdk.Meli(ClientID, ClientCode, ClientSecret, "https://www.example.com")
nt.Delete("/items/123")
Community

You can contact us if you have questions using the standard communication channels described in the Developer's Forum.

I want to contribute!

That's great! Pull requests are very welcome!

Just fork the project in GitHub. Create a topic branch, write some code and add some tests for your new code. You can find some examples by taking a look at the main.go file.

To run the tests run `make test`.

Be aware: Please, make sure all tests are passing before committing any change. Any pull request with failing tests will be automatically discarded.

Thanks for helping!


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.