Initial success talking to lob directly with my own client
This commit is contained in:
parent
c0935c848b
commit
97ec0667a5
2 changed files with 86 additions and 0 deletions
26
lob/cmd/address-list/main.go
Normal file
26
lob/cmd/address-list/main.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/Gleipnir-Technology/nidus-sync/lob"
|
||||
)
|
||||
|
||||
func main() {
|
||||
key := os.Getenv("LOB_API_KEY")
|
||||
if key == "" {
|
||||
log.Println("LOB_API_KEY is empty")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
client := lob.NewLob(key)
|
||||
ctx := context.TODO()
|
||||
_, err := client.AddressList(ctx)
|
||||
if err != nil {
|
||||
log.Printf("err: %v", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
}
|
||||
60
lob/lob.go
Normal file
60
lob/lob.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package lob
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"resty.dev/v3"
|
||||
)
|
||||
|
||||
type Lob struct {
|
||||
APIKey string
|
||||
|
||||
client *resty.Client
|
||||
urlBaseApi string
|
||||
}
|
||||
|
||||
func NewLob(api_key string) *Lob {
|
||||
r := resty.New()
|
||||
if os.Getenv("LOB_INSECURE_SKIP_VERIFY") != "" {
|
||||
log.Warn().Msg("Using insecure TLS verification settings")
|
||||
r.SetTLSClientConfig(&tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
})
|
||||
}
|
||||
r.SetBasicAuth(api_key, "")
|
||||
l := &Lob{
|
||||
APIKey: api_key,
|
||||
client: r,
|
||||
urlBaseApi: "api.lob.com",
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
type Address struct{}
|
||||
type ResponseAddressList struct {
|
||||
Addresses []Address `json:"data"`
|
||||
Count int `json:"count"`
|
||||
CountTotal int `json:"total_count"`
|
||||
}
|
||||
|
||||
func (l *Lob) AddressList(ctx context.Context) ([]*Address, error) {
|
||||
var result ResponseAddressList
|
||||
|
||||
resp, err := l.client.R().
|
||||
//SetQueryParamsFromValues(query).
|
||||
SetContext(ctx).
|
||||
SetResult(&result).
|
||||
SetPathParam("urlBase", l.urlBaseApi).
|
||||
Get("https://{urlBase}/v1/addresses")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("address list get: %w", err)
|
||||
}
|
||||
if !resp.IsSuccess() {
|
||||
return nil, fmt.Errorf("not successful")
|
||||
}
|
||||
return []*Address{}, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue