use of ml.comet.experiment.impl.constants.FormParamName in project comet-java-sdk by comet-ml.
the class ConnectionUtilsTest method testCreatePostFormRequest.
@Test
public void testCreatePostFormRequest() {
// Create test data
//
String url = "http://test.com" + ApiEndpoints.ADD_ASSET;
Map<QueryParamName, String> params = new HashMap<QueryParamName, String>() {
{
put(EXPERIMENT_KEY, "someValue");
put(OVERWRITE, Boolean.toString(true));
}
};
Map<FormParamName, Object> formParams = new HashMap<FormParamName, Object>() {
{
put(METADATA, "some string");
put(LINK, "https://some.site.com");
}
};
// Create request
//
Request r = ConnectionUtils.createPostFormRequest(url, params, formParams);
this.validateRequest(r, url, params, POST, MULTIPART_FORM_DATA.toString());
// Check body parts
//
assertEquals(2, r.getBodyParts().size(), "wrong number of body parts");
assertTrue(r.getBodyParts().stream().map(part -> (StringPart) part).allMatch(stringPart -> Objects.equals(stringPart.getValue(), formParams.get(FormParamName.valueOf(stringPart.getName().toUpperCase())))));
}
use of ml.comet.experiment.impl.constants.FormParamName in project comet-java-sdk by comet-ml.
the class RestApiClient method logAsset.
<T extends Asset> Single<RestApiResponse> logAsset(final T asset, String experimentKey) {
Map<QueryParamName, String> queryParams = RestApiUtils.assetQueryParameters((AssetImpl) asset, experimentKey);
Map<FormParamName, Object> formParams = RestApiUtils.assetFormParameters(asset);
if (asset instanceof ArtifactAsset) {
queryParams.put(ARTIFACT_VERSION_ID, ((ArtifactAssetImpl) asset).getArtifactVersionId());
}
// call appropriate send method
if (asset.getFile().isPresent()) {
return singleFromAsyncPost(asset.getFile().get(), ADD_ASSET, queryParams, formParams, RestApiResponse.class);
} else if (asset.getFileLikeData().isPresent()) {
return singleFromAsyncPost(asset.getFileLikeData().get(), ADD_ASSET, queryParams, formParams, RestApiResponse.class);
}
// no data response
RestApiResponse response = new RestApiResponse();
response.setMsg("asset has no data");
response.setCode(-1);
return Single.just(response);
}
use of ml.comet.experiment.impl.constants.FormParamName in project comet-java-sdk by comet-ml.
the class ConnectionUtilsTest method testCreatePostByteArrayRequest.
@Test
public void testCreatePostByteArrayRequest() {
// Create test data
//
String url = "http://test.com" + ApiEndpoints.ADD_ASSET;
Map<QueryParamName, String> params = new HashMap<QueryParamName, String>() {
{
put(EXPERIMENT_KEY, "someValue");
put(OVERWRITE, Boolean.toString(true));
}
};
Map<FormParamName, Object> formParams = new HashMap<FormParamName, Object>() {
{
put(METADATA, "some string");
}
};
byte[] data = "The test byte data".getBytes();
// Create request with metadata
//
Request r = ConnectionUtils.createPostByteArrayRequest(data, url, params, formParams);
this.validateRequest(r, url, params, POST, MULTIPART_FORM_DATA.toString());
// Check body parts
//
assertEquals(2, r.getBodyParts().size(), "wrong number of body parts");
// metadata part
StringPart stringPart = (StringPart) r.getBodyParts().get(0);
assertEquals(METADATA.paramName(), stringPart.getName(), "wrong name");
assertEquals(formParams.get(METADATA), stringPart.getValue(), "wrong value");
// data part
ByteArrayPart part = (ByteArrayPart) r.getBodyParts().get(1);
assertEquals(FILE.paramName(), part.getName(), "wrong name");
assertEquals(APPLICATION_OCTET_STREAM.toString(), part.getContentType(), "wrong content type");
assertEquals(data, part.getBytes(), "wrong data array");
// Create request without metadata
//
r = ConnectionUtils.createPostByteArrayRequest(data, url, params, null);
this.validateRequest(r, url, params, POST, MULTIPART_FORM_DATA.toString());
// Check body parts
//
assertEquals(1, r.getBodyParts().size(), "wrong number of body parts");
// data part
part = (ByteArrayPart) r.getBodyParts().get(0);
assertEquals(FILE.paramName(), part.getName(), "wrong name");
assertEquals(APPLICATION_OCTET_STREAM.toString(), part.getContentType(), "wrong content type");
assertEquals(data, part.getBytes(), "wrong data array");
}
use of ml.comet.experiment.impl.constants.FormParamName in project comet-java-sdk by comet-ml.
the class ConnectionUtilsTest method testCreatePostFileRequest.
@Test
public void testCreatePostFileRequest() {
// Create test data
//
String url = "http://test.com" + ApiEndpoints.ADD_ASSET;
Map<QueryParamName, String> queryParams = new HashMap<QueryParamName, String>() {
{
put(EXPERIMENT_KEY, "someValue");
put(OVERWRITE, Boolean.toString(true));
}
};
Map<FormParamName, Object> formParams = new HashMap<FormParamName, Object>() {
{
put(METADATA, "some string");
}
};
File file = TestUtils.getFile(SOME_TEXT_FILE_NAME);
assertNotNull(file, "test file not found");
// Create request with metadata
//
Request r = ConnectionUtils.createPostFileRequest(file, url, queryParams, formParams);
this.validateRequest(r, url, queryParams, POST, MULTIPART_FORM_DATA.toString());
// Check body parts
//
assertEquals(2, r.getBodyParts().size(), "wrong number of body parts");
// metadata part
StringPart stringPart = (StringPart) r.getBodyParts().get(0);
assertEquals(METADATA.paramName(), stringPart.getName(), "wrong name");
assertEquals(formParams.get(METADATA), stringPart.getValue(), "wrong value");
// file part
FilePart filePart = (FilePart) r.getBodyParts().get(1);
assertEquals(FILE.paramName(), filePart.getName(), "wrong name");
assertEquals(TEXT_PLAIN.toString(), filePart.getContentType(), "wrong content type");
assertEquals(file, filePart.getFile(), "wrong file");
// Create request without metadata
//
r = ConnectionUtils.createPostFileRequest(file, url, queryParams, null);
this.validateRequest(r, url, queryParams, POST, MULTIPART_FORM_DATA.toString());
// Check body parts
//
assertEquals(1, r.getBodyParts().size(), "wrong number of body parts");
// file part
filePart = (FilePart) r.getBodyParts().get(0);
assertEquals(FILE.paramName(), filePart.getName(), "wrong name");
assertEquals(TEXT_PLAIN.toString(), filePart.getContentType(), "wrong content type");
assertEquals(file, filePart.getFile(), "wrong file");
}
Aggregations