Search in sources :

Example 1 with ParameterValue

use of com.stanfy.enroscar.net.operation.ParameterValue in project enroscar by stanfy.

the class SimplePostConverter method sendRequest.

@Override
public void sendRequest(final URLConnection connection) throws IOException {
    Uri.Builder builder = Uri.parse("http://any.com").buildUpon();
    for (final Parameter p : getRequestDescription().getSimpleParameters().getChildren()) {
        if (p instanceof ParameterValue) {
            builder.appendQueryParameter(p.getName(), ((ParameterValue) p).getValue());
        }
    }
    @SuppressWarnings("ConstantConditions") String query = builder.build().getEncodedQuery();
    if (query == null) {
        query = "";
    }
    final byte[] content = query.getBytes(getRequestDescription().getEncoding().name());
    final OutputStream stream = connection.getOutputStream();
    stream.write(content);
    stream.flush();
    if (Utils.isDebugRest(getContext())) {
        Log.d(TAG, "(" + getRequestDescription().getId() + ")" + ": " + query);
    }
}
Also used : ParameterValue(com.stanfy.enroscar.net.operation.ParameterValue) OutputStream(java.io.OutputStream) Parameter(com.stanfy.enroscar.net.operation.Parameter) Uri(android.net.Uri)

Example 2 with ParameterValue

use of com.stanfy.enroscar.net.operation.ParameterValue in project enroscar by stanfy.

the class UploadPostConverter method composeParts.

/**
   * @param context application context
   * @param requestDescription request description
   * @return upload request parts obtained from the request description
   * @throws IOException if an I/O error happens
   */
protected static Part[] composeParts(final Context context, final RequestDescription requestDescription) throws IOException {
    final ArrayList<BinaryData<?>> binaryData = requestDescription.getBinaryData();
    final List<Parameter> params = requestDescription.getSimpleParameters().getChildren();
    int realCount = 0;
    final int binaryCount = binaryData != null ? binaryData.size() : 0;
    Part[] parts = new Part[params.size() + binaryCount];
    for (final Parameter p : params) {
        if (p instanceof ParameterValue) {
            final ParameterValue pv = (ParameterValue) p;
            if (pv.getValue() == null) {
                continue;
            }
            parts[realCount++] = new StringPart(pv.getName(), pv.getValue(), requestDescription.getEncoding().name());
        }
    }
    for (int i = 0; i < binaryCount; i++) {
        final Part part = binaryData.get(i).createHttpPart(context);
        if (part != null) {
            parts[realCount++] = part;
        }
    }
    if (realCount < parts.length) {
        final Part[] trim = new Part[realCount];
        System.arraycopy(parts, 0, trim, 0, realCount);
        parts = trim;
    }
    return parts;
}
Also used : ParameterValue(com.stanfy.enroscar.net.operation.ParameterValue) Part(com.stanfy.enroscar.rest.request.net.multipart.Part) StringPart(com.stanfy.enroscar.rest.request.net.multipart.StringPart) StringPart(com.stanfy.enroscar.rest.request.net.multipart.StringPart) Parameter(com.stanfy.enroscar.net.operation.Parameter) BinaryData(com.stanfy.enroscar.rest.request.binary.BinaryData)

Example 3 with ParameterValue

use of com.stanfy.enroscar.net.operation.ParameterValue in project enroscar by stanfy.

the class BaseRequestBuilderTest method testAddSimpleParameters.

@Test
public void testAddSimpleParameters() {
    final MyBuilder b = new MyBuilder();
    final int count = 5;
    b.addSimpleParameter("boolean1", true);
    b.addSimpleParameter("boolean2", false);
    b.addSimpleParameter("string", "abc");
    b.addSimpleParameter("int", 1);
    b.addSimpleParameter("long", 2L);
    final RequestDescription d = b.getResult();
    assertThat(d.getSimpleParameters().getChildren().size()).isEqualTo(count);
    final ArrayList<String> names = new ArrayList<String>(), values = new ArrayList<String>();
    for (final Parameter p : d.getSimpleParameters().getChildren()) {
        final ParameterValue pv = (ParameterValue) p;
        names.add(pv.getName());
        values.add(pv.getValue());
    }
    assertThat(names).isEqualTo(Arrays.asList("boolean1", "boolean2", "string", "int", "long"));
    assertThat(values).isEqualTo(Arrays.asList("1", "0", "abc", "1", "2"));
}
Also used : ParameterValue(com.stanfy.enroscar.net.operation.ParameterValue) ArrayList(java.util.ArrayList) RequestDescription(com.stanfy.enroscar.net.operation.RequestDescription) Parameter(com.stanfy.enroscar.net.operation.Parameter) AbstractNetTest(com.stanfy.enroscar.test.AbstractNetTest) Test(org.junit.Test)

Example 4 with ParameterValue

use of com.stanfy.enroscar.net.operation.ParameterValue in project enroscar by stanfy.

the class BaseRequestBuilderTest method testRemoveParameter.

@Test
public void testRemoveParameter() {
    final MyBuilder b = new MyBuilder();
    b.addSimpleParameter("p1", "v1");
    b.addSimpleParameter("p2", "v2");
    assertThat(b.getResult().getSimpleParameters().getChildren().size()).isEqualTo(2);
    final Parameter removed = b.removeParameter("p1");
    assertThat(removed).isNotNull();
    assertThat(removed.getName()).isEqualTo("p1");
    assertThat(((ParameterValue) removed).getValue()).isEqualTo("v1");
    assertThat(b.getResult().getSimpleParameters().getChildren().size()).isEqualTo(1);
    assertThat(b.getResult().getSimpleParameters().getChildren().get(0).getName()).isEqualTo("p2");
}
Also used : ParameterValue(com.stanfy.enroscar.net.operation.ParameterValue) Parameter(com.stanfy.enroscar.net.operation.Parameter) AbstractNetTest(com.stanfy.enroscar.test.AbstractNetTest) Test(org.junit.Test)

Aggregations

Parameter (com.stanfy.enroscar.net.operation.Parameter)4 ParameterValue (com.stanfy.enroscar.net.operation.ParameterValue)4 AbstractNetTest (com.stanfy.enroscar.test.AbstractNetTest)2 Test (org.junit.Test)2 Uri (android.net.Uri)1 RequestDescription (com.stanfy.enroscar.net.operation.RequestDescription)1 BinaryData (com.stanfy.enroscar.rest.request.binary.BinaryData)1 Part (com.stanfy.enroscar.rest.request.net.multipart.Part)1 StringPart (com.stanfy.enroscar.rest.request.net.multipart.StringPart)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1