All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.lecomptoirdespharmacies.medipim.api.MedipimMediaApi Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
package fr.lecomptoirdespharmacies.medipim.api;

import fr.lecomptoirdespharmacies.medipim.api.client.Client;
import fr.lecomptoirdespharmacies.medipim.api.client.Response;
import fr.lecomptoirdespharmacies.medipim.api.entities.media.MedipimMediaType;
import fr.lecomptoirdespharmacies.medipim.api.entities.media.MedipimPhoto;
import fr.lecomptoirdespharmacies.medipim.api.query.PaginatedResponse;
import fr.lecomptoirdespharmacies.medipim.api.query.QueryPage;
import fr.lecomptoirdespharmacies.medipim.api.query.SortingValue;
import fr.lecomptoirdespharmacies.medipim.api.query.media.Query;
import fr.lecomptoirdespharmacies.medipim.api.query.media.QueryFilter;
import fr.lecomptoirdespharmacies.medipim.api.query.media.QuerySorting;
import com.fasterxml.jackson.databind.JsonNode;

import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.ExecutionException;

public class MedipimMediaApi extends MedipimApi {

    public MedipimMediaApi(Client ws, String baseUrl, String username, String password) {
        super(ws, baseUrl, username, password);
    }



    private  PaginatedResponse postMediaQuery(JsonNode query, Class toValueType) {
        try {
            Response response = this.createAuthenticatedRequest("/v4/media/query")
                    .post(query)
                    .toCompletableFuture()
                    .get();

            if (response.getStatus() != 200) {
                throw new RuntimeException(String.format("Medipim API returned status %s - %s", response.getStatus(), response.getBody()));
            }

            JsonNode jsonResponse = response.asJson();
            JsonNode meta = jsonResponse.get("meta");
            JsonNode results = jsonResponse.get("results");

            long totalRecord = meta.get("total").asLong();
            JsonNode page = meta.get("page");
            long pageOffset = page.get("offset").asLong();
            long pageSize = page.get("size").asLong();
            boolean isExhaustive = totalRecord <= pageOffset + pageSize;
            
            List photos = this.deserializeList(results, toValueType);

            return new PaginatedResponse(
                    isExhaustive,
                    photos
            );

        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    public PaginatedResponse getModifiedPhotosSince(OffsetDateTime updatedAtGe) {
        return postMediaQuery(buildGetModifiedPhotosSinceQuery(updatedAtGe), MedipimPhoto.class);
    }

    public JsonNode buildGetModifiedPhotosSinceQuery(OffsetDateTime updatedAtGe) {

        List filters = new ArrayList<>();

        filters.add(
                new QueryFilter.QueryFilterBuilder()
                        .type(MedipimMediaType.photo)
                        .build()
        );
        filters.add(
                new QueryFilter.QueryFilterBuilder()
                        .touchedAt(updatedAtGe.toEpochSecond())
                        .build()
        );
        filters.add(
                new QueryFilter.QueryFilterBuilder()
                        .locale(Locale.FRENCH)
                        .build()
        );
        QueryFilter filter = new QueryFilter.QueryFilterBuilder()
                .and(filters)
                .build();

        QuerySorting sorting = new QuerySorting.QuerySortingBuilder()
                .touchedAt(SortingValue.ASC)
                .build();

        QueryPage page = new QueryPage(0, 250);

        Query query = new Query(
                filter,
                sorting,
                page
        );

        return this.serialize(query);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy