-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0740cd4
Showing
7 changed files
with
395 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Go API client for mpsdk | ||
|
||
Mercadopago api SDK for golang |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package mpsdkgo | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"net/http/httputil" | ||
) | ||
|
||
type ApiClient struct { | ||
cfg *Config | ||
Store *StoreService | ||
BaseURL string | ||
} | ||
|
||
func NewApiClient(cfg *Config) *ApiClient { | ||
if cfg.HTTPClient == nil { | ||
cfg.HTTPClient = http.DefaultClient | ||
} | ||
|
||
client := &ApiClient{ | ||
cfg: cfg, | ||
BaseURL: fmt.Sprintf("%s://%s/", cfg.Scheme, cfg.Host), | ||
} | ||
|
||
client.Store = NewStoreService(client) | ||
|
||
return client | ||
|
||
} | ||
|
||
func (c *ApiClient) callAPI(request *http.Request) (*http.Response, error) { | ||
for k, v := range c.cfg.DefaultHeader { | ||
request.Header.Add(k, v) | ||
} | ||
|
||
if c.cfg.Debug { | ||
dump, err := httputil.DumpRequestOut(request, true) | ||
if err != nil { | ||
return nil, err | ||
} | ||
log.Printf("\n%s\n", string(dump)) | ||
} | ||
|
||
resp, err := c.cfg.HTTPClient.Do(request) | ||
if err != nil { | ||
return resp, err | ||
} | ||
|
||
if c.cfg.Debug { | ||
dump, err := httputil.DumpResponse(resp, true) | ||
if err != nil { | ||
return resp, err | ||
} | ||
log.Printf("\n%s\n", string(dump)) | ||
} | ||
return resp, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package mpsdkgo | ||
|
||
import "net/http" | ||
|
||
type ServerVariable struct { | ||
Description string | ||
DefaultValue string | ||
EnumValues []string | ||
} | ||
|
||
// ServerConfiguration stores the information about a server | ||
type ServerConfiguration struct { | ||
URL string | ||
Description string | ||
Variables map[string]ServerVariable | ||
} | ||
|
||
type ServerConfigurations []ServerConfiguration | ||
|
||
type Config struct { | ||
Host string `json:"host,omitempty"` | ||
Scheme string `json:"scheme,omitempty"` | ||
DefaultHeader map[string]string `json:"defaultHeader,omitempty"` | ||
UserAgent string `json:"userAgent,omitempty"` | ||
Debug bool `json:"debug,omitempty"` | ||
HTTPClient *http.Client | ||
} | ||
|
||
// ServerVariable stores the information about a server variab | ||
// NewConfiguration returns a new Configuration object | ||
func NewConfiguration(options ...func(*Config)) *Config { | ||
cfg := &Config{ | ||
DefaultHeader: make(map[string]string), | ||
UserAgent: "OpenAPI-Generator/0.0.4/go", | ||
Debug: false, | ||
Scheme: "https", | ||
Host: "api.mercadopago.com", | ||
} | ||
|
||
for _, o := range options { | ||
o(cfg) | ||
} | ||
|
||
return cfg | ||
} | ||
|
||
type ConfigOptions struct { | ||
Token string | ||
Debug bool | ||
} | ||
|
||
type ConfigOption func(*Config) | ||
|
||
func WithDebug(debug bool) ConfigOption { | ||
return func(o *Config) { | ||
o.Debug = debug | ||
} | ||
} | ||
|
||
func WithToken(token string) ConfigOption { | ||
return func(o *Config) { | ||
o.DefaultHeader["Authorization"] = "Bearer " + token | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
mpsdkgo "github.com/vudu-people/mp-sdk-go" | ||
) | ||
|
||
func main() { | ||
|
||
cfg := mpsdkgo.NewConfiguration(mpsdkgo.WithDebug(false), mpsdkgo.WithToken("TEST-11233")) | ||
|
||
client := mpsdkgo.NewApiClient(cfg) | ||
|
||
stores, err := client.Store.GetStores("12345", 10) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(stores.Paging.Total) | ||
|
||
for _, store := range stores.Results { | ||
fmt.Println(store.Name) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/vudu-people/mp-sdk-go | ||
|
||
go 1.20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package model | ||
|
||
import "time" | ||
|
||
type CreateStoreRequest struct { | ||
Name string `json:"name"` | ||
BusinessHours struct { | ||
Monday []struct { | ||
Open string `json:"open"` | ||
Close string `json:"close"` | ||
} `json:"monday,omitempty"` | ||
Tuesday []struct { | ||
Open string `json:"open"` | ||
Close string `json:"close"` | ||
} `json:"tuesday,omitempty"` | ||
Wednesday []struct { | ||
Open string `json:"open"` | ||
Close string `json:"close"` | ||
} `json:"wednesday,omitempty"` | ||
Thursday []struct { | ||
Open string `json:"open"` | ||
Close string `json:"close"` | ||
} `json:"thursday,omitempty"` | ||
Friday []struct { | ||
Open string `json:"open"` | ||
Close string `json:"close"` | ||
} `json:"friday,omitempty"` | ||
Saturday []struct { | ||
Open string `json:"open"` | ||
Close string `json:"close"` | ||
} `json:"saturday,omitempty"` | ||
Sunday []struct { | ||
Open string `json:"open"` | ||
Close string `json:"close"` | ||
} `json:"sunday,omitempty"` | ||
} `json:"business_hours"` | ||
Location struct { | ||
StreetNumber string `json:"street_number"` | ||
StreetName string `json:"street_name"` | ||
CityName string `json:"city_name"` | ||
StateName string `json:"state_name"` | ||
Latitude float64 `json:"latitude"` | ||
Longitude float64 `json:"longitude"` | ||
Reference string `json:"reference"` | ||
} `json:"location"` | ||
ExternalID string `json:"external_id"` | ||
} | ||
|
||
type CreateStoreResponse struct { | ||
ID int `json:"id"` | ||
Name string `json:"name"` | ||
DateCreated time.Time `json:"date_created"` | ||
BusinessHours struct { | ||
Monday []struct { | ||
Open string `json:"open"` | ||
Close string `json:"close"` | ||
} `json:"monday"` | ||
Tuesday []struct { | ||
Open string `json:"open"` | ||
Close string `json:"close"` | ||
} `json:"tuesday"` | ||
} `json:"business_hours"` | ||
Location struct { | ||
AddressLine string `json:"address_line"` | ||
Latitude float64 `json:"latitude"` | ||
Longitude float64 `json:"longitude"` | ||
Reference string `json:"reference"` | ||
} `json:"location"` | ||
ExternalID string `json:"external_id"` | ||
} | ||
|
||
type GetStoreResponse struct { | ||
CreateStoreResponse | ||
} | ||
|
||
type UpdateStoreRequest struct { | ||
CreateStoreRequest | ||
} | ||
|
||
type GetStoresResponse struct { | ||
Paging struct { | ||
Total int `json:"total"` | ||
Offset int `json:"offset"` | ||
Limit int `json:"limit"` | ||
} `json:"paging"` | ||
Results []CreateStoreResponse `json:"results"` | ||
} |
Oops, something went wrong.