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);
}
}
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;
}
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"));
}
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");
}
Aggregations