Skip to content

Commit 79404be

Browse files
committed
Merge branch 'feature/swagger-codegen' into develop
* feature/swagger-codegen: Added category, user, comment param in spec Changed auth param to definition auth_param Changed put method of body param to definition post_param Added param definitions Deleted ./java Added AsyncTask life cycle Added dexOptions Fixed duplication post array Added comments, commants_pagination in post Added pagination, posts array Added array type object Added android java sample code Added generate okhttp-gson cmd Added schema '$ref' => '#/definitions/post' Added java of codegen
2 parents 794d422 + 0cd6ea8 commit 79404be

File tree

10 files changed

+492
-191
lines changed

10 files changed

+492
-191
lines changed

README.md

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -501,11 +501,84 @@ end
501501
502502
### Codegen
503503
> We developed server side code and We shoud need Client code. you can use Swagger-Codegen https://github.com/swagger-api/swagger-codegen#swagger-code-generator
504-
504+
505+
> https://github.com/swagger-api/swagger-codegen/wiki/FAQ#how-can-i-generate-an-android-sdk
505506
```bash
506507
brew install swagger-codegen
507-
swagger-codegen generate -i http://localhost:3000/api-docs/v1/swagger.yaml -l swift5 -o ./swift
508-
```
508+
mkdir -p /var/tmp/java/okhttp-gson/
509+
swagger-codegen generate -i http://localhost:3000/api-docs/v1/swagger.yaml \
510+
-l java --library=okhttp-gson \
511+
-D hideGenerationTimestamp=true \
512+
-o /var/tmp/java/okhttp-gson/
513+
```
514+
515+
Caused by: android.os.NetworkOnMainThreadException
516+
https://www.toptal.com/android/android-threading-all-you-need-to-know
517+
518+
sample
519+
https://i.stack.imgur.com/ytin1.png
520+
https://gist.github.com/just-kip/1376527af60c74b07bef7bd7f136ff56
521+
```java
522+
AsyncTask<Post, Void, Post> asyncTask = new AsyncTask<Post, Void, Post>() {
523+
@Override
524+
protected Post doInBackground(Post... params) {
525+
try {
526+
ApiClient defaultClient = Configuration.getDefaultApiClient();
527+
String authorization = "eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJleHAiOjE1ODIwOTg3NzF9.JGPR2oOOeGcjSocU4Ohvw1bg49ZjTQ9tQ3FtxmqmPDM"; // String | JWT token for Authorization
528+
ApiKeyAuth Bearer = (ApiKeyAuth) defaultClient.getAuthentication("Bearer");
529+
Bearer.setApiKey(authorization);
530+
PostApi apiInstance = new PostApi();
531+
String id = "1"; // String | id
532+
Integer commentPage = 1; // Integer | Page number for Comment
533+
Integer commentPer = 10; // Integer | Per page number For Comment
534+
Post result;
535+
try {
536+
result = apiInstance.apiV1PostsIdGet(id, authorization, commentPage, commentPer);
537+
// System.out.println(result);
538+
} catch (ApiException e) {
539+
System.err.println("Exception when calling PostApi#apiV1PostsIdGet");
540+
e.printStackTrace();
541+
result = new Post();
542+
}
543+
return result;
544+
} catch (Exception e) {
545+
e.printStackTrace();
546+
return new Post();
547+
}
548+
}
549+
550+
@Override
551+
protected void onPostExecute(Post post) {
552+
super.onPostExecute(post);
553+
if (post != null) {
554+
mEmailView.setText(post.getBody());
555+
System.out.print(post);
556+
}
557+
}
558+
};
559+
560+
asyncTask.execute();
561+
```
562+
563+
https://www.sitepoint.com/consuming-web-apis-in-android-with-okhttp/
564+
<uses-permission android:name="android.permission.INTERNET"/>
565+
566+
567+
No Network Security Config specified, using platform default
568+
Use 10.0.2.2 to access your actual machine.
569+
https://stackoverflow.com/questions/5528850/how-do-you-connect-localhost-in-the-android-emulator
570+
// private String basePath = "https://tutorial-rails-rest-api.herokuapp.com";
571+
// private String basePath = "http://localhost:3000";
572+
private String basePath = "http://10.0.2.2:3000";
573+
574+
swagger-annotations Unable to pre-dex
575+
https://stackoverflow.com/questions/43997544/execution-failed-for-task-java-lang-runtimeexceptionunable-to-pre-dex
576+
```groovy
577+
dexOptions {
578+
javaMaxHeapSize "2g" // set it to 4g will bring unable to start JavaVirtualMachine
579+
preDexLibraries = false
580+
}
581+
```
509582
510583
### Log For ELK stack (Elastic Search, Logstash, Kibana)
511584

spec/requests/api/v1/authentication_spec.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,9 @@
55
post('login authentication') do
66
tags 'Authentication'
77
consumes 'application/json'
8-
parameter name: :body, in: :body, required: true, schema: {
9-
type: :object,
10-
properties: {
11-
email: { type: :string, required: true, description: 'Email for Login', example: '[email protected]' },
12-
password: { type: :string, required: true, description: 'Password for Login', example: 'hello1234' }
13-
}
14-
}
15-
8+
parameter name: :body, in: :body, required: true, schema: {'$ref' => '#/definitions/auth_param' }
169
response(200, 'Ok') do
10+
schema '$ref' => '#/definitions/auth'
1711
let(:user){
1812
create(:user)
1913
}

spec/requests/api/v1/categories_spec.rb

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
end
9494

9595
response(200, 'Successful') do
96+
schema '$ref' => '#/definitions/categories'
9697
let(:user){
9798
create(:user)
9899
}
@@ -127,20 +128,10 @@
127128
security [Bearer: []]
128129
consumes 'application/json'
129130
parameter name: :Authorization, in: :header, type: :string, description: 'JWT token for Authorization'
130-
parameter name: :body, in: :body, required: true, schema: {
131-
type: :object,
132-
properties: {
133-
category: {
134-
type: :object,
135-
properties: {
136-
title: { type: :string },
137-
body: { type: :string }
138-
}
139-
}
140-
}
141-
}
131+
parameter name: :body, in: :body, required: true, schema: {'$ref' => '#/definitions/category_param' }
142132
produces 'application/json'
143133
response(201, 'Successful') do
134+
schema '$ref' => '#/definitions/category'
144135
let(:admin){
145136
create(:admin)
146137
}
@@ -212,6 +203,7 @@
212203
parameter name: :post_per, in: :query, type: :integer, description: 'Per page number For Post'
213204
produces 'application/json'
214205
response(200, 'Successful') do
206+
schema '$ref' => '#/definitions/category'
215207
let(:user){
216208
create(:user)
217209
}
@@ -269,19 +261,10 @@
269261
consumes 'application/json'
270262
parameter name: :Authorization, in: :header, type: :string, description: 'JWT token for Authorization'
271263
parameter name: 'id', in: :path, type: :string, description: 'id'
272-
parameter name: :body, in: :body, required: true, schema: {
273-
type: :object,
274-
properties: {
275-
category: {
276-
type: :object,
277-
properties: {
278-
body: { type: :string }
279-
}
280-
}
281-
}
282-
}
264+
parameter name: :body, in: :body, required: true, schema: {'$ref' => '#/definitions/category_param' }
283265
produces 'application/json'
284266
response(200, 'Successful') do
267+
schema '$ref' => '#/definitions/category'
285268
let(:admin){
286269
create(:admin)
287270
}

spec/requests/api/v1/comments_spec.rb

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
end
4646

4747
response(200, 'Successful') do
48+
schema '$ref' => '#/definitions/comments'
4849
let(:user){
4950
create(:user)
5051
}
@@ -124,20 +125,10 @@
124125
security [Bearer: []]
125126
consumes 'application/json'
126127
parameter name: :Authorization, in: :header, type: :string, description: 'JWT token for Authorization'
127-
parameter name: :body, in: :body, required: true, schema: {
128-
type: :object,
129-
properties: {
130-
comment: {
131-
type: :object,
132-
properties: {
133-
body: { type: :string },
134-
post_id: { type: :string }
135-
}
136-
}
137-
}
138-
}
128+
parameter name: :body, in: :body, required: true, schema: {'$ref' => '#/definitions/comment_param' }
139129
produces 'application/json'
140130
response(201, 'Successful') do
131+
schema '$ref' => '#/definitions/comment'
141132
let(:user){
142133
create(:user)
143134
}
@@ -194,6 +185,7 @@
194185
parameter name: 'id', in: :path, type: :string, description: 'id'
195186
produces 'application/json'
196187
response(200, 'Successful') do
188+
schema '$ref' => '#/definitions/comment'
197189
let(:user){
198190
create(:user)
199191
}
@@ -245,19 +237,10 @@
245237
consumes 'application/json'
246238
parameter name: :Authorization, in: :header, type: :string, description: 'JWT token for Authorization'
247239
parameter name: 'id', in: :path, type: :string, description: 'id'
248-
parameter name: :body, in: :body, required: true, schema: {
249-
type: :object,
250-
properties: {
251-
comment: {
252-
type: :object,
253-
properties: {
254-
body: { type: :string }
255-
}
256-
}
257-
}
258-
}
240+
parameter name: :body, in: :body, required: true, schema: {'$ref' => '#/definitions/comment_param' }
259241
produces 'application/json'
260242
response(200, 'Successful') do
243+
schema '$ref' => '#/definitions/comment'
261244
let(:comment){
262245
create(:comment)
263246
}

spec/requests/api/v1/posts_spec.rb

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
end
122122

123123
response(200, 'Successful') do
124+
schema '$ref' => '#/definitions/posts'
124125
let(:user){
125126
create(:user)
126127
}
@@ -187,21 +188,10 @@
187188
security [Bearer: []]
188189
consumes 'application/json'
189190
parameter name: :Authorization, in: :header, type: :string, description: 'JWT token for Authorization'
190-
parameter name: :body, in: :body, required: true, schema: {
191-
type: :object,
192-
properties: {
193-
post: {
194-
type: :object,
195-
properties: {
196-
title: { type: :string },
197-
body: { type: :string },
198-
category_id: { type: :integer }
199-
}
200-
}
201-
}
202-
}
191+
parameter name: :body, in: :body, required: true, schema: {'$ref' => '#/definitions/post_param' }
203192
produces 'application/json'
204193
response(201, 'Successful') do
194+
schema '$ref' => '#/definitions/post'
205195
let(:user){
206196
create(:user)
207197
}
@@ -280,6 +270,7 @@
280270
parameter name: :comment_per, in: :query, type: :integer, description: 'Per page number For Comment'
281271
produces 'application/json'
282272
response(200, 'Successful') do
273+
schema '$ref' => '#/definitions/post'
283274
let(:total_count) { 12 }
284275
let(:comment_count) { 15 }
285276
let(:category){
@@ -378,18 +369,7 @@
378369
consumes 'application/json'
379370
parameter name: :Authorization, in: :header, type: :string, description: 'JWT token for Authorization'
380371
parameter name: 'id', in: :path, type: :string, description: 'id'
381-
parameter name: :body, in: :body, required: true, schema: {
382-
type: :object,
383-
properties: {
384-
post: {
385-
type: :object,
386-
properties: {
387-
title: { type: :string },
388-
body: { type: :string }
389-
}
390-
}
391-
}
392-
}
372+
parameter name: :body, in: :body, required: true, schema: {'$ref' => '#/definitions/post_param' }
393373
produces 'application/json'
394374

395375
response(200, 'Successful - update after multuplart file upload') do
@@ -413,6 +393,7 @@
413393
end
414394

415395
response(200, 'Successful') do
396+
schema '$ref' => '#/definitions/post'
416397
let(:post){
417398
create(:post)
418399
}

spec/requests/api/v1/posts_upload_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
end
4343

4444
response(201, 'Successful') do
45+
schema '$ref' => '#/definitions/posts'
4546
let(:user){
4647
create(:user)
4748
}
@@ -75,6 +76,7 @@
7576
parameter name: 'post[body]', in: :formData, type: :string, required: true
7677
parameter name: 'post[files]', in: :formData, type: :file
7778
response(200, 'Successful') do
79+
schema '$ref' => '#/definitions/post'
7880
let(:user){
7981
create(:user)
8082
}

spec/requests/api/v1/users_spec.rb

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,7 @@
4747
post('create user') do
4848
tags 'User'
4949
consumes 'application/json'
50-
parameter name: :body, in: :body, required: true, schema: {
51-
type: :object,
52-
properties: {
53-
user: {
54-
type: :object,
55-
properties: {
56-
name: { type: :string },
57-
username: { type: :string },
58-
email: { type: :string },
59-
password: { type: :string },
60-
password_confirmation: { type: :string }
61-
}
62-
}
63-
}
64-
}
50+
parameter name: :body, in: :body, required: true, schema: {'$ref' => '#/definitions/user_param' }
6551
produces 'application/json'
6652

6753
let(:build_user){
@@ -164,21 +150,7 @@
164150
},
165151
required: ['_username']
166152
}
167-
parameter name: :body, in: :body, required: true, schema: {
168-
type: :object,
169-
properties: {
170-
user: {
171-
type: :object,
172-
properties: {
173-
name: { type: :string },
174-
username: { type: :string },
175-
email: { type: :string },
176-
password: { type: :string },
177-
password_confirmation: { type: :string }
178-
}
179-
}
180-
}
181-
}
153+
parameter name: :body, in: :body, required: true, schema: {'$ref' => '#/definitions/user_param' }
182154
produces 'application/json'
183155
response(200, 'Successful') do
184156
let(:user){

spec/requests/api/v1/users_upload_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
build(:user, avatar: uploadfile_avatar)
2323
}
2424
response(201, 'User created') do
25+
schema '$ref' => '#/definitions/user'
2526
let(:'user[name]') { build_user.name }
2627
let(:'user[username]') { build_user.username }
2728
let(:'user[email]') { build_user.email }
@@ -58,6 +59,7 @@
5859
parameter name: 'user[avatar]', in: :formData, type: :file
5960
produces 'application/json'
6061
response(200, 'Successful') do
62+
schema '$ref' => '#/definitions/user'
6163
let(:user){
6264
create(:user, avatar: uploadfile_avatar)
6365
}

0 commit comments

Comments
 (0)