Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/pl/smsapi/api/action/user/UserList.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package pl.smsapi.api.action.user;

import org.json.JSONArray;
import pl.smsapi.api.action.AbstractAction;
import pl.smsapi.api.response.UsersResponse;

Expand All @@ -11,7 +12,8 @@ public UserList() {
}

protected UsersResponse createResponse(String data) {
return new UsersResponse(data);
JSONArray jsonArray = new JSONArray(data);
return new UsersResponse(jsonArray.length(), jsonArray);
}

@Override
Expand Down
50 changes: 17 additions & 33 deletions src/main/java/pl/smsapi/api/response/CheckNumberResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,25 @@
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
public class CheckNumberResponse extends ListResponse<NumberResponse> {

public class CheckNumberResponse extends CountableResponse {

private ArrayList<NumberResponse> list = new ArrayList<NumberResponse>();

public CheckNumberResponse(int count, JSONArray array) {

super(count);

if (array != null) {

int arrayLength = array.length();
for (int i = 0; i < arrayLength; i++) {

JSONObject tmp = array.getJSONObject(i);
list.add(
new NumberResponse(
tmp.optString("id"),
tmp.optString("number"),
tmp.optInt("mcc"),
tmp.optInt("mnc"),
tmp.optString("info"),
tmp.optString("status"),
tmp.optInt("date"),
tmp.optInt("ported"),
tmp.optInt("ported_from"),
tmp.optString("price")
)
);
}
}
public CheckNumberResponse(int count, JSONArray jsonArray) {
super(count, jsonArray);
}

public ArrayList<NumberResponse> getList() {
return list;
@Override
protected NumberResponse buildItem(JSONObject jsonObject) {
return new NumberResponse(
jsonObject.optString("id"),
jsonObject.optString("number"),
jsonObject.optInt("mcc"),
jsonObject.optInt("mnc"),
jsonObject.optString("info"),
jsonObject.optString("status"),
jsonObject.optInt("date"),
jsonObject.optInt("ported"),
jsonObject.optInt("ported_from"),
jsonObject.optString("price")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ public class CountableResponse implements Response {
protected int count;

public CountableResponse(int count) {

this.count = count;
}

Expand Down
29 changes: 29 additions & 0 deletions src/main/java/pl/smsapi/api/response/ListResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package pl.smsapi.api.response;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

abstract public class ListResponse<T> extends CountableResponse {

private final ArrayList<T> list = new ArrayList<>();

public ListResponse(int count, JSONArray jsonArray) {
super(count);

if (jsonArray != null) {
final int n = jsonArray.length();
for (int i = 0; i < n; i++) {
JSONObject row = jsonArray.getJSONObject(i);
list.add(buildItem(row));
}
}
}

abstract protected T buildItem(JSONObject jsonObject);

public ArrayList<T> getList() {
return list;
}
}
9 changes: 3 additions & 6 deletions src/main/java/pl/smsapi/api/response/SendStatusResponse.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package pl.smsapi.api.response;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

public class SendStatusResponse extends StatusResponse {

private int parts;
private final int parts;

public SendStatusResponse(int count, int parts, JSONArray list) {
super(count, list);
public SendStatusResponse(int count, int parts, JSONArray jsonArray) {
super(count, jsonArray);
this.parts = parts;
}

Expand Down
29 changes: 13 additions & 16 deletions src/main/java/pl/smsapi/api/response/StatusResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,21 @@
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
public class StatusResponse extends ListResponse<MessageResponse> {

public class StatusResponse extends CountableResponse {

private ArrayList<MessageResponse> list = new ArrayList<MessageResponse>();

public StatusResponse(int count, JSONArray list) {

super(count);

final int n = list.length();
for (int i = 0; i < n; i++) {
JSONObject row = list.getJSONObject(i);
this.list.add(new MessageResponse(row.optString("id"), row.optString("points"), row.optString("number"), row.optString("status"), row.optString("error"), row.optString("idx")));
}
public StatusResponse(int count, JSONArray jsonArray) {
super(count, jsonArray);
}

public ArrayList<MessageResponse> getList() {
return list;
@Override
protected MessageResponse buildItem(JSONObject jsonObject) {
return new MessageResponse(
jsonObject.optString("id"),
jsonObject.optString("points"),
jsonObject.optString("number"),
jsonObject.optString("status"),
jsonObject.optString("error"),
jsonObject.optString("idx")
);
}
}
35 changes: 14 additions & 21 deletions src/main/java/pl/smsapi/api/response/UsersResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,22 @@
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

public class UsersResponse implements Response {

private ArrayList<UserResponse> list = new ArrayList<UserResponse>();

public UsersResponse(String data) {

if (data != null && !data.isEmpty()) {

JSONArray aData = new JSONArray(data);
final int n = aData.length();
if (n > 0) {
for (int i = 0; i < n; i++) {
JSONObject tmp = aData.getJSONObject(i);
list.add(new UserResponse(tmp.optString("username"), tmp.optDouble("limit"), tmp.optDouble("month_limit"), tmp.optInt("senders"), tmp.optInt("phonebook"), tmp.optInt("active"), tmp.optString("info")));
}
}
}
public class UsersResponse extends ListResponse<UserResponse> {

public UsersResponse(int count, JSONArray jsonArray) {
super(count, jsonArray);
}

public ArrayList<UserResponse> getList() {
return list;
@Override
protected UserResponse buildItem(JSONObject jsonObject) {
return new UserResponse(
jsonObject.optString("username"),
jsonObject.optDouble("limit"),
jsonObject.optDouble("month_limit"),
jsonObject.optInt("senders"),
jsonObject.optInt("phonebook"),
jsonObject.optInt("active"),
jsonObject.optString("info")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,31 @@

import org.json.JSONArray;
import org.json.JSONObject;
import pl.smsapi.api.response.CountableResponse;
import pl.smsapi.api.response.ListResponse;

import java.util.ArrayList;
public class ContactsContactListResponse extends ListResponse<ContactsContactResponse> {

public class ContactsContactListResponse extends CountableResponse {
private ArrayList<ContactsContactResponse> list = new ArrayList<ContactsContactResponse>();

public ContactsContactListResponse(int count, JSONArray list) {
super(count);

final int n = list.length();
for (int i = 0; i < n; i++) {
JSONObject row = list.getJSONObject(i);
this.list.add(new ContactsContactResponse(
row.getString("id"),
row.optString("first_name"),
row.optString("last_name"),
row.optString("birthday_date"),
row.optString("phone_number"),
row.optString("email"),
row.optString("gender"),
row.optString("city"),
row.optString("country"),
row.optString("date_created"),
row.optString("date_updated"),
row.optString("description"),
row.optJSONArray("groups"),
row.optString("source")
));
}
public ContactsContactListResponse(int count, JSONArray jsonArray) {
super(count, jsonArray);
}

public ArrayList<ContactsContactResponse> getList() {
return list;
@Override
protected ContactsContactResponse buildItem(JSONObject jsonObject) {
return new ContactsContactResponse(
jsonObject.getString("id"),
jsonObject.optString("first_name"),
jsonObject.optString("last_name"),
jsonObject.optString("birthday_date"),
jsonObject.optString("phone_number"),
jsonObject.optString("email"),
jsonObject.optString("gender"),
jsonObject.optString("city"),
jsonObject.optString("country"),
jsonObject.optString("date_created"),
jsonObject.optString("date_updated"),
jsonObject.optString("description"),
jsonObject.optJSONArray("groups"),
jsonObject.optString("source")
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,25 @@

import org.json.JSONArray;
import org.json.JSONObject;
import pl.smsapi.api.response.CountableResponse;
import pl.smsapi.api.response.ListResponse;

import java.util.ArrayList;
public class ContactsGroupListResponse extends ListResponse<ContactsGroupResponse> {

public class ContactsGroupListResponse extends CountableResponse {
private ArrayList<ContactsGroupResponse> list = new ArrayList<ContactsGroupResponse>();

public ContactsGroupListResponse(int count, JSONArray list) {
super(count);

final int n = list.length();
for (int i = 0; i < n; i++) {
JSONObject row = list.getJSONObject(i);
this.list.add(new ContactsGroupResponse(
row.optString("id"),
row.optString("name"),
row.optString("description"),
row.optString("date_created"),
row.optString("date_updated"),
row.optString("created_by"),
row.optString("idx"),
row.optJSONArray("permissions")
));
}
public ContactsGroupListResponse(int count, JSONArray jsonArray) {
super(count, jsonArray);
}

public ArrayList<ContactsGroupResponse> getList() {
return list;
@Override
protected ContactsGroupResponse buildItem(JSONObject jsonObject) {
return new ContactsGroupResponse(
jsonObject.optString("id"),
jsonObject.optString("name"),
jsonObject.optString("description"),
jsonObject.optString("date_created"),
jsonObject.optString("date_updated"),
jsonObject.optString("created_by"),
jsonObject.optString("idx"),
jsonObject.optJSONArray("permissions")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,22 @@

import org.json.JSONArray;
import org.json.JSONObject;
import pl.smsapi.api.response.CountableResponse;
import pl.smsapi.api.response.ListResponse;

import java.util.ArrayList;
public class ContactsPermissionListResponse extends ListResponse<ContactsPermissionResponse> {

public class ContactsPermissionListResponse extends CountableResponse {
private ArrayList<ContactsPermissionResponse> list = new ArrayList<ContactsPermissionResponse>();

public ContactsPermissionListResponse(int count, JSONArray list) {
super(count);

final int n = list.length();
for (int i = 0; i < n; i++) {
JSONObject row = list.getJSONObject(i);
this.list.add(new ContactsPermissionResponse(
row.optString("group_id"),
row.optString("username"),
row.optBoolean("write"),
row.optBoolean("read"),
row.optBoolean("send")
));
}
public ContactsPermissionListResponse(int count, JSONArray jsonArray) {
super(count, jsonArray);
}

public ArrayList<ContactsPermissionResponse> getList() {
return list;
@Override
protected ContactsPermissionResponse buildItem(JSONObject jsonObject) {
return new ContactsPermissionResponse(
jsonObject.optString("group_id"),
jsonObject.optString("username"),
jsonObject.optBoolean("write"),
jsonObject.optBoolean("read"),
jsonObject.optBoolean("send")
);
}
}
18 changes: 18 additions & 0 deletions src/test/java/pl/smsapi/test/doubles/ClientStub.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package pl.smsapi.test.doubles;

import pl.smsapi.Client;
import pl.smsapi.api.authenticationStrategy.AuthenticationStrategy;

public class ClientStub implements Client {
@Override
public AuthenticationStrategy getAuthenticationStrategy() {
return new AuthenticationStrategyStub();
}

private static class AuthenticationStrategyStub implements AuthenticationStrategy {
@Override
public String getAuthenticationHeader() {
return null;
}
}
}
Loading