package com.izouma.awesomeadmin.web;

import com.izouma.awesomeadmin.dto.Result;
import com.izouma.awesomeadmin.model.DepartInfo;
import com.izouma.awesomeadmin.util.Base64;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.URLEncoder;

@Controller
@RequestMapping("/system")
@RequiresAuthentication
public class SystemController {

    @RequestMapping(value = "/deploy", method = RequestMethod.POST)
    @ResponseBody
    public Result deploy(DepartInfo record) {
        try {
            HttpClient httpClient = HttpClientBuilder.create().build();
            HttpPost post = new HttpPost("http://49.4.67.181:8000/job/信牧基础平台/build");
            post.addHeader("Authorization", "Basic " + Base64.getBase64("admin:3edc#EDC"));
            HttpResponse response = httpClient.execute(post);
            if (201 == response.getStatusLine().getStatusCode()) {
                String location = response.getFirstHeader("Location").getValue();
                return new Result(true, new StringBuilder(location));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, null);
    }

}
