-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring http ammo
- Loading branch information
Showing
21 changed files
with
413 additions
and
475 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,110 @@ | ||
package base | ||
|
||
import "github.com/yandex/pandora/core/aggregator/netsample" | ||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
urlpkg "net/url" | ||
|
||
type Ammo[R any] struct { | ||
Req *R | ||
tag string | ||
id uint64 | ||
isInvalid bool | ||
"github.com/yandex/pandora/components/providers/http/util" | ||
"github.com/yandex/pandora/core/aggregator/netsample" | ||
"github.com/yandex/pandora/lib/netutil" | ||
) | ||
|
||
func NewAmmo(method string, url string, body []byte, header http.Header, tag string) (*Ammo, error) { | ||
if ok := netutil.ValidHTTPMethod(method); !ok { | ||
return nil, errors.New("invalid HTTP method " + method) | ||
} | ||
if _, err := urlpkg.Parse(url); err != nil { | ||
return nil, fmt.Errorf("invalid URL %s; err %w ", url, err) | ||
} | ||
return &Ammo{ | ||
method: method, | ||
body: body, | ||
url: url, | ||
tag: tag, | ||
header: header, | ||
constructor: true, | ||
}, nil | ||
} | ||
|
||
type Ammo struct { | ||
Req *http.Request | ||
method string | ||
body []byte | ||
url string | ||
tag string | ||
header http.Header | ||
id uint64 | ||
isInvalid bool | ||
constructor bool | ||
} | ||
|
||
func (a *Ammo[R]) Request() (*R, *netsample.Sample) { | ||
func (a *Ammo) Request() (*http.Request, *netsample.Sample) { | ||
if a.Req == nil { | ||
_ = a.BuildRequest() // TODO: what if error. There isn't a logger | ||
} | ||
sample := netsample.Acquire(a.Tag()) | ||
sample.SetID(a.ID()) | ||
return a.Req, sample | ||
} | ||
|
||
func (a *Ammo[R]) Reset(req *R, tag string) { | ||
a.Req = req | ||
a.tag = tag | ||
a.id = 0 | ||
a.isInvalid = false | ||
} | ||
|
||
func (a *Ammo[_]) SetID(id uint64) { | ||
func (a *Ammo) SetID(id uint64) { | ||
a.id = id | ||
} | ||
|
||
func (a *Ammo[_]) ID() uint64 { | ||
func (a *Ammo) ID() uint64 { | ||
return a.id | ||
} | ||
|
||
func (a *Ammo[_]) Invalidate() { | ||
func (a *Ammo) Invalidate() { | ||
a.isInvalid = true | ||
} | ||
|
||
func (a *Ammo[_]) IsInvalid() bool { | ||
func (a *Ammo) IsInvalid() bool { | ||
return a.isInvalid | ||
} | ||
|
||
func (a *Ammo[_]) IsValid() bool { | ||
func (a *Ammo) IsValid() bool { | ||
return !a.isInvalid | ||
} | ||
|
||
func (a *Ammo[_]) Tag() string { | ||
func (a *Ammo) SetTag(tag string) { | ||
a.tag = tag | ||
} | ||
|
||
func (a *Ammo) Tag() string { | ||
return a.tag | ||
} | ||
|
||
func (a *Ammo) FromConstructor() bool { | ||
return a.constructor | ||
} | ||
|
||
// use NewAmmo() for skipping error here | ||
func (a *Ammo) BuildRequest() error { | ||
var buff io.Reader | ||
if a.body != nil { | ||
buff = bytes.NewReader(a.body) | ||
} | ||
req, err := http.NewRequest(a.method, a.url, buff) | ||
if err != nil { | ||
return fmt.Errorf("cant create request: %w", err) | ||
} | ||
a.Req = req | ||
util.EnrichRequestWithHeaders(req, a.header) | ||
return nil | ||
} | ||
|
||
func (a *Ammo) Reset() { | ||
a.Req = nil | ||
a.method = "" | ||
a.body = nil | ||
a.url = "" | ||
a.tag = "" | ||
a.header = nil | ||
a.id = 0 | ||
a.isInvalid = false | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.