mailjet/mailjet-apiv3-java

Filtering does not work when retrieving the list of smses

mvanveen82 opened this issue · 2 comments

I'm trying to get the smslist of the smses send in the last 24 hours, so I can update the status of them in my sms-log. However the api returns the last 21 smses not filtering on timestamp.

Here is my code:

        OkHttpClient customHttpClient = new OkHttpClient.Builder()
            .connectTimeout(30, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .writeTimeout(30, TimeUnit.SECONDS)
            .build();

        ClientOptions options = ClientOptions.builder()
            .bearerAccessToken(mailjetSmsKey)
            .okHttpClient(customHttpClient)
            .build();

        this.client = new MailjetClient(options);

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime earlier = now.minusHours(24);
        Timestamp nowStamp = Timestamp.valueOf(now);
        Timestamp earlierStamp = Timestamp.valueOf(earlier);
        Long nowSeconds = nowStamp.getTime() / 1000;
        Long earlierSeconds = earlierStamp.getTime() / 1000;

        this.logger.info("from:" + earlierSeconds);
        this.logger.info("until:" + nowSeconds);

        MailjetRequest request = new MailjetRequest(Sms.resource)
            .property(Sms.FROMTS, earlierSeconds)
            .property(Sms.TOTS, nowSeconds);

        try {
            MailjetResponse response = this.client.get(request);
            this.logger.info("Response: " + response.getData().toString());

            this._parseResponse(response.getData());
        } catch (MailjetException e) {
            this.logger.error(e);
        }

Debug info:

from:1626010800
until:1626097200
request:{"Action":"","Filters":"{}","Action ID":0,"Resource":"sms","Body":"{\"FromTS\":1626010800,\"ToTS\":1626097200}"}
respone: [
  {
    "Status": {
      "Description": "Message rejected cause phone number is not correct",
      "Code": 14,
      "Name": "rejected_invalid_destination"
    },
    "ID": "15566f1b-b607-42bb-9c22-29ef6c0b8069",
    "From": "Bereslim",
    "To": "+316xxxxxxxx",
    "SmsCount": 1,
    "Cost": {
      "Currency": "EUR",
      "Value": 0.07602
    },
    "CreationTS": 1626089941
  },
  {
    "Status": {
      "Description": "Message delivered",
      "Code": 3,
      "Name": "delivered"
    },
    "ID": "75c7bf4b-7ec8-4946-ab4a-f608f660bb1b",
    "From": "Bereslim",
    "To": "+316xxxxxxxx",
    "SmsCount": 1,
    "Cost": {
      "Currency": "EUR",
      "Value": 0.07602
    },
    "CreationTS": 1626089941
  },
  {
    "Status": {
      "Description": "Message delivered",
      "Code": 3,
      "Name": "delivered"
    },
    "ID": "542c6467-b7e2-4419-9d6d-37b6dd5ff58b",
    "From": "Bereslim",
    "To": "+316xxxxxxxx",
    "SmsCount": 1,
    "Cost": {
      "Currency": "EUR",
      "Value": 0.07602
    },
    "CreationTS": 1626084781
  },
  {
    "Status": {
      "Description": "Message delivered",
      "Code": 3,
      "Name": "delivered"
    },
    "ID": "15f193f3-8313-40a2-a8cd-465e4965a1c9",
    "From": "Bereslim",
    "To": "+316xxxxxxxx",
    "SmsCount": 1,
    "Cost": {
      "Currency": "EUR",
      "Value": 0.07602
    },
    "CreationTS": 1625844960
  }...

I tried first converting to Long, long, Integer, int or String, but it keeps returning the last 21 items. Also filtering on TO / phone number does not work.

We're using the lastest Mailjet Java api v5.2.0

Hi @mvanveen82!

Thanks for raising this issue.

.property(Sms.FROMTS, earlierSeconds)

This code adds those properties to the request body, which is not used in GET requests.
Filters are passed as query params, so they should be set like this:

        int nowSeconds = (int)(nowStamp.getTime() / 1000);
        int earlierSeconds = (int)(earlierStamp.getTime() / 1000);

        MailjetRequest request = new MailjetRequest(Sms.resource)
                .filter(Sms.FROMTS, earlierSeconds)
                .filter(Sms.TOTS, nowSeconds);

Thnx for the quick response. You added a test great! Would be nice if you also add an example request to the sms api documentation of the java client.