Skip to content

Commit

Permalink
Merge 8c8c06e into 1b5e03e
Browse files Browse the repository at this point in the history
  • Loading branch information
while-loop committed Jul 26, 2018
2 parents 1b5e03e + 8c8c06e commit 33c1abb
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 80 deletions.
5 changes: 5 additions & 0 deletions .ci/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

echo $GPG_SECRET_KEYS | base64 --decode | $GPG_EXECUTABLE --import
echo $GPG_OWNERTRUST | base64 --decode | $GPG_EXECUTABLE --import-ownertrust
mvn -s .ci/settings.xml deploy -DskipTests -B -P release
13 changes: 13 additions & 0 deletions .ci/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,17 @@
<password>${env.SONATYPE_PASSWORD}</password>
</server>
</servers>

<profiles>
<profile>
<id>ossrh</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<gpg.executable>${env.GPG_EXECUTABLE}</gpg.executable>
<gpg.passphrase>${env.GPG_PASSPHRASE}</gpg.passphrase>
</properties>
</profile>
</profiles>
</settings>
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ cache:

deploy:
provider: script
script: "mvn -s .ci/settings.xml deploy -DskipTests -B"
script: bash .ci/deploy.sh
skip_cleanup: true
on:
branch:
Expand Down
12 changes: 9 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [1.0.2] yyyy-mm-dd
## [1.0.3] yyyy-mm-dd

## [1.0.2] 2018-07-25
### Added
- More Json helper methods in [`HttpRequest`](src/main/java/com/github/whileloop/rest4j/HttpRequest.java)
- More Json helper methods in [`HttpResponse`](src/main/java/com/github/whileloop/rest4j/HttpResponse.java)

## [1.0.1] 2018-07-24
### Added
- Json helper methods in [`HttpRequest`](src/main/java/com/github/whileloop/rest4j/HttpRequest.java)
Expand All @@ -24,7 +29,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

[1.0.0]: https://github.com/while-loop/rest4j/releases/tag/v1.0.0
[1.0.1]: https://github.com/while-loop/rest4j/compare/v1.0.0...v1.0.1
[1.0.2]: https://github.com/while-loop/rest4j/compare/v1.0.1...master
[unreleased]: https://github.com/while-loop/rest4j/compare/v1.0.1...master
[1.0.2]: https://github.com/while-loop/rest4j/compare/v1.0.1...v1.0.2
[1.0.3]: https://github.com/while-loop/rest4j/compare/v1.0.2...develop
[unreleased]: https://github.com/while-loop/rest4j/compare/v1.0.2...develop

[comment]: # (Added, Changed, Removed)
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Download
#### Gradle
```gradle
dependencies {
compile 'com.github.while-loop:rest4j:1.0.1'
compile 'com.github.while-loop:rest4j:1.0.2'
}
```

Expand All @@ -36,7 +36,7 @@ dependencies {
<dependency>
<groupId>com.github.while-loop</groupId>
<artifactId>rest4j</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
</dependency>
```

Expand Down
95 changes: 53 additions & 42 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.while-loop</groupId>
<artifactId>rest4j</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>

<developers>
<developer>
Expand All @@ -30,10 +30,58 @@
</scm>

<profiles>
<!-- Activate using the release property: mvn clean install -Prelease -->
<profile>
<id>sign</id>
<id>release</id>
<activation>
<property>
<name>release</name>
</property>
</activation>

<build>
<plugins>
<!-- To release to Maven central -->
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.8</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<!-- To generate javadoc -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.4</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>

<!-- To sign the artifacts -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
Expand All @@ -48,7 +96,8 @@
</execution>
</executions>
</plugin>
</plugins>
</plugins>

</build>
</profile>
</profiles>
Expand Down Expand Up @@ -81,43 +130,6 @@
<aggregate>true</aggregate>
</configuration>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.7</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down Expand Up @@ -188,5 +200,4 @@
<scope>test</scope>
</dependency>
</dependencies>

</project>
</project>
19 changes: 13 additions & 6 deletions src/main/java/com/github/whileloop/rest4j/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.lang.reflect.Type;
import java.net.InetSocketAddress;
import java.net.URL;

/**
* Created by aalves on 12/18/17
*/
public interface HttpRequest extends HttpContext {
JsonParser parser = new JsonParser();
Gson GSON = new Gson();

/**
* If getBody or getRawBody has already been called, this stream will be empty
*
Expand All @@ -31,11 +30,19 @@ public interface HttpRequest extends HttpContext {

String getProtocol();

default JsonElement bodyAsJson() {
return parser.parse(new InputStreamReader(getRawBody()));
default JsonElement asJson() {
return JsonUtils.asJson(new InputStreamReader(getRawBody()));
}

default String asString() {
return JsonUtils.is2String(getRawBody());
}

default <T> T asObject(Class<T> clazz) {
return GSON.fromJson(new InputStreamReader(getRawBody()), clazz);
return JsonUtils.asObject(new InputStreamReader(getRawBody()), clazz);
}

default <T> T asObject(Type type) {
return JsonUtils.asObject(new InputStreamReader(getRawBody()), type);
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/github/whileloop/rest4j/HttpResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.IOException;
import java.io.OutputStream;

import static com.github.whileloop.rest4j.HttpRequest.GSON;

/**
* Created by aalves on 12/18/17
Expand All @@ -12,7 +11,7 @@ public interface HttpResponse extends HttpContext {
OutputStream getRawBody();

default void write(Object object) throws IOException {
write(GSON.toJson(object));
write(JsonUtils.toJson(object));
}

void write(byte[] content) throws IOException;
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/github/whileloop/rest4j/JsonUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.whileloop.rest4j;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;

public class JsonUtils {
static final Gson GSON = new Gson();
static final JsonParser PARSER = new JsonParser();

static JsonElement asJson(Reader reader) {
return PARSER.parse(reader);
}

static <T> T asObject(Reader reader, Class<T> clazz) {
return GSON.fromJson(reader, clazz);
}

static <T> T asObject(Reader reader, Type type) {
return GSON.fromJson(reader, type);
}

static String is2String(InputStream is) {
try (java.util.Scanner s = new java.util.Scanner(is, StandardCharsets.UTF_8.name())) {
s.useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
}

static String toJson(Object object) {
return GSON.toJson(object);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.github.whileloop.rest4j.HttpMethod;
import com.github.whileloop.rest4j.HttpRequest;
import com.github.whileloop.rest4j.HttpStatus;

import java.io.InputStream;
import java.io.StringBufferInputStream;
Expand All @@ -13,22 +12,26 @@
import java.util.List;
import java.util.Map;

import static com.github.whileloop.rest4j.HttpStatus.OK;

/**
* Created by aalves on 12/18/17
*/
public class RequestRecorder implements HttpRequest {
private StringBuffer buf = new StringBuffer();
private HttpStatus status = OK;
private HttpHeaders headers = new HttpHeaders();
private Map<String, Object> params = new HashMap<>();
private HttpMethod method;
private URL url;

public RequestRecorder(HttpMethod method, String url) throws MalformedURLException {
this(method, url, null);
}

public RequestRecorder(HttpMethod method, String url, String body) throws MalformedURLException {
this.method = method;
this.url = new URL(url);
if (body != null) {
this.buf.append(body);
}
}

public RequestRecorder() {
Expand Down
Loading

0 comments on commit 33c1abb

Please sign in to comment.