From 6b686db66c614ef84c54e910d24575a4c6ddb2b4 Mon Sep 17 00:00:00 2001 From: Zakhar Petukhov Date: Wed, 11 Dec 2024 12:30:07 +0800 Subject: [PATCH] create a func to parse config from a custom url --- liteapi/client.go | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/liteapi/client.go b/liteapi/client.go index 292e967..736162e 100644 --- a/liteapi/client.go +++ b/liteapi/client.go @@ -222,24 +222,21 @@ func FromEnvs() Option { // Mainnet configures a client to use lite servers from the mainnet. func Mainnet() Option { return func(o *Options) error { - file, err := downloadConfig("https://ton.org/global.config.json") - if err != nil { - return err - } - o.LiteServers = file.LiteServers - return nil + return setLiteServersFromURL("https://ton.org/global.config.json", o) } } // Testnet configures a client to use lite servers from the testnet. func Testnet() Option { return func(o *Options) error { - file, err := downloadConfig("https://ton.org/testnet-global.config.json") - if err != nil { - return err - } - o.LiteServers = file.LiteServers - return nil + return setLiteServersFromURL("https://ton.org/testnet-global.config.json", o) + } +} + +// WithConfigurationUrl configures a client using a custom configuration URL. +func WithConfigurationUrl(url string) Option { + return func(o *Options) error { + return setLiteServersFromURL(url, o) } } @@ -1066,6 +1063,15 @@ func downloadConfig(path string) (*config.GlobalConfigurationFile, error) { return o, nil } +func setLiteServersFromURL(url string, o *Options) error { + file, err := downloadConfig(url) + if err != nil { + return err + } + o.LiteServers = file.LiteServers + return nil +} + func (c *Client) getNetworkGlobalID() *int32 { c.mu.RLock() defer c.mu.RUnlock()