Share
Embed
Transcript
25 slides
-
Protocol Buffers language-neutral, platform-neutral extensible mechanism for serializing structured data
ɾ ɾ faster than Json ɾ binary based data transmission -
Protocol Buffers json Protocol buffers \ QSFGFDUVSFT< \ JE OBNFւಓ
SPNBKJIPLLBJEP ^ \ JE OBNF੨ݝ SPNBKJBPNPSJ ^ \ ʜ \ JE OBNFࣛࣇౡݝ SPNBKJLBHPTIJNB ^ \ JE OBNFԭೄݝ SPNBKJPLJOBXB ^ > ^ ʜ -
How to use define proto file ɾ ɾ generate compile
/ decompile file ɾ make an API call -
define proto file ɾ ɾ generate compile / decompile file
ɾ make an API call How to use -
syntax = "proto3"; option java_package = "com.your.package.name"; option java_outer_classname =
"Prefectures"; option go_package = "proto"; package prefectures; message Prefecture { int64 id = 1; // ID string name = 2; // name string romaji = 3; // romaji } message GetPrefecturesResponse { repeated Prefecture prefectures = 1; } define proto file -
syntax = "proto3"; option java_package = "com.your.package.name"; option java_outer_classname =
"Prefectures"; option go_package = "proto"; package prefectures; message Prefecture { int64 id = 1; // ID string name = 2; // name string romaji = 3; // romaji } message GetPrefecturesResponse { repeated Prefecture prefectures = 1; } define proto file -
syntax = "proto3"; option java_package = "com.your.package.name"; option java_outer_classname =
"Prefectures"; option go_package = "proto"; package prefectures; message Prefecture { int64 id = 1; // ID string name = 2; // name string romaji = 3; // romaji } message GetPrefecturesResponse { repeated Prefecture prefectures = 1; } define proto file -
How to use define proto file ɾ ɾ generate compile
/ decompile file ɾ make an API call -
public final class Prefecture extends Message<Prefecture, Prefecture.Builder> { public final
class GetPrefecturesResponse extends Message<GetPrefecturesResponse, GetPrefecturesResponse.Builder> { ɾɾɾ } ɾɾɾ } generate compile / decompile file -
public final class Prefecture extends Message<Prefecture, Prefecture.Builder> { public final
class GetPrefecturesResponse extends Message<GetPrefecturesResponse, GetPrefecturesResponse.Builder> { ɾɾɾ } ɾɾɾ } generate compile / decompile file -
How to use define proto file ɾ ɾ generate compile
/ decompile file ɾ make an API call -
dependencies { // wire for retrofit compile ‘com.squareup.retrofit2:converter-wire:x.x.x' // wire
compile ‘com.squareup.wire:wire-runtime:x.x.x’ // protocol buffers compile 'com.google.protobuf:protobuf-java:x.x.x' } make an API call -
interface Service { @GET("prefectures") fun getProtoPrefectures(): Single<GetPrefecturesResponse> } private val
retrofit: Retrofit = Retrofit .Builder() .baseUrl(endpoint) .client(okHttpClient) .addConverterFactory(WireConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build() make an API call -
interface Service { @GET("prefectures") fun getProtoPrefectures(): Single<GetPrefecturesResponse> } private val
retrofit: Retrofit = Retrofit .Builder() .baseUrl(endpoint) .client(okHttpClient) .addConverterFactory(WireConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build() make an API call -
interface Service { @GET("prefectures") fun getProtoPrefectures(): Single<GetPrefecturesResponse> } private val
retrofit: Retrofit = Retrofit .Builder() .baseUrl(endpoint) .client(okHttpClient) .addConverterFactory(WireConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build() make an API call -
fun getPrefectures(): Single<List<Prefecture>> { return retrofit .create(Service::class.java) .getProtoPrefectures() .map(GetPrefecturesResponse::prefectures) .map
{ val prefectures: ArrayList<Prefecture> = ArrayList() it.mapTo(prefectures) { value -> Prefecture().apply { id = value.id name = value.name romaji = value.romaji } } prefectures.toList() } } make an API call -
fun getPrefectures(): Single<List<Prefecture>> { return retrofit .create(Service::class.java) .getProtoPrefectures() .map(GetPrefecturesResponse::prefectures) .map
{ val prefectures: ArrayList<Prefecture> = ArrayList() it.mapTo(prefectures) { value -> Prefecture().apply { id = value.id name = value.name romaji = value.romaji } } prefectures.toList() } } make an API call -
fun getPrefectures(): Single<List<Prefecture>> { return retrofit .create(Service::class.java) .getProtoPrefectures() .map(GetPrefecturesResponse::prefectures) .map
{ val prefectures: ArrayList<Prefecture> = ArrayList() it.mapTo(prefectures) { value -> Prefecture().apply { id = value.id name = value.name romaji = value.romaji } } prefectures.toList() } } make an API call -
fast because of small data proto file could be an
API document Conclusion pros hard to debug take time to make an api change Cons