convertHTTPResponse converts an HTTP response to a stdio response
(requestID ID, recorder *httptest.ResponseRecorder)
| 505 | } |
| 506 | // Set admin token header (for admin authentication) |
| 507 | if adminToken, ok := req.Params["adminToken"].(string); ok { |
| 508 | httpReq.Header.Set("Authorization", adminToken) |
| 509 | } |
| 510 | |
| 511 | recorder := httptest.NewRecorder() |
| 512 | ss.server.router.ServeHTTP(recorder, httpReq) |
| 513 | ss.convertHTTPResponse(req.ID, recorder) |
| 514 | } |
| 515 | |
| 516 | // convertHTTPResponse converts an HTTP response to a stdio response |
| 517 | func (ss *stdioServer) convertHTTPResponse(requestID ID, recorder *httptest.ResponseRecorder) { |
| 518 | statusCode := recorder.Code |
| 519 | responseBody := recorder.Body.Bytes() |
| 520 | |
| 521 | var responseData interface{} |
| 522 | if len(responseBody) > 0 { |
| 523 | if err := json.Unmarshal(responseBody, &responseData); err != nil { |
| 524 | // If it's not JSON, just use the raw string |
| 525 | responseData = string(responseBody) |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | success := statusCode >= 200 && statusCode < 300 |
| 530 | |
| 531 | if respMap, ok := responseData.(map[string]interface{}); ok { |
| 532 | // If it's already in wuzapi format, extract the data/error |
| 533 | if data, hasData := respMap["data"]; hasData { |
| 534 | ss.sendSuccess(requestID, statusCode, data) |
| 535 | return |
| 536 | } |
| 537 | if errMsg, hasError := respMap["error"]; hasError { |
| 538 | if errStr, ok := errMsg.(string); ok { |
| 539 | ss.sendError(requestID, statusCode, errStr) |
| 540 | return |
| 541 | } |
| 542 | } |
| 543 | ss.sendSuccess(requestID, statusCode, respMap) |
| 544 | return |
| 545 | } |
| 546 | |
| 547 | // For non-JSON or simple responses |
| 548 | if success { |
| 549 | ss.sendSuccess(requestID, statusCode, responseData) |
| 550 | } else { |
no test coverage detected