// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package client

import java.lang.reflect.*

import org.apache.http.*
import org.apache.http.client.*
import org.apache.http.client.methods.*
import org.apache.http.entity.*
import org.apache.http.impl.client.*

import com.github.arteam.simplejsonrpc.client.*

import common.*

class MyTransport2 implements Transport {
	String urlstr
	@Override
	String pass(String body) {
        def client = HttpClientBuilder.create().build()
        def request = new HttpPost(urlstr) 
        request.setEntity(new StringEntity(body, ContentType.APPLICATION_JSON))
        def response = client.execute(request)
        def sb = new StringBuilder()
        def br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()))
		def line
        while((line = br.readLine()) != null) {
            sb.append(line)
        }
        br.close()
        return sb.toString()
	}
}

class MyTransport implements Transport {
	String urlstr
	@Override
	String pass(String body) {
		def con = (HttpURLConnection)(new URL(urlstr)).openConnection()
        con.setRequestMethod("POST")
        con.addRequestProperty("accept",  "application/json-rpc")
        con.addRequestProperty("content-type", "application/json-rpc")
        con.setDoOutput(true)
        con.getOutputStream().write(body.getBytes())
        def sb = new StringBuilder()
        con.connect()
        if(con.getResponseCode() / 100 == 2) {
            def is = con.getInputStream()
            def b = new byte[1000]
			def n
            while((n = is.read(b)) >= 0) {
                sb.append(new String(b, 0, n))
            }
            is.close()
        } else {
            printf("Error: %d %s\n", con.rResponseCode, con.responseMessage)
        }
        con.disconnect()
        return sb.toString()
	}
}

def test(String urlstr) {
	println(urlstr)
	client = new JsonRpcClient(new MyTransport(urlstr: urlstr))
	v = client.createRequest().method("getInt").id(1).returnAs(int.class).execute()
	println(v)
	s = client.createRequest().method("getString").id(2).returnAs(String.class).execute()
	println(s)
	d = client.createRequest().method("getData").id(3).returnAs(Data.class).execute()
	println(d)
	lstv = client.createRequest().method("getListOfInts").id(4).returnAs(List.class).execute()
	println(lstv)
	lsts = client.createRequest().method("getListOfStrings").id(5).returnAs(List.class).execute()
	println(lsts)
	lstd = client.createRequest().method("getListOfData").id(6).returnAs(List.class).execute()
	println(lstd)
	v1 = 123
	v2 = 456
	v3 = client.createRequest().method("add").params(v1, v2).id(7).returnAs(int.class).execute()
	println(v3)
	s1 = "ABC"
	s2 = "DEF"
	s3 = client.createRequest().method("concat").params(s1, s2).id(8).returnAs(String.class).execute()
	println(s3)
	d1 = new Data(ival: 123, sval: "ABC")
	d2 = client.createRequest().method("modify").params(d1).id(9).returnAs(Data.class).execute()
	println(d2)
}

test("http://localhost:8001/test")
test("http://localhost:8002/test")
test("http://localhost:8003/test")
test("http://localhost:8004/test")
test("http://localhost:8005/test")
test("http://localhost:8006/server.php")
test("http://localhost:8007/server.php")
