Search in sources :

Example 1 with QueryParamName

use of ml.comet.experiment.impl.constants.QueryParamName 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())))));
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) OVERWRITE(ml.comet.experiment.impl.constants.QueryParamName.OVERWRITE) Request(org.asynchttpclient.Request) HashMap(java.util.HashMap) TEXT_PLAIN(io.netty.handler.codec.http.HttpHeaderValues.TEXT_PLAIN) EXPERIMENT_KEY(ml.comet.experiment.impl.constants.QueryParamName.EXPERIMENT_KEY) ApiEndpoints(ml.comet.experiment.impl.constants.ApiEndpoints) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Map(java.util.Map) URI(java.net.URI) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) LINK(ml.comet.experiment.impl.constants.FormParamName.LINK) FormParamName(ml.comet.experiment.impl.constants.FormParamName) ValueSource(org.junit.jupiter.params.provider.ValueSource) FILE(ml.comet.experiment.impl.constants.FormParamName.FILE) QueryParamName(ml.comet.experiment.impl.constants.QueryParamName) POST(org.asynchttpclient.util.HttpConstants.Methods.POST) MULTIPART_FORM_DATA(io.netty.handler.codec.http.HttpHeaderValues.MULTIPART_FORM_DATA) ByteArrayPart(org.asynchttpclient.request.body.multipart.ByteArrayPart) FilePart(org.asynchttpclient.request.body.multipart.FilePart) HtmlRest(ml.comet.experiment.impl.rest.HtmlRest) File(java.io.File) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) APPLICATION_JSON(io.netty.handler.codec.http.HttpHeaderValues.APPLICATION_JSON) JsonUtils(ml.comet.experiment.impl.utils.JsonUtils) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TestUtils(ml.comet.experiment.impl.TestUtils) HttpConstants(org.asynchttpclient.util.HttpConstants) METADATA(ml.comet.experiment.impl.constants.FormParamName.METADATA) StringPart(org.asynchttpclient.request.body.multipart.StringPart) CONTENT_TYPE(io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE) APPLICATION_OCTET_STREAM(io.netty.handler.codec.http.HttpHeaderValues.APPLICATION_OCTET_STREAM) HashMap(java.util.HashMap) Request(org.asynchttpclient.Request) QueryParamName(ml.comet.experiment.impl.constants.QueryParamName) FormParamName(ml.comet.experiment.impl.constants.FormParamName) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with QueryParamName

use of ml.comet.experiment.impl.constants.QueryParamName 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);
}
Also used : ArtifactAsset(ml.comet.experiment.artifact.ArtifactAsset) QueryParamName(ml.comet.experiment.impl.constants.QueryParamName) FormParamName(ml.comet.experiment.impl.constants.FormParamName) LogMessages.getString(ml.comet.experiment.impl.resources.LogMessages.getString) RestApiResponse(ml.comet.experiment.impl.rest.RestApiResponse)

Example 3 with QueryParamName

use of ml.comet.experiment.impl.constants.QueryParamName 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");
}
Also used : HashMap(java.util.HashMap) Request(org.asynchttpclient.Request) StringPart(org.asynchttpclient.request.body.multipart.StringPart) QueryParamName(ml.comet.experiment.impl.constants.QueryParamName) FormParamName(ml.comet.experiment.impl.constants.FormParamName) ByteArrayPart(org.asynchttpclient.request.body.multipart.ByteArrayPart) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with QueryParamName

use of ml.comet.experiment.impl.constants.QueryParamName 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");
}
Also used : HashMap(java.util.HashMap) Request(org.asynchttpclient.Request) StringPart(org.asynchttpclient.request.body.multipart.StringPart) QueryParamName(ml.comet.experiment.impl.constants.QueryParamName) FormParamName(ml.comet.experiment.impl.constants.FormParamName) File(java.io.File) FilePart(org.asynchttpclient.request.body.multipart.FilePart) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with QueryParamName

use of ml.comet.experiment.impl.constants.QueryParamName in project comet-java-sdk by comet-ml.

the class ConnectionUtilsTest method testCreateGetRequest.

@Test
public void testCreateGetRequest() {
    String url = "http://test.com/get";
    HashMap<QueryParamName, String> params = new HashMap<QueryParamName, String>() {

        {
            put(EXPERIMENT_KEY, "someValue");
            put(OVERWRITE, Boolean.toString(true));
        }
    };
    Request r = ConnectionUtils.createGetRequest(url, params);
    this.validateRequest(r, url, params, HttpConstants.Methods.GET, null);
}
Also used : HashMap(java.util.HashMap) Request(org.asynchttpclient.Request) QueryParamName(ml.comet.experiment.impl.constants.QueryParamName) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

QueryParamName (ml.comet.experiment.impl.constants.QueryParamName)6 HashMap (java.util.HashMap)5 FormParamName (ml.comet.experiment.impl.constants.FormParamName)4 Request (org.asynchttpclient.Request)4 Test (org.junit.jupiter.api.Test)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 StringPart (org.asynchttpclient.request.body.multipart.StringPart)3 File (java.io.File)2 ByteArrayPart (org.asynchttpclient.request.body.multipart.ByteArrayPart)2 FilePart (org.asynchttpclient.request.body.multipart.FilePart)2 CONTENT_TYPE (io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE)1 APPLICATION_JSON (io.netty.handler.codec.http.HttpHeaderValues.APPLICATION_JSON)1 APPLICATION_OCTET_STREAM (io.netty.handler.codec.http.HttpHeaderValues.APPLICATION_OCTET_STREAM)1 MULTIPART_FORM_DATA (io.netty.handler.codec.http.HttpHeaderValues.MULTIPART_FORM_DATA)1 TEXT_PLAIN (io.netty.handler.codec.http.HttpHeaderValues.TEXT_PLAIN)1 URI (java.net.URI)1 Map (java.util.Map)1 Objects (java.util.Objects)1 ArtifactAsset (ml.comet.experiment.artifact.ArtifactAsset)1 ExperimentContext (ml.comet.experiment.context.ExperimentContext)1