To add a new endpoint to the Market Data Go SDK, follow these steps:
Add the new endpoint to the endpoints map in endpoints.go. Specify the version and category it belongs to.
// Example for adding a new "futures" quote in version 1
1: {
"futures": {
"quotes": "/v1/futures/quotes/{symbol}/",
},
Add a type assertion for the new request type.
// Example for adding a new FuturesQuoteRequest in version 1
if fqr, ok := br.child.(*FuturesQuoteRequest); ok {
params, err := fqr.getParams()
if err != nil {
return nil, err
}
return params, nil
}
- Define a struct in the
client
package to represent the new request. Use the existing requests as a guide. - Attach the existing parameters, or design new parameters in the
helpers/parameters
package. Most common parameters are already defined. - Create setter methods for the request struct that passes throught the parameters to the
helpers/parameters
that you've imported to use. These methods return a pointer to the struct and store errors, allowing the user to use the builder pattern to define the parameters. - Create a function to initialize a new empty request that attaches the default client or a user-provided client.
package client
import (
"github.com/MarketDataApp/sdk-go/helpers/parameters"
)
// FuturesQuoteRequest is the struct that sets/stores the request parameters.
type FuturesQuoteRequest struct {
*baseRequest
symbolParams *parameters.SymbolParams
}
// Symbol sets the FuturesQuoteRequest ticket symbol.
func (fqr *FuturesQuoteRequest) Symbol(q string) *FuturesQuoteRequest {
if fqr == nil {
return nil
}
err := fqr.symbolParams.SetSymbol(q)
if err != nil {
fqr.Error = err
}
return fqr
}
// FuturesQuote initializes a new FuturesQuoteRequest.
func FuturesQuote(client ...*MarketDataClient) *FuturesQuoteRequest {
baseReq := newBaseRequest(client...)
baseReq.path = endpoints[1]["futures"]["quotes"]
fqr := &StockQuoteRequest{
baseRequest: baseReq,
symbolParams: ¶meters.SymbolParams{},
}
baseReq.child = fqr
return fqr
}
Define a struct in the models
package to unmarshal the JSON response from the Market Data API. This struct should mirror the JSON structure returned by the new endpoint. Market Data structs are typically JSON arrays.
package models
// FuturesQuoteResponse represents the JSON response structure for futures quotes.
type FuturesQuoteResponse struct {
Symbol []string `json:"symbol"`
Bid []float64 `json:"bid"`
Ask []float64 `json:"ask"`
Time []int64 `json:"time"`
}
Define a struct that represents the unpacked object. This struct is what the Unpack method will return.
// FuturesQuote represents a single futures quote.
type FuturesQuote struct {
Symbol string
Bid float64
Ask float64
Time time.Time
}
Implement an Unpack method for your response struct to convert the packed JSON array format into individual struct objects. This method should be part of the response struct in the models
package.
// Unpack converts packed FuturesQuoteResponse into a slice of individual structs.
func (fpr *FuturesQuoteResponse) Unpack() ([]FuturesQuote, error) {
var quotes []FuturesQuote
for i := range fpr.Symbol {
quote := FuturesQuote{
Symbol: fpr.Symbol[i],
Bid: fpr.Bid[i],
Ask: fpr.Ask[i],
Time: time.Unix(fpr.Time[i], 0),
}
quotes = append(quotes, quote)
}
return quotes, nil
In the client package, create methods for your endpoint that return the packed response (Packed) and the unpacked response (Get). These methods should utilize the base request functionality and your response struct.
By following these steps, you can extend the Market Data Go SDK to support new endpoints, ensuring that the SDK remains a comprehensive tool for accessing financial data.
Document with DocStrings, create tests, and create testable examples for both the Get
and Packed
methods.