Skip to content

Commit cd64380

Browse files
Improve the javadoc for the API. (#33)
* Some code extracts had strange spacing because of longstanding bugs in the javadoc tool. * Most javadoc warnings have been fixed, often at the cost of annoyingly redundant `@param` and `@return` tags.
1 parent 7d1ab74 commit cd64380

File tree

6 files changed

+92
-17
lines changed

6 files changed

+92
-17
lines changed

functions-framework-api/src/main/java/com/google/cloud/functions/BackgroundFunction.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323
* <p>Here is an example of an implementation that accesses the {@code messageId} property from
2424
* a payload that matches a user-defined {@code PubSubMessage} class:
2525
*
26+
* <!-- The {@code} placement is a bit strange here, to prevent spurious spaces introduced by the
27+
* javadoc tool. -->
2628
* <pre>
27-
* public class Example implements {@code BackgroundFunction<PubSubMessage>} {
29+
* public class Example implements{@code BackgroundFunction<PubSubMessage>} {
2830
* private static final Logger logger = Logger.getLogger(Example.class.getName());
2931
*
30-
* {@code @Override}
32+
* {@code @Override}
3133
* public void accept(PubSubMessage pubSubMessage, Context context) {
3234
* logger.info("Got messageId " + pubSubMessage.messageId);
3335
* }
@@ -36,7 +38,7 @@
3638
* // Where PubSubMessage is a user-defined class like this:
3739
* public class PubSubMessage {
3840
* String data;
39-
* {@code Map<String, String>} attributes;
41+
* {@code Map<String, String>} attributes;
4042
* String messageId;
4143
* String publishTime;
4244
* }
@@ -54,6 +56,7 @@ public interface BackgroundFunction<T> {
5456
* @param payload the payload of the event, deserialized from the original JSON string.
5557
* @param context the context of the event. This is a set of values that every event has,
5658
* separately from the payload, such as timestamp and event type.
59+
* @throws Exception to produce a 500 status code in the HTTP response.
5760
*/
5861
void accept(T payload, Context context) throws Exception;
5962
}

functions-framework-api/src/main/java/com/google/cloud/functions/HttpFunction.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ public interface HttpFunction {
2323
* Called to service an incoming HTTP request. This interface is implemented by user code to
2424
* provide the action for a given function. If the method throws any exception (including any
2525
* {@link Error}) then the HTTP response will have a 500 status code.
26+
*
27+
* @param request a representation of the incoming HTTP request.
28+
* @param response an object that can be used to provide the corresponding HTTP response.
29+
* @throws Exception if thrown, the HTTP response will have a 500 status code.
2630
*/
2731
void service(HttpRequest request, HttpResponse response) throws Exception;
2832
}

functions-framework-api/src/main/java/com/google/cloud/functions/HttpMessage.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,26 @@
2525
* Represents an HTTP message, either an HTTP request or a part of a multipart HTTP request.
2626
*/
2727
public interface HttpMessage {
28-
/** Returns the value of the {@code Content-Type} header, if any. */
28+
/**
29+
* Returns the value of the {@code Content-Type} header, if any.
30+
*
31+
* @return the content type, if any.
32+
*/
2933
Optional<String> getContentType();
3034

31-
/** Returns the numeric value of the {@code Content-Length} header. */
35+
/**
36+
* Returns the numeric value of the {@code Content-Length} header.
37+
*
38+
* @return the content length.
39+
*/
3240
long getContentLength();
3341

3442
/**
3543
* Returns the character encoding specified in the {@code Content-Type} header,
3644
* or {@code Optional.empty()} if there is no {@code Content-Type} header or it does not have the
3745
* {@code charset} parameter.
46+
*
47+
* @return the character encoding for the content type, if one is specified.
3848
*/
3949
Optional<String> getCharacterEncoding();
4050

@@ -44,6 +54,7 @@ public interface HttpMessage {
4454
* This method is typically used to read binary data. If the body is text, the
4555
* {@link #getReader()} method is more appropriate.
4656
*
57+
* @return an {@link InputStream} that can be used to read the body of this HTTP request.
4758
* @throws IOException if a valid {@link InputStream} cannot be returned for some reason.
4859
* @throws IllegalStateException if {@link #getReader()} has already been called on this instance.
4960
*/
@@ -53,6 +64,7 @@ public interface HttpMessage {
5364
* Returns a {@link BufferedReader} that can be used to read the text body of this HTTP request.
5465
* Every call to this method on the same {@link HttpMessage} will return the same object.
5566
*
67+
* @return a {@link BufferedReader} that can be used to read the text body of this HTTP request.
5668
* @throws IOException if a valid {@link BufferedReader} cannot be returned for some reason.
5769
* @throws IllegalStateException if {@link #getInputStream()} has already been called on this
5870
* instance.
@@ -69,9 +81,12 @@ public interface HttpMessage {
6981
* Some-Header: another value
7082
* </pre>
7183
*
72-
* ...then the returned value will map {@code Content-Type} to a one-element list containing
73-
* {@code text/plain}, and {@code Some-Header} to a two-element list containing {@code some value}
74-
* and {@code another value}.
84+
* ...then the returned value will map {@code "Content-Type"} to a one-element list containing
85+
* {@code "text/plain"}, and {@code "Some-Header"} to a two-element list containing
86+
* {@code "some value"} and {@code "another value"}.
87+
*
88+
* @return a map where each key is an HTTP header and the corresponding {@code List} value has
89+
* one element for each occurrence of that header.
7590
*/
7691
Map<String, List<String>> getHeaders();
7792

@@ -87,6 +102,10 @@ public interface HttpMessage {
87102
*
88103
* ...then {@code getFirstHeader("Some-Header")} will return {@code Optional.of("some value")},
89104
* and {@code getFirstHeader("Another-Header")} will return {@code Optional.empty()}.
105+
*
106+
* @param name an HTTP header name.
107+
*
108+
* @return the first value of the given header, if present.
90109
*/
91110
default Optional<String> getFirstHeader(String name) {
92111
List<String> headers = getHeaders().get(name);

functions-framework-api/src/main/java/com/google/cloud/functions/HttpRequest.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,45 @@
2222
* Represents the contents of an HTTP request that is being serviced by a Cloud Function.
2323
*/
2424
public interface HttpRequest extends HttpMessage {
25-
/** The HTTP method of this request, such as {@code "POST"} or {@code "GET"}. */
25+
/**
26+
* The HTTP method of this request, such as {@code "POST"} or {@code "GET"}.
27+
*
28+
* @return the HTTP method of this request.
29+
*/
2630
String getMethod();
2731

28-
/** The full URI of this request as it arrived at the server. */
32+
/**
33+
* The full URI of this request as it arrived at the server.
34+
*
35+
* @return the full URI of this request.
36+
*/
2937
String getUri();
3038

3139
/**
3240
* The path part of the URI for this request, without any query. If the full URI is
3341
* {@code http://foo.com/bar/baz?this=that}, then this method will return {@code /bar/baz}.
42+
*
43+
* @return the path part of the URI for this request.
3444
*/
3545
String getPath();
3646

3747
/**
3848
* The query part of the URI for this request. If the full URI is
3949
* {@code http://foo.com/bar/baz?this=that}, then this method will return {@code this=that}.
4050
* If there is no query part, the returned {@code Optional} is empty.
51+
*
52+
* @return the query part of the URI, if any.
4153
*/
4254
Optional<String> getQuery();
4355

4456
/**
4557
* The query parameters of this request. If the full URI is
4658
* {@code http://foo.com/bar?thing=thing1&thing=thing2&cat=hat}, then the returned map will map
47-
* {@code thing} to the list {@code "thing1", "thing2"} and {@code cat} to the list with the
59+
* {@code thing} to the list {@code ["thing1", "thing2"]} and {@code cat} to the list with the
4860
* single element {@code "hat"}.
61+
*
62+
* @return a map where each key is the name of a query parameter and the corresponding
63+
* {@code List} value indicates every value that was associated with that name.
4964
*/
5065
Map<String, List<String>> getQueryParameters();
5166

@@ -55,6 +70,9 @@ public interface HttpRequest extends HttpMessage {
5570
* {@code getFirstQueryParameter("thing")} will return {@code Optional.of("thing1")} and
5671
* {@code getFirstQueryParameter("something")} will return {@code Optional.empty()}. This is a
5772
* more convenient alternative to {@link #getQueryParameters}.
73+
*
74+
* @param name a query parameter name.
75+
* @return the first query parameter value with the given name, if any.
5876
*/
5977
default Optional<String> getFirstQueryParameter(String name) {
6078
List<String> parameters = getQueryParameters().get(name);
@@ -70,7 +88,11 @@ default Optional<String> getFirstQueryParameter(String name) {
7088
* {@link HttpMessage}.
7189
*/
7290
interface HttpPart extends HttpMessage {
73-
/** Returns the filename associated with this part, if any. */
91+
/**
92+
* Returns the filename associated with this part, if any.
93+
*
94+
* @return the filename associated with this part, if any.
95+
*/
7496
Optional<String> getFileName();
7597
}
7698

@@ -79,6 +101,7 @@ interface HttpPart extends HttpMessage {
79101
* in the returned map has the name of the part as its key and the contents as the associated
80102
* value.
81103
*
104+
* @return a map from part names to part contents.
82105
* @throws IllegalStateException if the {@link #getContentType() content type} is not
83106
* {@code multipart/form-data}.
84107
*/

functions-framework-api/src/main/java/com/google/cloud/functions/HttpResponse.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,42 +29,63 @@ public interface HttpResponse {
2929
/**
3030
* Sets the numeric HTTP
3131
* <a href="https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml">status
32-
* code</a> to use in the response. Most often this will be 200, which is the OK status.
32+
* code</a> to use in the response. Most often this will be 200, which is the OK status. The
33+
* named constants in {@link java.net.HttpURLConnection}, such as
34+
* {@link java.net.HttpURLConnection#HTTP_OK HTTP_OK}, can be used as an alternative to writing
35+
* numbers in your source code.
36+
*
37+
* @param code the status code.
3338
*/
3439
void setStatusCode(int code);
3540

3641
/**
3742
* Sets the numeric HTTP
3843
* <a href="https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml">status
3944
* code</a> and reason message to use in the response. For example<br>
40-
* {@code setStatusCode(400, "Something went wrong")}.
45+
* {@code setStatusCode(400, "Something went wrong")}. The
46+
* named constants in {@link java.net.HttpURLConnection}, such as
47+
* {@link java.net.HttpURLConnection#HTTP_BAD_REQUEST HTTP_BAD_REQUEST}, can be used as an
48+
* alternative to writing numbers in your source code.
49+
*
50+
* @param code the status code.
51+
* @param message the status message.
4152
*/
4253
void setStatusCode(int code, String message);
4354

4455
/**
4556
* Sets the value to use for the {@code Content-Type} header in the response. This may include
4657
* a character encoding, for example {@code setContentType("text/plain; charset=utf-8")}.
58+
*
59+
* @param contentType the content type.
4760
*/
4861
void setContentType(String contentType);
4962

5063
/**
5164
* Returns the {@code Content-Type} that was previously set by {@link #setContentType}, or by
5265
* {@link #appendHeader} with a header name of {@code Content-Type}. If no {@code Content-Type}
5366
* has been set, returns {@code Optional.empty()}.
67+
*
68+
* @return the content type, if any.
5469
*/
5570
Optional<String> getContentType();
5671

5772
/**
5873
* Includes the given header name with the given value in the response. This method may be called
5974
* several times for the same header, in which case the response will contain the header the same
6075
* number of times.
76+
*
77+
* @param header an HTTP header, such as {@code Content-Type}.
78+
* @param value a value to associate with that header.
6179
*/
6280
void appendHeader(String header, String value);
6381

6482
/**
6583
* Returns the headers that have been defined for the response so far. This will contain at least
6684
* the headers that have been set via {@link #appendHeader} or {@link #setContentType}, and may
6785
* contain additional headers such as {@code Date}.
86+
*
87+
* @return a map where each key is a header name and the corresponding {@code List} value has one
88+
* entry for every value associated with that header.
6889
*/
6990
Map<String, List<String>> getHeaders();
7091

@@ -73,6 +94,7 @@ public interface HttpResponse {
7394
* This method is typically used to write binary data. If the body is text, the
7495
* {@link #getWriter()} method is more appropriate.
7596
*
97+
* @return the output stream.
7698
* @throws IOException if a valid {@link OutputStream} cannot be returned for some reason.
7799
* @throws IllegalStateException if {@link #getWriter} has already been called on this instance.
78100
*/
@@ -85,6 +107,7 @@ public interface HttpResponse {
85107
* {@link #appendHeader appendHeader("Content-Type", "text/foo; charset=bar")}
86108
* before calling this method.
87109
*
110+
* @return the writer.
88111
* @throws IOException if a valid {@link BufferedWriter} cannot be returned for some reason.
89112
* @throws IllegalStateException if {@link #getOutputStream} has already been called on this
90113
* instance.

functions-framework-api/src/main/java/com/google/cloud/functions/RawBackgroundFunction.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
* <p>Here is an example of an implementation that parses the JSON payload using Gson, to access its
2323
* {@code messageId} property:
2424
*
25+
* <!-- The {@code} placement is a bit strange here, to prevent spurious spaces introduced by the
26+
* javadoc tool. -->
2527
* <pre>
2628
* public class Example implements RawBackgroundFunction {
2729
* private static final Logger logger = Logger.getLogger(Example.class.getName());
2830
*
29-
* {@code @Override}
31+
* {@code @Override}
3032
* public void accept(String json, Context context) {
3133
* JsonObject jsonObject = new Gson().fromJson(json, JsonObject.class);
3234
* JsonElement messageId = jsonObject.get("messageId");
@@ -43,7 +45,7 @@
4345
* public class Example implements RawBackgroundFunction {
4446
* private static final Logger logger = Logger.getLogger(Example.class.getName());
4547
*
46-
* {@code @Override}
48+
* {@code @Override}
4749
* public void accept(String json, Context context) {
4850
* PubSubMessage message = new Gson().fromJson(json, PubSubMessage.class);
4951
* logger.info("Got messageId " + message.messageId);
@@ -53,7 +55,7 @@
5355
* // Where PubSubMessage is a user-defined class like this:
5456
* public class PubSubMessage {
5557
* String data;
56-
* {@code Map<String, String>} attributes;
58+
* {@code Map<String, String>} attributes;
5759
* String messageId;
5860
* String publishTime;
5961
* }
@@ -69,6 +71,7 @@ public interface RawBackgroundFunction {
6971
* @param json the payload of the event, as a JSON string.
7072
* @param context the context of the event. This is a set of values that every event has,
7173
* separately from the payload, such as timestamp and event type.
74+
* @throws Exception to produce a 500 status code in the HTTP response.
7275
*/
7376
void accept(String json, Context context) throws Exception;
7477
}

0 commit comments

Comments
 (0)