-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added support for httpRule definitions in YAML files * Yaml path can now be passed in. Added tests around YAML functionality * removed hardcoded path
- Loading branch information
Showing
8 changed files
with
251 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
http: | ||
rules: | ||
- selector: TestService.TestMethod4 | ||
get: /yaml_users/{s}/{uint3}/{nt.f1} | ||
- selector: TestService.TestMethod5 | ||
get: /yaml_users/{s=hello/**}/x/{uint3}/{nt.f1}/*/**/test | ||
- selector: TestService.TestMethod6 | ||
post: /yaml_users/ | ||
body: "*" | ||
additionalBindings: | ||
- post: /yaml_users_nested | ||
body: "nt" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
protoc-gen-jersey/src/main/java/com/fullcontact/rpc/jersey/yaml/YamlHttpConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.fullcontact.rpc.jersey.yaml; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
/** | ||
* | ||
* Allows HTTPRules to be generated from a .yml file instead of the .proto files. | ||
* Any rule defined in a .yml file will override rules in the .proto. | ||
* https://cloud.google.com/endpoints/docs/grpc-service-config | ||
* | ||
* Path to the .yml file should be passed in as part of the optional parameter | ||
* | ||
* @author Kyle Hansen (sypticus) | ||
*/ | ||
public class YamlHttpConfig { | ||
public Map<String, List<YamlHttpRule>> http; | ||
public List<YamlHttpRule> getRules(){ | ||
return http.get("rules"); | ||
} | ||
|
||
public static Optional<YamlHttpConfig> getFromOptions(Set<String> options) { | ||
Optional<String> yamlOption = options.stream().filter(option -> option.startsWith("yaml=")).findFirst(); | ||
if(yamlOption.isPresent()) { | ||
String yamlPath = yamlOption.get().split("=")[1]; | ||
try { | ||
File yamlFile = new File(yamlPath); | ||
if(!yamlFile.exists()) { | ||
throw new RuntimeException("YAMLs file does not exist: "+ yamlFile.getAbsolutePath()); | ||
} | ||
InputStream yamlStream = new FileInputStream(yamlFile); | ||
|
||
ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); | ||
return Optional.of(mapper.readValue(yamlStream, YamlHttpConfig.class)); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to parse YAML", e); | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
protoc-gen-jersey/src/main/java/com/fullcontact/rpc/jersey/yaml/YamlHttpRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.fullcontact.rpc.jersey.yaml; | ||
|
||
import com.google.api.HttpRule; | ||
import lombok.Value; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
/** | ||
* | ||
* HTTPRules defined in the .yml will be parsed into a YamlHTTPRule, from which a com.google.api.HttpRule can be generated. | ||
* | ||
* @author Kyle Hansen (sypticus) | ||
*/ | ||
|
||
@Value | ||
public class YamlHttpRule { | ||
String selector; | ||
String get; | ||
String post; | ||
String put; | ||
String delete; | ||
String body; | ||
List<YamlHttpRule> additionalBindings; | ||
|
||
public HttpRule buildHttpRule() { | ||
HttpRule.Builder builder = HttpRule.newBuilder(); | ||
if(get != null){ | ||
builder.setGet(get); | ||
} | ||
if(put != null){ | ||
builder.setPut(put); | ||
} | ||
if(delete != null){ | ||
builder.setDelete(delete); | ||
} | ||
if(post != null){ | ||
builder.setPost(post); | ||
} | ||
if(body != null){ | ||
builder.setBody(body); | ||
} | ||
if(additionalBindings != null){ | ||
builder.addAllAdditionalBindings(additionalBindings.stream().map(YamlHttpRule::buildHttpRule).collect(Collectors.toList())); | ||
} | ||
|
||
return builder.build(); | ||
|
||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters