1
- /*
2
- * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
- * SPDX-License-Identifier: Apache-2.0
4
- */
5
-
6
1
package software .amazon .smithy .java .example ;
7
2
8
- import static java .nio .ByteBuffer .wrap ;
9
-
10
- import java .nio .charset .StandardCharsets ;
11
- import java .time .Instant ;
12
- import java .util .List ;
13
- import java .util .concurrent .ExecutionException ;
14
- import org .junit .jupiter .api .Test ;
15
3
import software .amazon .smithy .java .client .core .endpoint .EndpointResolver ;
16
4
import software .amazon .smithy .java .client .core .interceptors .ClientInterceptor ;
17
5
import software .amazon .smithy .java .client .core .interceptors .RequestHook ;
28
16
import software .amazon .smithy .java .io .datastream .DataStream ;
29
17
import software .amazon .smithy .java .json .JsonCodec ;
30
18
31
- public class GenericTest {
19
+ import java .nio .charset .StandardCharsets ;
20
+ import java .time .Instant ;
21
+ import java .util .List ;
22
+ import java .util .concurrent .ExecutionException ;
23
+
24
+ import static java .nio .ByteBuffer .wrap ;
25
+
26
+ public final class ClientExample {
27
+ public static void main (String [] args ) throws Exception {
28
+ putPerson ();
29
+ getPersonImage ();
30
+ streamingRequestPayload ();
31
+ testDocument ();
32
+ serde ();
33
+ supportsInterceptors ();
34
+ }
32
35
33
- @ Test
34
- public void putPerson () throws ExecutionException , InterruptedException {
36
+ public static void putPerson () throws ExecutionException , InterruptedException {
35
37
// Create a generated client using rest-json and a fixed endpoint.
36
38
var client = PersonDirectoryClient .builder ()
37
- .endpointResolver (EndpointResolver .staticHost ("http://httpbin.org/anything" ))
38
- .build ();
39
+ .endpointResolver (EndpointResolver .staticHost ("http://httpbin.org/anything" ))
40
+ .build ();
39
41
40
42
PutPersonInput input = PutPersonInput .builder ()
41
- .name ("Michael" )
42
- .age (999 )
43
- .favoriteColor ("Green" )
44
- .birthday (Instant .now ())
45
- .build ();
43
+ .name ("Michael" )
44
+ .age (999 )
45
+ .favoriteColor ("Green" )
46
+ .birthday (Instant .now ())
47
+ .build ();
46
48
47
49
PutPersonOutput output = client .putPerson (input );
48
50
System .out .println ("Output: " + output );
49
51
}
50
52
51
- @ Test
52
- public void getPersonImage () {
53
+ public static void getPersonImage () {
53
54
PersonDirectoryClient client = PersonDirectoryClient .builder ()
54
- .endpointResolver (EndpointResolver .staticHost ("http://httpbin.org/anything" ))
55
- .build ();
55
+ .endpointResolver (EndpointResolver .staticHost ("http://httpbin.org/anything" ))
56
+ .build ();
56
57
57
58
GetPersonImageInput input = GetPersonImageInput .builder ().name ("Michael" ).build ();
58
59
GetPersonImageOutput output = client .getPersonImage (input );
59
60
System .out .println ("Output: " + output );
60
61
}
61
62
62
- @ Test
63
- public void streamingRequestPayload () {
63
+ public static void streamingRequestPayload () {
64
64
PersonDirectoryClient client = PersonDirectoryClient .builder ()
65
- .endpointResolver (EndpointResolver .staticHost ("http://httpbin.org/anything" ))
66
- .build ();
65
+ .endpointResolver (EndpointResolver .staticHost ("http://httpbin.org/anything" ))
66
+ .build ();
67
67
68
68
PutPersonImageInput input = PutPersonImageInput .builder ()
69
- .name ("Michael" )
70
- .tags (List .of ("Foo" , "Bar" ))
71
- .moreTags (List .of ("Abc" , "one two" ))
72
- .image (DataStream .ofString ("image..." ))
73
- .build ();
69
+ .name ("Michael" )
70
+ .tags (List .of ("Foo" , "Bar" ))
71
+ .moreTags (List .of ("Abc" , "one two" ))
72
+ .image (DataStream .ofString ("image..." ))
73
+ .build ();
74
74
PutPersonImageOutput output = client .putPersonImage (input );
75
75
System .out .println ("Output: " + output );
76
76
}
77
77
78
- @ Test
79
- public void testDocument () {
78
+ public static void testDocument () {
80
79
Codec codec = JsonCodec .builder ().useJsonName (true ).useTimestampFormat (true ).build ();
81
80
82
81
PutPersonInput input = PutPersonInput .builder ()
83
- .name ("Michael" )
84
- .age (999 )
85
- .favoriteColor ("Green" )
86
- .birthday (Instant .now ())
87
- .binary (wrap ("Hello" .getBytes (StandardCharsets .UTF_8 )))
88
- .build ();
82
+ .name ("Michael" )
83
+ .age (999 )
84
+ .favoriteColor ("Green" )
85
+ .birthday (Instant .now ())
86
+ .binary (wrap ("Hello" .getBytes (StandardCharsets .UTF_8 )))
87
+ .build ();
89
88
90
89
// Serialize directly to JSON.
91
90
System .out .println (codec .serializeToString (input ));
@@ -101,14 +100,13 @@ public void testDocument() {
101
100
System .out .println (codec .serializeToString (inputCopy ));
102
101
}
103
102
104
- @ Test
105
- public void serde () {
103
+ public static void serde () {
106
104
PutPersonInput input = PutPersonInput .builder ()
107
- .name ("Michael" )
108
- .age (999 )
109
- .favoriteColor ("Green" )
110
- .birthday (Instant .now ())
111
- .build ();
105
+ .name ("Michael" )
106
+ .age (999 )
107
+ .favoriteColor ("Green" )
108
+ .birthday (Instant .now ())
109
+ .build ();
112
110
113
111
JsonCodec codec = JsonCodec .builder ().useJsonName (true ).useTimestampFormat (true ).build ();
114
112
@@ -122,8 +120,7 @@ public void serde() {
122
120
System .out .println (codec .serializeToString (copy ));
123
121
}
124
122
125
- @ Test
126
- public void supportsInterceptors () throws Exception {
123
+ public static void supportsInterceptors () throws Exception {
127
124
var interceptor = new ClientInterceptor () {
128
125
@ Override
129
126
public void readBeforeTransmit (RequestHook <?, ?, ?> hook ) {
@@ -133,15 +130,15 @@ public void readBeforeTransmit(RequestHook<?, ?, ?> hook) {
133
130
@ Override
134
131
public <RequestT > RequestT modifyBeforeTransmit (RequestHook <?, ?, RequestT > hook ) {
135
132
return hook .mapRequest (
136
- HttpRequest .class ,
137
- h -> h .request ().toBuilder ().withAddedHeader ("X-Foo" , "Bar" ).build ());
133
+ HttpRequest .class ,
134
+ h -> h .request ().toBuilder ().withAddedHeader ("X-Foo" , "Bar" ).build ());
138
135
}
139
136
};
140
137
141
138
PersonDirectoryClient client = PersonDirectoryClient .builder ()
142
- .endpointResolver (EndpointResolver .staticHost ("http://httpbin.org/anything" ))
143
- .addInterceptor (interceptor )
144
- .build ();
139
+ .endpointResolver (EndpointResolver .staticHost ("http://httpbin.org/anything" ))
140
+ .addInterceptor (interceptor )
141
+ .build ();
145
142
146
143
GetPersonImageInput input = GetPersonImageInput .builder ().name ("Michael" ).build ();
147
144
GetPersonImageOutput output = client .getPersonImage (input );
0 commit comments