Skip to content

Commit

Permalink
first commit store service
Browse files Browse the repository at this point in the history
  • Loading branch information
gipsh committed Nov 13, 2023
0 parents commit 0740cd4
Show file tree
Hide file tree
Showing 7 changed files with 395 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Go API client for mpsdk

Mercadopago api SDK for golang
58 changes: 58 additions & 0 deletions client.go
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
}
64 changes: 64 additions & 0 deletions config.go
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
}
}
26 changes: 26 additions & 0 deletions example/main.go
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)
}

}
3 changes: 3 additions & 0 deletions go.mod
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
87 changes: 87 additions & 0 deletions model/store.go
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"`
}
Loading

0 comments on commit 0740cd4

Please sign in to comment.