Skip to content

Commit

Permalink
update bvw
Browse files Browse the repository at this point in the history
  • Loading branch information
friendshipbridge committed Sep 3, 2020
1 parent cb6db6f commit b593856
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 83 deletions.
6 changes: 0 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,6 @@
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down
62 changes: 4 additions & 58 deletions src/main/java/com/baidubce/services/bvw/BvwClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
import com.baidubce.util.HttpUtils;
import com.baidubce.util.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URI;
Expand Down Expand Up @@ -883,70 +882,17 @@ public Text2AudioResponse text2Audio(Text2AudioRequest text2AudioRequest) {
return invokeHttpClient(request, Text2AudioResponse.class);
}

/**
* The method to fill the internalRequest's content field with bceRequest.
* Only support HttpMethodName.POST or HttpMethodName.PUT
*
* @param internalRequest A request object, populated with endpoint, resource path, ready for callers to populate
* any additional headers or parameters, and execute.
* @param bceRequest The original request, as created by the user.
*/
private void fillEditPayload(InternalRequest internalRequest, AbstractBceRequest bceRequest) {
if (internalRequest.getHttpMethod() == HttpMethodName.POST
|| internalRequest.getHttpMethod() == HttpMethodName.PUT) {
String strJson = new JSONObject(bceRequest).toString();
byte[] requestJson;
try {
requestJson = strJson.getBytes(DEFAULT_ENCODING);
} catch (UnsupportedEncodingException e) {
throw new BceClientException("Unsupported encode.", e);
}
internalRequest.addHeader(Headers.CONTENT_LENGTH, String.valueOf(requestJson.length));
internalRequest.addHeader(Headers.CONTENT_TYPE, DEFAULT_CONTENT_TYPE);
internalRequest.setContent(RestartableInputStream.wrap(requestJson));
}
}

/**
* Creates and initializes a new request object for the specified bcc resource. This method is responsible
* for determining the right way to address resources.
*
* @param bceRequest The original request, as created by the user.
* @param httpMethod The HTTP method to use when sending the request.
* @param pathVariables The optional variables used in the URI path.
* @return A new request object, populated with endpoint, resource path, ready for callers to populate
* any additional headers or parameters, and execute.
*/
private InternalRequest createEditRequest(AbstractBceRequest bceRequest, HttpMethodName httpMethod,
String... pathVariables) {
List<String> path = new ArrayList<String>();
path.add(VERSION);
if (pathVariables != null) {
for (String pathVariable : pathVariables) {
path.add(pathVariable);
}
}
URI uri = HttpUtils.appendUri(this.getEndpoint(), path.toArray(new String[path.size()]));
InternalRequest request = new InternalRequest(httpMethod, uri);
request.setCredentials(bceRequest.getRequestCredentials());
if (httpMethod == HttpMethodName.POST
|| httpMethod == HttpMethodName.PUT) {
fillEditPayload(request, bceRequest);
}
return request;
}

/**
* create a task of video edit
* The cmd Object in VideoEditCreateRequest
* 1> do not need to modify it by your self, you only put the json string to JSONObject to it(From web FE)
* 2> if you want to construct it by yourself, make object by JSONObject (Like Map)
* 1> do not need to modify it by your self, you only put the json string to Map<String, Object> to it(From web FE)
* 2> if you want to construct it by yourself, make object by Map<String, Object>
*
* @param videoEditCreateRequest
* @return A response of job create result
*/
public VideoEditCreateResponse createVideoEdit(VideoEditCreateRequest videoEditCreateRequest) {
InternalRequest request = this.createEditRequest(videoEditCreateRequest, HttpMethodName.POST, VIDEO_EDIT,
InternalRequest request = this.createRequest(videoEditCreateRequest, HttpMethodName.POST, VIDEO_EDIT,
VIDEO_EDIT_CREATE);
return invokeHttpClient(request, VideoEditCreateResponse.class);
}
Expand All @@ -959,7 +905,7 @@ public VideoEditCreateResponse createVideoEdit(VideoEditCreateRequest videoEditC
*/
public VideoEditPollingResponse pollingVideoEdit(long editId) {
VideoEditPollingRequest videoEditPollingRequest = new VideoEditPollingRequest(editId);
InternalRequest request = this.createEditRequest(videoEditPollingRequest, HttpMethodName.GET,
InternalRequest request = this.createRequest(videoEditPollingRequest, HttpMethodName.GET,
VIDEO_EDIT, VIDEO_EDIT_POLLING, String.format("%d", editId));
return invokeHttpClient(request, VideoEditPollingResponse.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
*/
package com.baidubce.services.bvw.model.videoedit;

import com.baidubce.BceClientException;
import com.baidubce.auth.BceCredentials;
import com.baidubce.model.AbstractBceRequest;
import org.json.JSONObject;

import java.util.Map;

public class VideoEditCreateRequest extends AbstractBceRequest {
private static final long DefaultTaskId = 1;
Expand Down Expand Up @@ -48,32 +50,44 @@ public class VideoEditCreateRequest extends AbstractBceRequest {
/**
* request body
*/
private JSONObject cmd;
private Map<String, Object> cmd;

public String getString(Object object) {
if (object instanceof String) {
return (String) object;
}
throw new BceClientException("bucket/title not a string.");
}

public VideoEditCreateRequest() {
}

public VideoEditCreateRequest(JSONObject jsonObject) {
this.cmd = jsonObject.has(CmdKey) ? jsonObject.getJSONObject(CmdKey) : null;
this.bucket = jsonObject.has(BucketKey) ? jsonObject.getString(BucketKey) : null;
this.title = jsonObject.has(TitleKey) ? jsonObject.getString(TitleKey) : null;
this.taskId = jsonObject.has(TaskIdKey) ? jsonObject.getLong(TaskIdKey) : DefaultTaskId;
public VideoEditCreateRequest(Map<String, Object> jsonObject) {
this.cmd = jsonObject.containsKey(CmdKey) ? (Map<String, Object>) jsonObject.get(CmdKey) : null;
this.bucket = jsonObject.containsKey(BucketKey) ? getString(jsonObject.get(BucketKey)) : null;
this.title = jsonObject.containsKey(TitleKey) ? getString(jsonObject.get(TitleKey)) : null;
this.taskId = jsonObject.containsKey(TaskIdKey) ?
Long.parseLong(String.valueOf(jsonObject.get(TaskIdKey))) : DefaultTaskId;
this.referer = RefererType.baidu.name();
}

public VideoEditCreateRequest(JSONObject jsonObject, RefererType refererType) {
this.cmd = jsonObject.has(CmdKey) ? jsonObject.getJSONObject(CmdKey) : null;
this.bucket = jsonObject.has(BucketKey) ? jsonObject.getString(BucketKey) : null;
this.title = jsonObject.has(TitleKey) ? jsonObject.getString(TitleKey) : null;
this.taskId = jsonObject.has(TaskIdKey) ? jsonObject.getLong(TaskIdKey) : DefaultTaskId;
public VideoEditCreateRequest(
Map<String, Object> jsonObject, RefererType refererType) {
this.cmd = jsonObject.containsKey(CmdKey) ? (Map<String, Object>) jsonObject.get(CmdKey) : null;
this.bucket = jsonObject.containsKey(BucketKey) ? getString(jsonObject.get(BucketKey)) : null;
this.title = jsonObject.containsKey(TitleKey) ? getString(jsonObject.get(TitleKey)) : null;
this.taskId = jsonObject.containsKey(TaskIdKey) ?
Long.parseLong(String.valueOf(jsonObject.get(TaskIdKey))) : DefaultTaskId;
this.referer = refererType.name();
}

public VideoEditCreateRequest(JSONObject jsonObject, RefererType refererType, String notification) {
this.cmd = jsonObject.has(CmdKey) ? jsonObject.getJSONObject(CmdKey) : null;
this.bucket = jsonObject.has(BucketKey) ? jsonObject.getString(BucketKey) : null;
this.title = jsonObject.has(TitleKey) ? jsonObject.getString(TitleKey) : null;
this.taskId = jsonObject.has(TaskIdKey) ? jsonObject.getLong(TaskIdKey) : DefaultTaskId;
public VideoEditCreateRequest(
Map<String, Object> jsonObject, RefererType refererType, String notification) {
this.cmd = jsonObject.containsKey(CmdKey) ? (Map<String, Object>) jsonObject.get(CmdKey) : null;
this.bucket = jsonObject.containsKey(BucketKey) ? getString(jsonObject.get(BucketKey)) : null;
this.title = jsonObject.containsKey(TitleKey) ? getString(jsonObject.get(TitleKey)) : null;
this.taskId = jsonObject.containsKey(TaskIdKey) ?
Long.parseLong(String.valueOf(jsonObject.get(TaskIdKey))) : DefaultTaskId;
this.referer = refererType.name();
this.notification = notification;
}
Expand Down Expand Up @@ -102,7 +116,7 @@ public String getNotification() {
return notification;
}

public JSONObject getCmd() {
public Map<String, Object> getCmd() {
return cmd;
}

Expand All @@ -126,7 +140,7 @@ public void setNotification(String notification) {
this.notification = notification;
}

public void setCmd(JSONObject cmd) {
public void setCmd(Map<String, Object> cmd) {
this.cmd = cmd;
}

Expand Down

0 comments on commit b593856

Please sign in to comment.