Skip to content

Commit

Permalink
support LONGPORT_ prefix and support set region (#31)
Browse files Browse the repository at this point in the history
* support LONGPORT_ prefix and support set region

* fix code ident
  • Loading branch information
sunfuze authored Nov 28, 2023
1 parent 6f55b34 commit c15d7d1
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 124 deletions.
220 changes: 112 additions & 108 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Longbridge OpenAPI SDK for Go
# LONGPORT OpenAPI SDK for Go

`longbridge` provides an easy-to-use interface for invokes [`Longbridge OpenAPI`](https://open.longportapp.com/en/).
`LONGPORT` provides an easy-to-use interface for invokes [`LONGPORT OpenAPI`](https://open.longportapp.com/en/).

## Quickstart

Expand All @@ -13,17 +13,17 @@ import "github.com/longportapp/openapi-go"
_Setting environment variables(MacOS/Linux)_

```bash
export LONGBRIDGE_APP_KEY="App Key get from user center"
export LONGBRIDGE_APP_SECRET="App Secret get from user center"
export LONGBRIDGE_ACCESS_TOKEN="Access Token get from user center"
export LONGPORT_APP_KEY="App Key get from user center"
export LONGPORT_APP_SECRET="App Secret get from user center"
export LONGPORT_ACCESS_TOKEN="Access Token get from user center"
```

_Setting environment variables(Windows)_

```powershell
setx LONGBRIDGE_APP_KEY "App Key get from user center"
setx LONGBRIDGE_APP_SECRET "App Secret get from user center"
setx LONGBRIDGE_ACCESS_TOKEN "Access Token get from user center"
setx LONGPORT_APP_KEY "App Key get from user center"
setx LONGPORT_APP_SECRET "App Secret get from user center"
setx LONGPORT_ACCESS_TOKEN "Access Token get from user center"
```

## Config
Expand Down Expand Up @@ -61,11 +61,15 @@ func main() {
All envs is listed in the last of [README](#environment-variables)

### Load from file[yaml,toml]

yaml

```golang
conf, err := config.New(config.WithFilePath("./test.yaml"))
```

toml

```golang
conf, err := config.New(config.WithFilePath("./test.toml"))
```
Expand All @@ -76,22 +80,23 @@ Config structure as follow:

```golang
type Config struct {
HttpURL string `env:"LONGBRIDGE_HTTP_URL" yaml:"LONGBRIDGE_HTTP_URL" toml:"LONGBRIDGE_HTTP_URL"`
HTTPTimeout time.Duration `env:"LONGBRIDGE_HTTP_TIMEOUT" yaml:"LONGBRIDGE_HTTP_TIMEOUT" toml:"LONGBRIDGE_HTTP_TIMEOUT"`
AppKey string `env:"LONGBRIDGE_APP_KEY" yaml:"LONGBRIDGE_APP_KEY" toml:"LONGBRIDGE_APP_KEY"`
AppSecret string `env:"LONGBRIDGE_APP_SECRET" yaml:"LONGBRIDGE_APP_SECRET" toml:"LONGBRIDGE_APP_SECRET"`
AccessToken string `env:"LONGBRIDGE_ACCESS_TOKEN" yaml:"LONGBRIDGE_ACCESS_TOKEN" toml:"LONGBRIDGE_ACCESS_TOKEN"`
TradeUrl string `env:"LONGBRIDGE_TRADE_URL" yaml:"LONGBRIDGE_TRADE_URL" toml:"LONGBRIDGE_TRADE_URL"`
QuoteUrl string `env:"LONGBRIDGE_QUOTE_URL" yaml:"LONGBRIDGE_QUOTE_URL" toml:"LONGBRIDGE_QUOTE_URL"`
HttpURL string `env:"LONGPORT_HTTP_URL" yaml:"LONGPORT_HTTP_URL" toml:"LONGPORT_HTTP_URL"`
HTTPTimeout time.Duration `env:"LONGPORT_HTTP_TIMEOUT" yaml:"LONGPORT_HTTP_TIMEOUT" toml:"LONGPORT_HTTP_TIMEOUT"`
AppKey string `env:"LONGPORT_APP_KEY" yaml:"LONGPORT_APP_KEY" toml:"LONGPORT_APP_KEY"`
AppSecret string `env:"LONGPORT_APP_SECRET" yaml:"LONGPORT_APP_SECRET" toml:"LONGPORT_APP_SECRET"`
AccessToken string `env:"LONGPORT_ACCESS_TOKEN" yaml:"LONGPORT_ACCESS_TOKEN" toml:"LONGPORT_ACCESS_TOKEN"`
TradeUrl string `env:"LONGPORT_TRADE_URL" yaml:"LONGPORT_TRADE_URL" toml:"LONGPORT_TRADE_URL"`
QuoteUrl string `env:"LONGPORT_QUOTE_URL" yaml:"LONGPORT_QUOTE_URL" toml:"LONGPORT_QUOTE_URL"`

LogLevel string `env:"LONGBRIDGE_LOG_LEVEL" yaml:"LONGBRIDGE_LOG_LEVEL" toml:"LONGBRIDGE_LOG_LEVEL"`
// longbridge protocol config
AuthTimeout time.Duration `env:"LONGBRIDGE_AUTH_TIMEOUT" yaml:"LONGBRIDGE_AUTH_TIMEOUT"toml:"LONGBRIDGE_AUTH_TIMEOUT"`
Timeout time.Duration `env:"LONGBRIDGE_TIMEOUT" yaml:"LONGBRIDGE_TIMEOUT" toml:"LONGBRIDGE_TIMEOUT"`
WriteQueueSize int `env:"LONGBRIDGE_WRITE_QUEUE_SIZE" yaml:"LONGBRIDGE_WRITE_QUEUE_SIZE" toml:"LONGBRIDGE_WRITE_QUEUE_SIZE"`
ReadQueueSize int `env:"LONGBRIDGE_READ_QUEUE_SIZE" yaml:"LONGBRIDGE_READ_QUEUE_SIZE" toml:"LONGBRIDGE_READ_QUEUE_SIZE"`
ReadBufferSize int `env:"LONGBRIDGE_READ_BUFFER_SIZE" yaml:"LONGBRIDGE_READ_BUFFER_SIZE" toml:"LONGBRIDGE_READ_BUFFER_SIZE"`
MinGzipSize int `env:"LONGBRIDGE_MIN_GZIP_SIZE" yaml:"LONGBRIDGE_MIN_GZIP_SIZE" toml:"LONGBRIDGE_MIN_GZIP_SIZE"`
LogLevel string `env:"LONGPORT_LOG_LEVEL" yaml:"LONGPORT_LOG_LEVEL" toml:"LONGPORT_LOG_LEVEL"`
// LONGPORT protocol config
AuthTimeout time.Duration `env:"LONGPORT_AUTH_TIMEOUT" yaml:"LONGPORT_AUTH_TIMEOUT"toml:"LONGPORT_AUTH_TIMEOUT"`
Timeout time.Duration `env:"LONGPORT_TIMEOUT" yaml:"LONGPORT_TIMEOUT" toml:"LONGPORT_TIMEOUT"`
WriteQueueSize int `env:"LONGPORT_WRITE_QUEUE_SIZE" yaml:"LONGPORT_WRITE_QUEUE_SIZE" toml:"LONGPORT_WRITE_QUEUE_SIZE"`
ReadQueueSize int `env:"LONGPORT_READ_QUEUE_SIZE" yaml:"LONGPORT_READ_QUEUE_SIZE" toml:"LONGPORT_READ_QUEUE_SIZE"`
ReadBufferSize int `env:"LONGPORT_READ_BUFFER_SIZE" yaml:"LONGPORT_READ_BUFFER_SIZE" toml:"LONGPORT_READ_BUFFER_SIZE"`
MinGzipSize int `env:"LONGPORT_MIN_GZIP_SIZE" yaml:"LONGPORT_MIN_GZIP_SIZE" toml:"LONGPORT_MIN_GZIP_SIZE"`
Region Region `env:"LONGPORT_REGION" yaml:"LONGPORT_REGION" toml:"LONGPORT_REGION"`
}

```
Expand All @@ -112,22 +117,21 @@ Our logger interface as follow:

```golang
type Logger interface {
SetLevel(string)
Info(msg string)
Error(msg string)
Warn(msg string)
Debug(msg string)
Infof(msg string, args ...interface{})
Errorf(msg string, args ...interface{})
Warnf(msg string, args ...interface{})
Debugf(msg string, args ...interface{})
SetLevel(string)
Info(msg string)
Error(msg string)
Warn(msg string)
Debug(msg string)
Infof(msg string, args ...interface{})
Errorf(msg string, args ...interface{})
Warnf(msg string, args ...interface{})
Debugf(msg string, args ...interface{})
}

```

Your can use you own logger by imply the interface


```golang
c, err := config.New()

Expand Down Expand Up @@ -156,42 +160,41 @@ c.Client = &http.Client{

```


## Quote API (Get basic information of securities)

```golang
package main

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

"github.com/longportapp/openapi-go/quote"
"github.com/longportapp/openapi-go/config"
"github.com/longportapp/openapi-go/quote"
"github.com/longportapp/openapi-go/config"
)

func main() {
conf, err := config.New()
if err != nil {
log.Fatal(err)
return
}
// create quote context from environment variables
quoteContext, err := quote.NewFromCfg(conf)
if err != nil {
log.Fatal(err)
return
}
defer quoteContext.Close()
ctx := context.Background()
// Get basic information of securities
quotes, err := quoteContext.Quote(ctx, []string{"700.HK", "AAPL.US", "TSLA.US", "NFLX.US"})
if err != nil {
log.Fatal(err)
return
}
fmt.Printf("quotes: %v", quotes)
conf, err := config.New()
if err != nil {
log.Fatal(err)
return
}
// create quote context from environment variables
quoteContext, err := quote.NewFromCfg(conf)
if err != nil {
log.Fatal(err)
return
}
defer quoteContext.Close()
ctx := context.Background()
// Get basic information of securities
quotes, err := quoteContext.Quote(ctx, []string{"700.HK", "AAPL.US", "TSLA.US", "NFLX.US"})
if err != nil {
log.Fatal(err)
return
}
fmt.Printf("quotes: %v", quotes)
}
```

Expand All @@ -201,44 +204,44 @@ func main() {
package main

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

"github.com/longportapp/openapi-go/trade"
"github.com/longportapp/openapi-go/config"
"github.com/shopspring/decimal"
"github.com/longportapp/openapi-go/trade"
"github.com/longportapp/openapi-go/config"
"github.com/shopspring/decimal"
)

func main() {
conf, err := config.New()
if err != nil {
log.Fatal(err)
return
}
// create trade context from environment variables
tradeContext, err := trade.NewFromCfg(conf)
if err != nil {
log.Fatal(err)
return
}
defer tradeContext.Close()
ctx := context.Background()
// submit order
order := &trade.SubmitOrder{
Symbol: "700.HK",
OrderType: trade.OrderTypeLO,
Side: trade.OrderSideBuy,
SubmittedQuantity: 200,
TimeInForce: trade.TimeTypeDay,
SubmittedPrice: decimal.NewFromFloat(12),
}
orderId, err := tradeContext.SubmitOrder(ctx, order)
if err != nil {
log.Fatal(err)
return
}
fmt.Printf("orderId: %v\n", orderId)
conf, err := config.New()
if err != nil {
log.Fatal(err)
return
}
// create trade context from environment variables
tradeContext, err := trade.NewFromCfg(conf)
if err != nil {
log.Fatal(err)
return
}
defer tradeContext.Close()
ctx := context.Background()
// submit order
order := &trade.SubmitOrder{
Symbol: "700.HK",
OrderType: trade.OrderTypeLO,
Side: trade.OrderSideBuy,
SubmittedQuantity: 200,
TimeInForce: trade.TimeTypeDay,
SubmittedPrice: decimal.NewFromFloat(12),
}
orderId, err := tradeContext.SubmitOrder(ctx, order)
if err != nil {
log.Fatal(err)
return
}
fmt.Printf("orderId: %v\n", orderId)
}
```

Expand All @@ -248,23 +251,24 @@ Support load env from `.env` file.

| name | description | default value | example |
|-----------------------------|------------------------------------------------|---------------------------------------|---------|
| LONGBRIDGE_HTTP_URL | longbridge rest api url | https://openapi.longportapp.com | |
| LONGBRIDGE_APP_KEY | app key | | |
| LONGBRIDGE_APP_SECRET | app secret | | |
| LONGBRIDGE_ACCESS_TOKEN | access token | | |
| LONGBRIDGE_TRADE_URL | longbridge protocol url for trade context | wss://openapi-trade.longportapp.com | |
| LONGBRIDGE_QUOTE_URL | longbridge protocol url for quote context | wss://openapi-quote.longportapp.com | |
| LONGBRIDGE_LOG_LEVEL | log level | info | |
| LONGBRIDGE_AUTH_TIMEOUT | longbridge protocol authorize request time out | 10 second | 10s |
| LONGBRIDGE_TIMEOUT | longbridge protocol dial timeout | 5 second | 6s |
| LONGBRIDGE_WRITE_QUEUE_SIZE | longbirdge protocol write queue size | 16 | |
| LONGBRIDGE_READ_QUEUE_SIZE | longbirdge protocol read queue size | 16 | |
| LONGBRIDGE_READ_BUFFER_SIZE | longbirdge protocol read buffer size | 4096 | |
| LONGBRIDGE_MIN_GZIP_SIZE | longbirdge protocol minimal gzip size | 1024 | |
| LONGPORT_REGION | Set access region, if region equals `cn`, sdk will set httpUrl, quoteUrl, tradeUrl to China endpoints | - | cn |
| LONGPORT_HTTP_URL | LONGPORT rest api url | <https://openapi.longportapp.com> | |
| LONGPORT_APP_KEY | app key | | |
| LONGPORT_APP_SECRET | app secret | | |
| LONGPORT_ACCESS_TOKEN | access token | | |
| LONGPORT_TRADE_URL | LONGPORT protocol url for trade context | wss://openapi-trade.longportapp.com | |
| LONGPORT_QUOTE_URL | LONGPORT protocol url for quote context | wss://openapi-quote.longportapp.com | |
| LONGPORT_LOG_LEVEL | log level | info | |
| LONGPORT_AUTH_TIMEOUT | LONGPORT protocol authorize request time out | 10 second | 10s |
| LONGPORT_TIMEOUT | LONGPORT protocol dial timeout | 5 second | 6s |
| LONGPORT_WRITE_QUEUE_SIZE | longport protocol write queue size | 16 | |
| LONGPORT_READ_QUEUE_SIZE | longport protocol read queue size | 16 | |
| LONGPORT_READ_BUFFER_SIZE | longport protocol read buffer size | 4096 | |
| LONGPORT_MIN_GZIP_SIZE | longport protocol minimal gzip size | 1024 | |

## License

Licensed under either of

* Apache License, Version 2.0,([LICENSE-APACHE](./LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](./LICENSE-MIT) or http://opensource.org/licenses/MIT) at your option.
* Apache License, Version 2.0,([LICENSE-APACHE](./LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>)
* MIT license ([LICENSE-MIT](./LICENSE-MIT) or <http://opensource.org/licenses/MIT>) at your option.
50 changes: 34 additions & 16 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"net/http"
"time"

"github.com/longportapp/openapi-go/log"

_ "github.com/joho/godotenv/autoload"
"github.com/pkg/errors"

"github.com/longportapp/openapi-go/log"
)

type IConfig interface {
Expand All @@ -20,29 +20,40 @@ var configTypeMap = map[ConfigType]IConfig{
ConfigTypeTOML: &TOMLConfig{},
}

type Region string

var (
RegionCN Region = "cn"

cnHttpUrl = "https://openapi.longportapp.cn"
cnQuoteUrl = "wss://openapi-quote.longportapp.cn"
cnTradeUrl = "wss://openapi-trade.longportapp.cn"
)

// Config store Longbridge config
type Config struct {
// Client custom http client
Client *http.Client

HttpURL string `env:"LONGBRIDGE_HTTP_URL" yaml:"LONGBRIDGE_HTTP_URL" toml:"LONGBRIDGE_HTTP_URL"`
HTTPTimeout time.Duration `env:"LONGBRIDGE_HTTP_TIMEOUT" yaml:"LONGBRIDGE_HTTP_TIMEOUT" toml:"LONGBRIDGE_HTTP_TIMEOUT"`
AppKey string `env:"LONGBRIDGE_APP_KEY" yaml:"LONGBRIDGE_APP_KEY" toml:"LONGBRIDGE_APP_KEY"`
AppSecret string `env:"LONGBRIDGE_APP_SECRET" yaml:"LONGBRIDGE_APP_SECRET" toml:"LONGBRIDGE_APP_SECRET"`
AccessToken string `env:"LONGBRIDGE_ACCESS_TOKEN" yaml:"LONGBRIDGE_ACCESS_TOKEN" toml:"LONGBRIDGE_ACCESS_TOKEN"`
TradeUrl string `env:"LONGBRIDGE_TRADE_URL" yaml:"LONGBRIDGE_TRADE_URL" toml:"LONGBRIDGE_TRADE_URL"`
QuoteUrl string `env:"LONGBRIDGE_QUOTE_URL" yaml:"LONGBRIDGE_QUOTE_URL" toml:"LONGBRIDGE_QUOTE_URL"`
HttpURL string `env:"LONGBRIDGE_HTTP_URL,LONGPORT_HTTP_URL" yaml:"LONGBRIDGE_HTTP_URL,LONGPORT_HTTP_URL" toml:"LONGBRIDGE_HTTP_URL,LONGPORT_HTTP_URL"`
HTTPTimeout time.Duration `env:"LONGBRIDGE_HTTP_TIMEOUT,LONGPORT_HTTP_TIMEOUT" yaml:"LONGBRIDGE_HTTP_TIMEOUT,LONGPORT_HTTP_TIMEOUT" toml:"LONGPORT_HTTP_TIMEOUT"`
AppKey string `env:"LONGBRIDGE_APP_KEY,LONGPORT_APP_KEY" yaml:"LONGBRIDGE_APP_KEY,LONGPORT_APP_KEY" toml:"LONGBRIDGE_APP_KEY,LONGPORT_APP_KEY"`
AppSecret string `env:"LONGBRIDGE_APP_SECRET,LONGPORT_APP_SECRET" yaml:"eONGBRIDGE_APP_SECRET,LONGPORT_APP_SECRET" toml:"LONGBRIDGE_APP_SECRET,LONGPORT_APP_SECRET"`
AccessToken string `env:"LONGBRIDGE_ACCESS_TOKEN,LONGPORT_ACCESS_TOKEN" yaml:"LONGBRIDGE_ACCESS_TOKEN,LONGPORT_ACCESS_TOKEN" toml:"LONGBRIDGE_ACCESS_TOKEN,LONGPORT_ACCESS_TOKEN"`
TradeUrl string `env:"LONGBRIDGE_TRADE_URL,LONGPORT_TRADE_URL" yaml:"LONGBRIDGE_TRADE_URL,LONGPORT_TRADE_URL" toml:"LONGBRIDGE_TRADE_URL,LONGPORT_TRADE_URL"`
QuoteUrl string `env:"LONGBRIDGE_QUOTE_URL,LONGPORT_QUOTE_URL" yaml:"LONGBRIDGE_QUOTE_URL,LONGPORT_QUOTE_URL" toml:"LONGBRIDGE_QUOTE_URL,LONGPORT_QUOTE_URL"`

LogLevel string `env:"LONGBRIDGE_LOG_LEVEL" yaml:"LONGBRIDGE_LOG_LEVEL" toml:"LONGBRIDGE_LOG_LEVEL"`
LogLevel string `env:"LONGBRIDGE_LOG_LEVEL,LONGPORT_LOG_LEVEL" yaml:"LONGBRIDGE_LOG_LEVEL,LONGPORT_LOG_LEVEL" toml:"LONGBRIDGE_LOG_LEVEL,LONGPORT_LOG_LEVEL"`
logger log.Logger

// longbridge protocol config
AuthTimeout time.Duration `env:"LONGBRIDGE_AUTH_TIMEOUT" yaml:"LONGBRIDGE_AUTH_TIMEOUT"toml:"LONGBRIDGE_AUTH_TIMEOUT"`
Timeout time.Duration `env:"LONGBRIDGE_TIMEOUT" yaml:"LONGBRIDGE_TIMEOUT" toml:"LONGBRIDGE_TIMEOUT"`
WriteQueueSize int `env:"LONGBRIDGE_WRITE_QUEUE_SIZE" yaml:"LONGBRIDGE_WRITE_QUEUE_SIZE" toml:"LONGBRIDGE_WRITE_QUEUE_SIZE"`
ReadQueueSize int `env:"LONGBRIDGE_READ_QUEUE_SIZE" yaml:"LONGBRIDGE_READ_QUEUE_SIZE" toml:"LONGBRIDGE_READ_QUEUE_SIZE"`
ReadBufferSize int `env:"LONGBRIDGE_READ_BUFFER_SIZE" yaml:"LONGBRIDGE_READ_BUFFER_SIZE" toml:"LONGBRIDGE_READ_BUFFER_SIZE"`
MinGzipSize int `env:"LONGBRIDGE_MIN_GZIP_SIZE" yaml:"LONGBRIDGE_MIN_GZIP_SIZE" toml:"LONGBRIDGE_MIN_GZIP_SIZE"`
AuthTimeout time.Duration `env:"LONGBRIDGE_AUTH_TIMEOUT,LONGPORT_AUTH_TIMEOUT" yaml:"LONGBRIDGE_AUTH_TIMEOUT,LONGPORT_AUTH_TIMEOUT" toml:"LONGBRIDGE_AUTH_TIMEOUT,LONGPORT_AUTH_TIMEOUT"`
Timeout time.Duration `env:"LONGBRIDGE_TIMEOUT,LONGPORT_TIMEOUT" yaml:"LONGBRIDGE_TIMEOUT,LONGPORT_TIMEOUT" toml:"LONGBRIDGE_TIMEOUT,LONGPORT_TIMEOUT"`
WriteQueueSize int `env:"LONGBRIDGE_WRITE_QUEUE_SIZE,LONGPORT_WRITE_QUEUE_SIZE" yaml:"LONGBRIDGE_WRITE_QUEUE_SIZE,LONGPORT_WRITE_QUEUE_SIZE" toml:"LONGBRIDGE_WRITE_QUEUE_SIZE,LONGPORT_WRITE_QUEUE_SIZE"`
ReadQueueSize int `env:"LONGBRIDGE_READ_QUEUE_SIZE,LONGPORT_READ_QUEUE_SIZE" yaml:"LONGBRIDGE_READ_QUEUE_SIZE,LONGPORT_READ_QUEUE_SIZE" toml:"LONGBRIDGE_READ_QUEUE_SIZE,LONGPORT_READ_QUEUE_SIZE"`
ReadBufferSize int `env:"LONGBRIDGE_READ_BUFFER_SIZE,LONGPORT_READ_BUFFER_SIZE" yaml:"LONGBRIDGE_READ_BUFFER_SIZE,LONGPORT_READ_BUFFER_SIZE" toml:"LONGBRIDGE_READ_BUFFER_SIZE,LONGPORT_READ_BUFFER_SIZE"`
MinGzipSize int `env:"LONGBRIDGE_MIN_GZIP_SIZE,LONGPORT_MIN_GZIP_SIZE" yaml:"LONGBRIDGE_MIN_GZIP_SIZE,LONGPORT_MIN_GZIP_SIZE" toml:"LONGBRIDGE_MIN_GZIP_SIZE,LONGPORT_MIN_GZIP_SIZE"`
Region Region `env:"LONGPORT_REGION" yaml:"LONGPORT_REGION" toml:"LONGPORT_REGION"`
}

func (c *Config) SetLogger(l log.Logger) {
Expand All @@ -69,6 +80,13 @@ func New(opts ...Option) (configData *Config, err error) {
err = errors.Wrapf(err, "GetConfig err")
return
}

if configData.Region == RegionCN {
configData.HttpURL = cnHttpUrl
configData.QuoteUrl = cnQuoteUrl
configData.TradeUrl = cnTradeUrl
}

err = configData.check()
if err != nil {
err = errors.Wrapf(err, "New config check err")
Expand Down

0 comments on commit c15d7d1

Please sign in to comment.