File size: 1,774 Bytes
542878f
 
 
 
 
 
 
 
a1eed6b
 
542878f
 
 
a1eed6b
542878f
a1eed6b
542878f
 
 
 
 
 
 
 
a1eed6b
542878f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package national_currency

import (
	"fmt"
	"bytes"
	"context"
	"net/http"
	"encoding/json"
	proxy "tebakaja_lb_proxy/proxy"
) 


/*
 * --- National Currency Prediction Service ---
 */
func (s *NationalCurrencyServiceImpl) NationalCurrencyPredictionService(ctx context.Context, req PredictionRequest) (ApiResponse, error) {
	reqBody, err := json.Marshal(req)
	if err != nil {
		return ApiResponse{
			Message:    fmt.Sprintf("Failed to marshal request body: %v", err),
			StatusCode: http.StatusInternalServerError,
		}, err
	}

	endpoint := fmt.Sprintf("%s/prediction", proxy.GetEndpointByRestService("national"))
	httpReq, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewBuffer(reqBody))
	if err != nil {
		return ApiResponse{
			Message:    fmt.Sprintf("Failed to make request: %v", err),
			StatusCode: http.StatusInternalServerError,
		}, err
	}
	httpReq.Header.Set("Content-Type", "application/json")

	resp, err := http.DefaultClient.Do(httpReq)
	if err != nil {
		return ApiResponse{
			Message:    fmt.Sprintf("Failed to make request: %v", err),
			StatusCode: http.StatusInternalServerError,
		}, err
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return ApiResponse{
			Message:    fmt.Sprintf("Request failed: %d", resp.StatusCode),
			StatusCode: resp.StatusCode,
		}, fmt.Errorf("request failed: %d", resp.StatusCode)
	}

	var apiResponse PredictionResponse
	if err := json.NewDecoder(resp.Body).Decode(&apiResponse); err != nil {
		return ApiResponse{
			Message:    fmt.Sprintf("Failed to parse JSON response: %v", err),
			StatusCode: http.StatusInternalServerError,
		}, err
	}

	return ApiResponse{
		Message:    apiResponse.Message,
		StatusCode: apiResponse.StatusCode,
		Data:       apiResponse.Data,
	}, nil
}