package com.friday.executor.common;

import com.friday.base.common.response.DubboResult;
import com.friday.executor.common.rest.FridayHttpResponse;
import com.friday.executor.common.rest.OkHttpService;
import com.friday.operation.api.executor.dto.executor.OperationHttpRequest;
import com.friday.operation.api.executor.dto.executor.OperationHttpResponse;
import com.friday.operation.api.executor.service.IRestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;

import java.util.Objects;

/**
 * @Author YaoYao
 * @Date Created In 13:57 2023/4/4
 * @Description 资讯接口服务
 * @Modified By
 */
@Service
@Slf4j
public class RestHelper implements IRestService {
    @Autowired
    private OkHttpService okHttpService;

    @Override
    public DubboResult<OperationHttpResponse> doRequest(OperationHttpRequest req) {
        String body = null;
        if(Objects.nonNull(req.getRequestBody())){
            body = JSON.toJSONString(req.getRequestBody());
        }
        FridayHttpResponse fridayHttpResponse = okHttpService.doRequest(req.getMethod(), req.getUrl(), req.getHeaders(), body);
        OperationHttpResponse opResult = OperationHttpResponse.builder()
                .success(fridayHttpResponse.success())
                .httpRetCode(fridayHttpResponse.getHttpRetCode())
                .result(fridayHttpResponse.getResult())
                .message(fridayHttpResponse.getMessage())
                .build();
        return DubboResult.newSuccess(opResult);
    }
}
