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

pl.allegro.tech.hermes.management.api.OwnersEndpoint Maven / Gradle / Ivy

There is a newer version: 2.10.4
Show newest version
package pl.allegro.tech.hermes.management.api;

import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.HttpMethod;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import pl.allegro.tech.hermes.api.Owner;
import pl.allegro.tech.hermes.management.domain.owner.OwnerSource;
import pl.allegro.tech.hermes.management.domain.owner.OwnerSourceNotFound;
import pl.allegro.tech.hermes.management.domain.owner.OwnerSources;

@Component
@Path("/owners")
@Api(value = "/owners", description = "Provides owners information")
public class OwnersEndpoint {

  private final OwnerSources ownerSources;

  @Autowired
  public OwnersEndpoint(OwnerSources ownerSources) {
    this.ownerSources = ownerSources;
  }

  @GET
  @Path("/sources/{source}")
  @Produces(APPLICATION_JSON)
  @ApiOperation(
      value = "Lists owners from the given source matching the search string",
      response = List.class,
      httpMethod = HttpMethod.GET)
  public List search(
      @PathParam("source") String source, @QueryParam("search") String searchString) {
    return ownerSources.getAutocompletionFor(source).ownersMatching(searchString);
  }

  @GET
  @Path("/sources/{source}/{id}")
  @Produces(APPLICATION_JSON)
  @ApiOperation(
      value = "Returns owner from the given source of the given id",
      response = List.class,
      httpMethod = HttpMethod.GET)
  public Owner get(@PathParam("source") String source, @PathParam("id") String id) {
    return ownerSources
        .getByName(source)
        .map(s -> s.get(id))
        .orElseThrow(() -> new OwnerSourceNotFound(source));
  }

  @GET
  @Path("/sources")
  @Produces(APPLICATION_JSON)
  @ApiOperation(value = "Lists owner sources", response = List.class, httpMethod = HttpMethod.GET)
  public List listSources() {
    return StreamSupport.stream(ownerSources.spliterator(), false)
        .map(SourceDescriptor::of)
        .collect(Collectors.toList());
  }

  private static class SourceDescriptor {

    @JsonProperty("name")
    private final String name;

    @JsonProperty("autocomplete")
    private final boolean autocomplete;

    @JsonProperty("deprecated")
    private final boolean deprecated;

    private SourceDescriptor(OwnerSource source) {
      this.name = source.name();
      this.autocomplete = source.autocompletion().isPresent();
      this.deprecated = source.isDeprecated();
    }

    static SourceDescriptor of(OwnerSource source) {
      return new SourceDescriptor(source);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy