package org.wso2.carbon.apimgt.gateway;
import ballerina.net.http;
import ballerina.lang.messages;
import ballerina.lang.errors;
import ballerina.lang.system;

@http:config {basePath:"/sample-api"}
service<http> SampleAPI_efa1fcd1_d436_4101_b4db_45375dae5e4c {
	string KEY_TYPE = "KEY_TYPE";
			    @http:POST{}
        	@http:Path{value:"/"}
    resource post (message m) {
												        message response;
		string endpointType = messages:getProperty(m,KEY_TYPE);

	try{
		if (endpointType == "PRODUCTION") {
				response = execute_endpoint("${productionEndpoint.name}","post", "", m);
				} else {
				response = execute_endpoint("${sandboxEndpoint.name}","post", "", m);
				}
	} catch (errors:Error e) {
			system:println(e.msg);
			//fault:mediate(m, e);
			response = {};
		    messages:setStringPayload(response, "Internal error occurred");
			http:setStatusCode (response, 500);
		    reply response;
	}

		reply response;
}
	    @http:GET{}
        	@http:Path{value:"/"}
    resource get (message m) {
												        message response;
		string endpointType = messages:getProperty(m,KEY_TYPE);

	try{
		if (endpointType == "PRODUCTION") {
				response = execute_endpoint("${productionEndpoint.name}","get", "", m);
				} else {
				response = execute_endpoint("${sandboxEndpoint.name}","get", "", m);
				}
	} catch (errors:Error e) {
			system:println(e.msg);
			//fault:mediate(m, e);
			response = {};
		    messages:setStringPayload(response, "Internal error occurred");
			http:setStatusCode (response, 500);
		    reply response;
	}

		reply response;
}
		}