Skip to content

Commit

Permalink
Merge pull request #1 from palantir/fo/expand-api
Browse files Browse the repository at this point in the history
Expand API
  • Loading branch information
qinfchen authored Jul 20, 2018
2 parents 3d54103 + 7fb8ae5 commit 0803557
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 50 deletions.
6 changes: 1 addition & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 2
jobs:
build:
docker:
- image: circleci/openjdk:10.0.1-jdk-node-browsers
- image: circleci/openjdk:8u171-jdk-node-browsers
environment:
GRADLE_OPTS: -Dorg.gradle.console=plain -Dorg.gradle.internal.launcher.welcomeMessageEnabled=false
steps:
Expand Down Expand Up @@ -37,10 +37,6 @@ jobs:
command: |
if [[ "${CIRCLE_BRANCH}" == "develop" || "${CIRCLE_TAG}" =~ [0-9]+(\.[0-9]+)+(-[a-zA-Z]+[0-9]*)* ]]; then
./gradlew --stacktrace --continue publish
else
./gradlew --parallel --continue publishToMavenLocal
mkdir -p ~/poms
find . -name 'pom-default.xml' -exec cp --parents {} ~/poms \;
fi
- store_artifacts:
Expand Down
15 changes: 15 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,18 @@ allprojects {
propertiesFile file: project.rootProject.file('versions.props')
}
}

configure(subprojects - project(':example-api')) {
apply plugin: 'java'
apply plugin: 'com.palantir.baseline-checkstyle'
apply plugin: 'com.palantir.baseline-eclipse'
apply plugin: 'com.palantir.baseline-error-prone'
apply plugin: 'com.palantir.baseline-idea'
apply plugin: 'org.inferred.processors'

sourceCompatibility = 1.8

tasks.withType(JavaCompile) {
options.compilerArgs += ['-XepDisableWarningsInGeneratedCode', '-Werror']
}
}
31 changes: 23 additions & 8 deletions example-api/src/main/conjure/example-api.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
types:
definitions:
default-package: com.palantir.conjure.examples
default-package: com.palantir.conjure.examples.recipes.api
objects:
Temperature:
fields:
degree: double
unit: string

Ingredient:
alias: string

RecipeName:
alias: string

BakeStep:
fields:
temperature: Temperature
durationInSeconds: integer

RecipeStep:
union:
mix: set<Ingredient>
chop: Ingredient
bake: BakeStep

Recipe:
fields:
name: RecipeName
Expand All @@ -34,25 +39,35 @@ types:
services:
RecipeBookService:
name: Recipe Book
package: com.palantir.comnjure.examples.api
package: com.palantir.conjure.examples.recipes.api
base-path: /
docs: |
APIs for retrieving recipes
endpoints:
createRecipe:
http: POST /recipes
args:
createRecipeRequest:
param-type: body
type: Recipe

getRecipe:
http: GET /recipes/{name}
args:
name:
type: RecipeName
name: RecipeName
returns: Recipe
docs: |
Retrieves a recipe for the given name.
@param name
The name of the recipe
getRecipes:
getAllRecipes:
http: GET /recipes
returns: list<Recipe>
docs: |
Returns a list of avaiable recipes in the book.
returns: set<Recipe>

deleteRecipe:
http: DELETE /recipes/{name}
args:
name: RecipeName
14 changes: 1 addition & 13 deletions example-server/build.gradle
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'com.palantir.baseline-checkstyle'
apply plugin: 'com.palantir.baseline-eclipse'
apply plugin: 'com.palantir.baseline-error-prone'
apply plugin: 'com.palantir.baseline-idea'
apply plugin: 'com.palantir.launch-config'
apply plugin: 'org.inferred.processors'

tasks.withType(JavaCompile) {
options.compilerArgs += ['-XepDisableWarningsInGeneratedCode', '-Werror']
}

sourceCompatibility = '1.8'

dependencies {
processor 'org.immutables:value'
Expand All @@ -33,6 +20,7 @@ dependencies {
}

mainClassName = 'com.palantir.conjure.examples.RecipeBookApplication'

run {
args 'server', 'var/conf/recipes.yml'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,25 @@
package com.palantir.conjure.examples;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.palantir.conjure.examples.recipes.api.Recipe;
import io.dropwizard.Configuration;
import java.util.List;
import java.util.Set;
import org.hibernate.validator.constraints.NotEmpty;

public final class RecipeBookConfiguration extends Configuration {

@NotEmpty
private List<Recipe> recipes;
private Set<Recipe> recipes;

@JsonProperty
public List<Recipe> getRecipes() {
public Set<Recipe> getRecipes() {
return recipes;
}

@JsonProperty
public void setRecipes(List<Recipe> recipes) {
this.recipes = ImmutableList.copyOf(recipes);
this.recipes = ImmutableSet.copyOf(recipes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,47 @@
package com.palantir.conjure.examples.resources;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.palantir.comnjure.examples.api.RecipeBookService;
import com.palantir.conjure.examples.Recipe;
import com.palantir.conjure.examples.RecipeErrors;
import com.palantir.conjure.examples.RecipeName;
import java.util.List;
import java.util.Optional;
import com.palantir.conjure.examples.recipes.api.Recipe;
import com.palantir.conjure.examples.recipes.api.RecipeBookService;
import com.palantir.conjure.examples.recipes.api.RecipeErrors;
import com.palantir.conjure.examples.recipes.api.RecipeName;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

public final class RecipeBookResource implements RecipeBookService {

private final List<Recipe> recipes;
private final Map<RecipeName, Recipe> recipes;

public RecipeBookResource(List<Recipe> recipes) {
this.recipes = ImmutableList.copyOf(recipes);
public RecipeBookResource(Set<Recipe> recipes) {
this.recipes = recipes.stream()
.collect(Collectors.toConcurrentMap(Recipe::getName, Function.identity()));
}

@Override
public Recipe getRecipe(RecipeName name) {
Preconditions.checkNotNull(name, "Recipe name must be provided.");
Optional<Recipe> maybeRecipe = this.recipes.stream().filter(r -> r.getName().equals(name)).findAny();
if (!maybeRecipe.isPresent()) {
Recipe maybeRecipe = this.recipes.get(name);
if (maybeRecipe == null) {
throw RecipeErrors.recipeNotFound(name);
}
return maybeRecipe;
}

return maybeRecipe.get();
@Override
public void createRecipe(Recipe createRecipeRequest) {
recipes.put(createRecipeRequest.getName(), createRecipeRequest);
}

@Override
public Set<Recipe> getAllRecipes() {
return new HashSet<>(recipes.values());
}

@Override
public List<Recipe> getRecipes() {
return recipes;
public void deleteRecipe(RecipeName name) {
recipes.remove(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@
import static org.junit.Assert.assertEquals;

import com.google.common.io.Resources;
import com.palantir.comnjure.examples.api.RecipeBookService;
import com.palantir.conjure.examples.recipes.api.Recipe;
import com.palantir.conjure.examples.recipes.api.RecipeBookService;
import com.palantir.conjure.examples.recipes.api.RecipeName;
import feign.Client;
import feign.Feign;
import feign.FeignException;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
import feign.jaxrs.JAXRSContract;
import io.dropwizard.testing.junit.DropwizardAppRule;
import java.util.List;
import java.util.Set;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
Expand Down Expand Up @@ -63,11 +65,11 @@ public void getRecipeUsingInvalidName() {
public void getRecipe() {
RecipeName recipeName = RecipeName.of("roasted broccoli with garlic");
Recipe recipe = client.getRecipe(recipeName);
Recipe expectedRecipe = RULE.getConfiguration().getRecipes().stream().filter(
r -> r.getName().equals(recipeName)).findFirst().get();
Recipe expectedRecipe = RULE.getConfiguration().getRecipes().stream()
.filter(r -> r.getName().equals(recipeName)).findFirst().get();
assertEquals(expectedRecipe, recipe);

List<Recipe> recipes = client.getRecipes();
Set<Recipe> recipes = client.getAllRecipes();
assertEquals(RULE.getConfiguration().getRecipes(), recipes);
}
}

0 comments on commit 0803557

Please sign in to comment.