-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add response interface and http,grpc implement
Signed-off-by: naah69 <[email protected]>
- Loading branch information
Showing
7 changed files
with
267 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
sdk/src/main/java/io/dapr/client/domain/response/DaprResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2021 The Dapr Authors | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package io.dapr.client.domain.response; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
/** | ||
* Response. | ||
*/ | ||
public interface DaprResponse<T> { | ||
|
||
/** | ||
* get response code. | ||
* @return response code | ||
*/ | ||
int getCode(); | ||
|
||
/** | ||
* get response data. | ||
* @return response data | ||
*/ | ||
T getData() throws IOException; | ||
|
||
/** | ||
* get response header. | ||
* @return response header | ||
*/ | ||
Map<String,String> getHeaders(); | ||
} |
64 changes: 64 additions & 0 deletions
64
sdk/src/main/java/io/dapr/client/domain/response/GrpcDaprResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2021 The Dapr Authors | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package io.dapr.client.domain.response; | ||
|
||
import io.dapr.serializer.DaprObjectSerializer; | ||
import io.dapr.utils.TypeRef; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
/** | ||
* GrpcDaprResponse. | ||
*/ | ||
public class GrpcDaprResponse<T> implements DaprResponse<T> { | ||
|
||
private final DaprObjectSerializer serializer; | ||
|
||
private final TypeRef<T> type; | ||
|
||
private final byte[] data; | ||
|
||
private final Map<String,String> headers; | ||
|
||
/** | ||
* build grpc dapr response. | ||
* @param data grpc invoke response data | ||
* @param headers grpc invoke headers | ||
* @param serializer objectSerializer | ||
* @param type type | ||
*/ | ||
public GrpcDaprResponse(byte[] data, Map<String,String> headers, DaprObjectSerializer serializer, TypeRef<T> type) { | ||
this.data = data; | ||
this.headers = headers; | ||
this.serializer = serializer; | ||
this.type = type; | ||
} | ||
|
||
@Override | ||
public int getCode() { | ||
// InvokeResponse didn't have it. | ||
return 200; | ||
} | ||
|
||
@Override | ||
public T getData() throws IOException { | ||
return serializer.deserialize(data, type); | ||
} | ||
|
||
@Override | ||
public Map<String, String> getHeaders() { | ||
return this.headers; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
sdk/src/main/java/io/dapr/client/domain/response/HttpDaprResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2021 The Dapr Authors | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package io.dapr.client.domain.response; | ||
|
||
import io.dapr.client.DaprHttp; | ||
import io.dapr.serializer.DaprObjectSerializer; | ||
import io.dapr.utils.TypeRef; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
/** | ||
* HttpDaprResponse. | ||
*/ | ||
public class HttpDaprResponse<T> implements DaprResponse<T> { | ||
|
||
private final DaprHttp.Response response; | ||
|
||
private final DaprObjectSerializer serializer; | ||
|
||
private final TypeRef<T> type; | ||
|
||
/** | ||
* build http dapr response. | ||
* @param response http invoke response | ||
* @param serializer serializer | ||
* @param type type | ||
*/ | ||
public HttpDaprResponse(DaprHttp.Response response, DaprObjectSerializer serializer, TypeRef<T> type) { | ||
this.response = response; | ||
this.serializer = serializer; | ||
this.type = type; | ||
} | ||
|
||
@Override | ||
public int getCode() { | ||
return response.getStatusCode(); | ||
} | ||
|
||
@Override | ||
public T getData() throws IOException { | ||
byte[] data = response.getBody(); | ||
if (type.getType() == String.class) { | ||
return (T) new String(data); | ||
} | ||
return serializer.deserialize(data, type); | ||
} | ||
|
||
@Override | ||
public Map<String, String> getHeaders() { | ||
return response.getHeaders(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters