package com.ningdatech.carapi.driver.call;

import com.ningdatech.carapi.driver.entity.OverseeInterfaceCallDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.xml.transform.Result;

/**
 * @author CMM
 * @description
 * @since 2022/12/08 14:27
 */
@Component
public class RestTemplateToInterface {
    @Autowired
    private RestTemplate restTemplate;

    /**
     * 以get方式请求第三方http接口 getForEntity
     *
     * @param url
     * @return
     */
    public Result doGetWith1(String url) {
        ResponseEntity<Result> responseEntity = restTemplate.getForEntity(url, Result.class);
        Result result = responseEntity.getBody();
        return result;
    }

    /**
     * 以get方式请求第三方http接口 getForObject 返回值返回的是响应体，省去了我们再去getBody()
     *
     * @param url
     * @return
     */
    public Result doGetWith2(String url) {
        Result result = restTemplate.getForObject(url, Result.class);
        return result;
    }


    /**
     * 以post方式请求第三方http接口 postForEntity
     * @param url
     * @param req
     * @return
     */
    public String doPostWith1(String url, OverseeInterfaceCallDTO req) {
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, req, String.class);
        String body = responseEntity.getBody();
        return body;
    }

    /**
     * 以post方式请求第三方http接口 postForEntity 返回值返回的是响应体，省去了我们再去getBody()
     *
     * @param url
     * @param req
     * @return
     */
    public String doPostWith2(String url, OverseeInterfaceCallDTO req) {
        String body = restTemplate.postForObject(url, req, String.class);
        return body;
    }
}
