github.com/MetalBlockchain/metalgo@v1.11.9/utils/rpc/options.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package rpc 5 6 import ( 7 "net/http" 8 "net/url" 9 ) 10 11 type Option func(*Options) 12 13 type Options struct { 14 headers http.Header 15 queryParams url.Values 16 } 17 18 func NewOptions(ops []Option) *Options { 19 o := &Options{ 20 headers: http.Header{}, 21 queryParams: url.Values{}, 22 } 23 o.applyOptions(ops) 24 return o 25 } 26 27 func (o *Options) applyOptions(ops []Option) { 28 for _, op := range ops { 29 op(o) 30 } 31 } 32 33 func (o *Options) Headers() http.Header { 34 return o.headers 35 } 36 37 func (o *Options) QueryParams() url.Values { 38 return o.queryParams 39 } 40 41 func WithHeader(key, val string) Option { 42 return func(o *Options) { 43 o.headers.Set(key, val) 44 } 45 } 46 47 func WithQueryParam(key, val string) Option { 48 return func(o *Options) { 49 o.queryParams.Set(key, val) 50 } 51 }