package ogloszenia.klient;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;

public class Klient13_JsonBinarnie {
	
	public static void main(String[] args) {
		
		Client klient = ClientBuilder.newClient();		
		WebTarget baseUri = klient.target(Ustawienia.URI_SERWISU);
		
		System.out.println("Zaraz wyślę zapytanie...");
		Response response = baseUri
			.path("/ogloszenia")
			.request()
			.accept("application/json")
			.buildGet()
			.invoke();
		
		System.out.println("Odebrałem odpowiedź " + response);
		
		System.out.println("status: " + response.getStatus());
		System.out.println("typ danych: " + response.getMediaType());
		
		byte[] dane = response.readEntity(byte[].class);
		System.out.println("Gotowe. Wczytano " + dane.length + " bajtów.");
		try {
			Files.write(Paths.get("dane13.json"), dane, StandardOpenOption.CREATE);
		} catch (IOException e) {
			System.out.println("IOException podczas kopiowania danych do pliku");
		}
		
		System.out.println("Koniec zabawy");

	}

}
