Skip to content

Commit

Permalink
Merge pull request #3 from kenriortega/dev
Browse files Browse the repository at this point in the history
ref: breaking changes of all file structures and main metods
  • Loading branch information
kenriortega authored Sep 30, 2021
2 parents 18b7dd8 + 57096ba commit 3692a66
Show file tree
Hide file tree
Showing 6 changed files with 363 additions and 281 deletions.
181 changes: 181 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package qvapay

import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
)

const (
ApiVersion = "v1"
BaseURL = "https://qvapay.com/api"
)

// Client is an interface that implements https://qvapay.com/api
type Client interface {
// GetInfo returns the corresponding object info on fetch call, or an error.
GetInfo(
ctx context.Context,
) (*AppInfoResponse, error)

// CreateInvoice ...
CreateInvoice(
ctx context.Context,
amount float64,
description string,
remoteID string,
) (*InvoiceResponse, error)

// GetTrasactions ...
GetTransactions(
ctx context.Context,
) (*TransactionsResponse, error)

// GetTransaction ...
GetTransaction(
ctx context.Context,
id string,
) (*TransactionReponse, error)

// GetBalance ...
GetBalance(
ctx context.Context,
) (float64, error)
}

// Client
// client represents a qvapay client. If the Debug field is set to an io.Writer
// (for example os.Stdout), then the client will dump API requests and responses
// to it. To use a non-default HTTP client (for example, for testing, or to set
// a timeout), assign to the HTTPClient field. To set a non-default URL (for
// example, for testing), assign to the URL field.
type client struct {
appID string
appSecret string
url string
httpClient *http.Client
debug io.Writer
}

func NewAPIClient(
// APP_ID
appID string,
// APP_SECRET
secretID string,
// mtss API's base url
baseURL string,
// skipVerify
skipVerify bool,
//optional, defaults to http.DefaultClient
httpClient *http.Client,
debug io.Writer,
) Client {

c := &client{
appID: appID,
appSecret: secretID,
url: baseURL,
httpClient: httpClient,
debug: debug,
}
if appID == "" {
c.appID = os.Getenv("APP_ID")
}
if secretID == "" {
c.appSecret = os.Getenv("APP_SECRET")
}
if baseURL == "" {
c.url = BaseURL
}
if httpClient != nil {
c.httpClient = httpClient
} else {
c.httpClient = http.DefaultClient
}
if skipVerify {
// #nosec
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
c.httpClient.Transport = tr
}
return c
}

// dumpResponse writes the raw response data to the debug output, if set, or
// standard error otherwise.
func (c *client) dumpResponse(resp *http.Response) {
// ignore errors dumping response - no recovery from this
responseDump, err := httputil.DumpResponse(resp, true)
if err != nil {
log.Fatalf("dumpResponse: " + err.Error())
}
fmt.Fprintln(c.debug, string(responseDump))
fmt.Fprintln(c.debug)
}

// apiCall define how you can make a call to Mtss API
func (c *client) apiCall(
ctx context.Context,
method string,
URL string,
data []byte,
) (statusCode int, response string, err error) {

req, err := http.NewRequest(method, URL, bytes.NewBuffer(data))
if err != nil {
return 0, "", fmt.Errorf("failed to create HTTP request: %v", err)
}
req.Header.Add("content-type", "application/json")
req.Header.Set("User-Agent", "qvapaygo-client/0.0")
if c.debug != nil {
requestDump, err := httputil.DumpRequestOut(req, true)
if err != nil {
return 0, "", fmt.Errorf("error dumping HTTP request: %v", err)
}
fmt.Fprintln(c.debug, string(requestDump))
fmt.Fprintln(c.debug)
}
req = req.WithContext(ctx)
resp, err := c.httpClient.Do(req)
if err != nil {
return 0, "", fmt.Errorf("HTTP request failed with: %v", err)
}
defer resp.Body.Close()
if c.debug != nil {
c.dumpResponse(resp)
}
res, err := ioutil.ReadAll(resp.Body)
if err != nil {
return resp.StatusCode, "", fmt.Errorf("HTTP request failed: %v", err)
}
return resp.StatusCode, string(res), nil
}

type APIPagingParams struct {
Count int
Page int
Order string
From string
To string
}

type APIQueryParams APIPagingParams

func formatParams(v url.Values, query APIPagingParams) url.Values {

if query.Page > 0 {
v.Add("page", fmt.Sprintf("%d", query.Page))
}

v.Encode()
return v
}
25 changes: 25 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Package qvapay is the no-official mtss SDK for Go.
Use it to interact with the Qvapay API.
func main() {
api := qvapay.NewAPIClient(
os.Getenv("APP_ID"), // app_id
os.Getenv("APP_SECRET"), // secret_id
qvapay.BaseURL, // constants url base https://qvapay.com/api
false, // skip verificationSSL
nil, // custom http.Client
nil, // debug io.Writter (os.Stdout)
)
info, err := api.GetInfo(context.Background())
if err != nil {
log.Fatalf(err.Error())
}
fmt.Println(info)
}
Examples can be found at
https://github.com/kenriortega/qvapay-go/tree/main/examples
If you find an issue with the SDK, please report through
https://github.com/kenriortega/qvapay-go/issues/new
*/
package qvapay
27 changes: 27 additions & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"context"
"fmt"
"log"
"os"

"github.com/kenriortega/qvapay-go"
)

func main() {
api := qvapay.NewAPIClient(
os.Getenv("APP_ID"), // app_id
os.Getenv("APP_SECRET"), // secret_id
qvapay.BaseURL, // constants url base https://qvapay.com/api
false, // skip verificationSSL
nil, // custom http.Client
nil, // debug io.Writter (os.Stdout)
)

info, err := api.GetInfo(context.Background())
if err != nil {
log.Fatalf(err.Error())
}
fmt.Println(info)
}
Loading

0 comments on commit 3692a66

Please sign in to comment.