-
Notifications
You must be signed in to change notification settings - Fork 6
/
service.go
60 lines (53 loc) · 2.34 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// MIT License
//
// Copyright (c) 2020 goctl-android
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
package template
var Service = `package {{.ParentPackage}}.service;
{{.Import}}
import com.alibaba.fastjson.JSON;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class Service {
private static final String MEDIA_TYPE_JSON = "application/json; charset=utf-8";
private static final String BASE_RUL = "http://localhost:8888/";// TODO replace to your host and delete this comment
private static Service instance;
private static IService service;
private Service() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_RUL)
.addConverterFactory(GsonConverterFactory.create())
.build();
service = retrofit.create(IService.class);
}
public static Service getInstance() {
if (instance == null) {
instance = new Service();
}
return instance;
}
private RequestBody buildJSONBody(Object obj) {
String s = JSON.toJSONString(obj);
return RequestBody.create(s, MediaType.parse(MEDIA_TYPE_JSON));
}
{{range $index,$item := .Routes}}{{$item.Doc}}
public void {{$item.MethodName}}({{if $item.HasRequest}}{{$item.RequestBeanName}} in, {{end}}Callback{{if $item.HasResponse}}<{{$item.ResponseBeanName}}>{{else}}<Void>{{end}} callback) {
Call{{if $item.HasResponse}}<{{$item.ResponseBeanName}}>{{else}}<Void>{{end}} call = service.{{$item.MethodName}}({{if $item.HavePath}}{{$item.PathId}}{{end}}{{if $item.HaveQuery}}{{$item.QueryId}}{{end}}{{if $item.ShowRequestBody}}buildJSONBody(in){{end}});
call.enqueue(callback);
}
{{end}}
}
`