
package org.wso2.mdm.integration.common;

import org.wso2.carbon.automation.test.utils.http.client.HttpRequestUtil;
import org.wso2.carbon.automation.test.utils.http.client.HttpResponse;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class RestClient {
    private String backEndUrl;
    private Map<String, String> requestHeaders = new HashMap<String, String>();
    public static final String CONTENT_TYPE = "Content-Type";

    public RestClient(String backEndUrl) {
        this.backEndUrl = backEndUrl;
        if (requestHeaders.get(CONTENT_TYPE) == null) {
            this.requestHeaders.put(CONTENT_TYPE, "application/json");
        }
    }

    public void setHttpHeader(String headerName, String value) {
        this.requestHeaders.put(headerName, value);
    }

    public String getHttpHeader(String headerName) {
        return this.requestHeaders.get(headerName);
    }

    public void removeHttpHeader(String headerName) {
        this.requestHeaders.remove(headerName);
    }

    public HttpResponse post(String endpoint, String body) throws Exception {
        return HttpRequestUtil.doPost(new URL(backEndUrl + endpoint), body, requestHeaders);
    }

    public HttpResponse get(String endpoint) throws Exception {
        return HttpRequestUtil.doGet(backEndUrl + endpoint, requestHeaders);
    }

}
