@@ -2,10 +2,8 @@ package impl
22
33import (
44 "context"
5- "io"
65 "net"
76 "net/http"
8- "strings"
97
108 "github.com/deadblue/elevengo/internal/util"
119 "github.com/deadblue/elevengo/lowlevel/client"
@@ -47,19 +45,25 @@ func (c *ClientImpl) send(req *http.Request) (resp *http.Response, err error) {
4745 return
4846}
4947
50- // |post| performs an HTTP POST request to specific URL with given payload.
51- func (c * ClientImpl ) post (url string , payload client.Payload , context context.Context ) (body io.ReadCloser , err error ) {
48+ func (c * ClientImpl ) Post (
49+ url string , payload client.Payload , headers map [string ]string , context context.Context ,
50+ ) (body client.Body , err error ) {
5251 req , err := http .NewRequestWithContext (context , http .MethodPost , url , payload )
5352 if err != nil {
5453 return
5554 }
55+ if len (headers ) > 0 {
56+ for name , value := range headers {
57+ req .Header .Add (name , value )
58+ }
59+ }
5660 req .Header .Set (headerContentType , payload .ContentType ())
5761 if size := payload .ContentLength (); size > 0 {
5862 req .ContentLength = size
5963 }
6064 var resp * http.Response
6165 if resp , err = c .send (req ); err == nil {
62- body = resp . Body
66+ body = makeClientBody ( resp )
6367 }
6468 return
6569}
@@ -76,22 +80,19 @@ func (c *ClientImpl) Get(
7680 req .Header .Add (name , value )
7781 }
7882 }
79- resp , err := c .send (req )
80- if err == nil {
81- bi := & _BodyImpl {
82- rc : resp .Body ,
83- size : - 1 ,
84- total : - 1 ,
85- }
86- if hv := resp .Header .Get ("Content-Length" ); hv != "" {
87- bi .size = util .ParseInt64 (hv , - 1 )
88- }
89- if hv := resp .Header .Get ("Content-Range" ); hv != "" {
90- if index := strings .LastIndex (hv , "/" ); index >= 0 {
91- bi .total = util .ParseInt64 (hv [index + 1 :], - 1 )
92- }
93- }
94- body = bi
83+ if resp , err := c .send (req ); err == nil {
84+ body = makeClientBody (resp )
9585 }
9686 return
9787}
88+
89+ func makeClientBody (resp * http.Response ) client.Body {
90+ body := & _BodyImpl {
91+ rc : resp .Body ,
92+ size : - 1 ,
93+ }
94+ if hv := resp .Header .Get ("Content-Length" ); hv != "" {
95+ body .size = util .ParseInt64 (hv , - 1 )
96+ }
97+ return body
98+ }
0 commit comments