NewClient return new interop client.
( ctx context.Context, conf config.InteropClient, c ClientComponent, selector ComponentSelector, )
| 360 | |
| 361 | // NewClient return new interop client. |
| 362 | func NewClient( |
| 363 | ctx context.Context, conf config.InteropClient, c ClientComponent, selector ComponentSelector, |
| 364 | ) (*Client, error) { |
| 365 | fetcher, err := conf.Fetcher(ctx, c) |
| 366 | if err != nil { |
| 367 | return nil, err |
| 368 | } |
| 369 | if fetcher == nil { |
| 370 | return nil, errUnknownConfig.New() |
| 371 | } |
| 372 | confFileBytes, err := fetcher.File(InteropClientConfigurationName) |
| 373 | if err != nil { |
| 374 | return nil, err |
| 375 | } |
| 376 | |
| 377 | var yamlConf struct { |
| 378 | JoinServers []struct { |
| 379 | File string `yaml:"file"` |
| 380 | Components []ComponentSelector `yaml:"components"` |
| 381 | JoinEUIs []types.EUI64Prefix `yaml:"join-euis"` |
| 382 | } `yaml:"join-servers"` |
| 383 | } |
| 384 | if err := yaml.UnmarshalStrict(confFileBytes, &yamlConf); err != nil { |
| 385 | return nil, err |
| 386 | } |
| 387 | |
| 388 | type ComponentConfig struct { |
| 389 | DNSSuffix string `yaml:"dns"` |
| 390 | Scheme string `yaml:"scheme"` |
| 391 | FQDN string `yaml:"fqdn"` |
| 392 | Port uint32 `yaml:"port"` |
| 393 | Headers map[string]string `yaml:"headers"` |
| 394 | BasicAuth struct { |
| 395 | Username string `yaml:"username"` |
| 396 | Password string `yaml:"password"` |
| 397 | } `yaml:"basic-auth"` |
| 398 | TLS tlsConfig `yaml:"tls"` |
| 399 | } |
| 400 | |
| 401 | jss := make([]prefixJoinServerClient, 0, len(yamlConf.JoinServers)) |
| 402 | for _, jsEntry := range yamlConf.JoinServers { |
| 403 | // Skip Join Servers with unmatching component selector. |
| 404 | if len(jsEntry.Components) > 0 { |
| 405 | var found bool |
| 406 | if slices.Contains(jsEntry.Components, selector) { |
| 407 | found = true |
| 408 | } |
| 409 | if !found { |
| 410 | continue |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | fileParts := strings.Split(filepath.ToSlash(jsEntry.File), "/") |
| 415 | fetcher := fetch.WithBasePath(fetcher, fileParts[:len(fileParts)-1]...) |
| 416 | jsFileBytes, err := fetcher.File(fileParts[len(fileParts)-1]) |
| 417 | if err != nil { |
| 418 | return nil, err |
| 419 | } |