package com.jpsoft.shinestar.modules.sys.controller;

import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.Charset;

@RestController
@RequestMapping("/sys/api")
public class ApiController {
    @ApiOperation("获取后端api列表")
    @GetMapping("selectAll")
    public ResponseEntity<String> selectAll(HttpServletRequest request){
        RestTemplate restTemplate = new RestTemplate();

        for (HttpMessageConverter<?> httpMessageConverter : restTemplate.getMessageConverters()) {
            if(httpMessageConverter instanceof StringHttpMessageConverter) {
                ((StringHttpMessageConverter) httpMessageConverter).setDefaultCharset(Charset.forName("UTF-8"));
                break;
            }
        }

        String path = request.getContextPath();
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

        ResponseEntity<String> entity = restTemplate.getForEntity(basePath + "v2/api-docs",String.class);

        return entity;
    }
}
