@@ -15,15 +15,15 @@ type ConcurrencyLimit struct {
1515 Models map [string ]int
1616}
1717
18- func (c * ConcurrencyLimit ) UnmarshalYAML (value * yaml.Node ) error {
18+ func (c * ConcurrencyLimit ) UnmarshalYAMLResult (value * yaml.Node ) core. Result {
1919 var n int
2020 if err := value .Decode (& n ); err == nil {
2121 c .Total = n
22- return nil
22+ return core . Ok ( nil )
2323 }
2424 var m map [string ]int
2525 if err := value .Decode (& m ); err != nil {
26- return err
26+ return core . Fail ( err )
2727 }
2828 c .Total = m ["total" ]
2929 c .Models = make (map [string ]int )
@@ -32,7 +32,18 @@ func (c *ConcurrencyLimit) UnmarshalYAML(value *yaml.Node) error {
3232 c .Models [key ] = entry
3333 }
3434 }
35- return nil
35+ return core .Ok (nil )
36+ }
37+
38+ func (c * ConcurrencyLimit ) UnmarshalYAML (value * yaml.Node ) error { // yaml contract
39+ result := c .UnmarshalYAMLResult (value )
40+ if result .OK {
41+ return nil
42+ }
43+ if err , ok := result .Value .(error ); ok {
44+ return err
45+ }
46+ return core .E ("ConcurrencyLimit.UnmarshalYAML" , "decode failed" , nil )
3647}
3748
3849type HTTPStream struct {
@@ -43,11 +54,18 @@ type HTTPStream struct {
4354 Response []byte
4455}
4556
46- func (s * HTTPStream ) Send (data []byte ) error {
47- request , err := http .NewRequestWithContext (context .Background (), s .Method , s .URL , core .NewReader (string (data )))
48- if err != nil {
49- return err
57+ func (s * HTTPStream ) SendResult (data []byte ) core.Result {
58+ if s == nil {
59+ return core .Fail (core .E ("HTTPStream.Send" , "stream is required" , nil ))
60+ }
61+ if s .Client == nil {
62+ return core .Fail (core .E ("HTTPStream.Send" , "client is required" , nil ))
63+ }
64+ requestResult := core .NewHTTPRequestContext (context .Background (), s .Method , s .URL , core .NewReader (string (data )))
65+ if ! requestResult .OK {
66+ return requestResult
5067 }
68+ request := requestResult .Value .(* core.Request )
5169 request .Header .Set ("Content-Type" , "application/json" )
5270 request .Header .Set ("Accept" , "application/json" )
5371 if s .Token != "" {
@@ -56,25 +74,64 @@ func (s *HTTPStream) Send(data []byte) error {
5674
5775 response , err := s .Client .Do (request )
5876 if err != nil {
59- return err
77+ return core . Fail ( err )
6078 }
61- defer func () {
62- _ = response .Body .Close ()
63- }()
6479
6580 readResult := core .ReadAll (response .Body )
6681 if ! readResult .OK {
6782 err , _ := readResult .Value .(error )
68- return core .E ("httpStream.Send" , "failed to read response" , err )
83+ return core .Fail ( core . E ("httpStream.Send" , "failed to read response" , err ) )
6984 }
7085 s .Response = []byte (readResult .Value .(string ))
71- return nil
86+ return core .Ok (nil )
87+ }
88+
89+ func (s * HTTPStream ) Send (data []byte ) error { // core.Stream contract
90+ if s == nil || s .Client == nil {
91+ panic (core .E ("HTTPStream.Send" , "stream client is required" , nil ))
92+ }
93+ result := s .SendResult (data )
94+ if result .OK {
95+ return nil
96+ }
97+ if err , ok := result .Value .(error ); ok {
98+ return err
99+ }
100+ return core .E ("HTTPStream.Send" , "send failed" , nil )
101+ }
102+
103+ func (s * HTTPStream ) ReceiveResult () core.Result {
104+ if s == nil {
105+ return core .Fail (core .E ("HTTPStream.Receive" , "stream is required" , nil ))
106+ }
107+ return core .Ok (s .Response )
108+ }
109+
110+ func (s * HTTPStream ) Receive () ([]byte , error ) { // core.Stream contract
111+ if s == nil {
112+ panic (core .E ("HTTPStream.Receive" , "stream is required" , nil ))
113+ }
114+ result := s .ReceiveResult ()
115+ if result .OK {
116+ return result .Value .([]byte ), nil
117+ }
118+ if err , ok := result .Value .(error ); ok {
119+ return nil , err
120+ }
121+ return nil , core .E ("HTTPStream.Receive" , "receive failed" , nil )
72122}
73123
74- func (s * HTTPStream ) Receive () ([] byte , error ) {
75- return s . Response , nil
124+ func (s * HTTPStream ) CloseResult () core. Result {
125+ return core . Ok ( nil )
76126}
77127
78- func (s * HTTPStream ) Close () error {
79- return nil
128+ func (s * HTTPStream ) Close () error { // core.Stream contract
129+ result := s .CloseResult ()
130+ if result .OK {
131+ return nil
132+ }
133+ if err , ok := result .Value .(error ); ok {
134+ return err
135+ }
136+ return core .E ("HTTPStream.Close" , "close failed" , nil )
80137}
0 commit comments