Skip to content

Gerenciando Locations

João Victor Oliveira edited this page Jan 19, 2025 · 1 revision

Gerenciando Locations

O gerenciando de locations é simples, bastando você utilizando este código inicial:

public class Main {
    public static void main(String[] args) {
        PteroAPI.initPteroAPI("https://example.com.br", "TOKEN", ManagerPolicy.ALL);
        LocationManager locationManager = PteroAPI.getManager(LocationManager.class);
    }
}

Listando Locations

Para listar todos os locations, você precisará chamar a função "listLocations", que retorna um CompletableFuture, do tipo lista de Locations.

public class Main {
    public static void main(String[] args) {
        PteroAPI.initPteroAPI("https://example.com.br", "TOKEN", ManagerPolicy.ALL);
        LocationManager locationManager = PteroAPI.getManager(LocationManager.class);
        CompletableFuture<List<Location>> completableFuture = locationManager.listLocations();
        completableFuture.thenAccept(locationList -> locationList.forEach(location -> System.out.println("The location: " + location.getLocationName() + " is present"))).exceptionally(throwable -> {
            throw new RuntimeException(throwable);
        });
    }
}

Pegando uma Location

Para pegar as informações de uma location, você precisará chamar a função "getLocation", que retorna um CompletableFuture, do tipo Location.

public class Main {
    public static void main(String[] args) {
        PteroAPI.initPteroAPI("https://example.com.br", "TOKEN", ManagerPolicy.ALL);
        LocationManager locationManager = PteroAPI.getManager(LocationManager.class);
        CompletableFuture<Location> completableFuture = locationManager.getLocation(1L);
        completableFuture.thenAccept(location -> System.out.println("The location: " + location.getLocationName() + " is present")).exceptionally(throwable -> {
            throw new RuntimeException(throwable);
        });
    }
}

Atualizando uma Location

Para atualizar as informações de uma location, você precisará chamar a função "updateLocation", passando um object location atualizado, que retorna um CompletableFuture, do tipo Json, que é a resposta da request.

public class Main {
    public static void main(String[] args) {
        PteroAPI.initPteroAPI("https://example.com.br", "TOKEN", ManagerPolicy.ALL);
        LocationManager locationManager = PteroAPI.getManager(LocationManager.class);
        CompletableFuture<Location> completableFuture = locationManager.getLocation(1L);
        completableFuture.thenAccept(location -> {
            location.setLocationName("New Location Name");
            CompletableFuture<JSONObject> completableFutureResponse = locationManager.updateLocation(location);
            completableFutureResponse.thenAccept(System.out::println).exceptionally(throwable -> {
                throw new RuntimeException(throwable);
            });
        }).exceptionally(throwable -> {
            throw new RuntimeException(throwable);
        });
    }
}

Criando uma Location

Para criar uma location, você precisará chamar a função "createLocation", passando o builder de location, que retorna um CompletableFuture, do tipo Json, que é a resposta da request.

⚠️ATENÇÃO

Ao criar um builder, lembre-se de setar o valor em todos os parametros, pois, caso deixe algum nulo, poderá resultar em execessões.

public class Main {
    public static void main(String[] args) {
        PteroAPI.initPteroAPI("https://example.com.br", "TOKEN", ManagerPolicy.ALL);
        LocationManager locationManager = PteroAPI.getManager(LocationManager.class);
        LocationBuilder builder = new LocationBuilder().appendLocationName("GB")
                .appendLocationDescription("London Datacenter");
        CompletableFuture<JSONObject> completableFuture = locationManager.createLocation(builder);
        completableFuture.thenAccept(System.out::println).exceptionally(throwable -> {
            throw new RuntimeException(throwable);
        });
    }
}

Deletando uma Location

Para deletar uma Location, você precisará chamar a função "deleteLocation", passando o ID do location, que retorna um CompletableFuture, do tipo Json, que é a resposta da request.

public class Main {
    public static void main(String[] args) {
        PteroAPI.initPteroAPI("https://example.com.br", "TOKEN", ManagerPolicy.ALL);
        LocationManager locationManager = PteroAPI.getManager(LocationManager.class);
        CompletableFuture<JSONObject> completableFuture = locationManager.deleteLocation(1L);
        completableFuture.thenAccept(System.out::println).exceptionally(throwable -> {
            throw new RuntimeException(throwable);
        });
    }
}