MCPcopy
hub / github.com/keploy/keploy / GetOutgoing

Method GetOutgoing

pkg/platform/http/agent.go:257–328  ·  view source on GitHub ↗
(ctx context.Context, opts models.OutgoingOptions)

Source from the content-addressed store, hash-verified

255}
256
257func (a *AgentClient) GetOutgoing(ctx context.Context, opts models.OutgoingOptions) (<-chan *models.Mock, error) {
258
259 a.logger.Debug("Connecting to outgoing mocks stream...")
260
261 requestBody := models.OutgoingReq{
262 OutgoingOptions: opts,
263 }
264
265 requestJSON, err := json.Marshal(requestBody)
266 if err != nil {
267 utils.LogError(a.logger, err, "failed to marshal request body for mock outgoing")
268 return nil, fmt.Errorf("error marshaling request body for mock outgoing: %s", err.Error())
269 }
270
271 req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/outgoing", a.conf.Agent.AgentURI), bytes.NewBuffer(requestJSON))
272 if err != nil {
273 utils.LogError(a.logger, err, "failed to create request for mock outgoing")
274 return nil, fmt.Errorf("error creating request for mock outgoing: %s", err.Error())
275 }
276 req.Header.Set("Content-Type", "application/json")
277
278 // Make the HTTP request
279 res, err := a.client.Do(req)
280 if err != nil {
281 return nil, fmt.Errorf("failed to get outgoing response: %s", err.Error())
282 }
283
284 mockChan := make(chan *models.Mock)
285
286 grp, ok := ctx.Value(models.ErrGroupKey).(*errgroup.Group)
287 if !ok {
288 return nil, fmt.Errorf("failed to get errorgroup from the context")
289 }
290
291 grp.Go(func() error {
292 defer func() {
293 close(mockChan)
294
295 err := res.Body.Close()
296 if err != nil {
297 utils.LogError(a.logger, err, "failed to close response body for getoutgoing")
298 }
299 }()
300
301 decoder := gob.NewDecoder(res.Body)
302
303 for {
304 var mock models.Mock
305 if err := decoder.Decode(&mock); err != nil {
306 if utils.IsShutdownError(err) {
307 // End of the stream or connection closed during shutdown
308 break
309 }
310 utils.LogError(a.logger, err, "failed to decode mock from stream")
311 break
312 }
313
314 select {

Callers

nothing calls this directly

Calls 7

LogErrorFunction · 0.92
IsShutdownErrorFunction · 0.92
MarshalMethod · 0.80
DebugMethod · 0.65
CloseMethod · 0.65
ErrorMethod · 0.45
SetMethod · 0.45

Tested by

no test coverage detected