Obter registros de forma síncrona por intervalo de datas

Este trecho de código mostra como recuperar um conjunto de registros usando um intervalo de datas. Trata-se de uma chamada síncrona e, portanto, ficará bloqueada até que retorne uma resposta. Ela é usada quando se deseja retornar um número limitado de registros (milhares) imediatamente. Se você quiser obter um grande número de registros (milhões), use Criar relatório.

Os intervalos de datas estão limitados a uma janela de 24 horas para consultas síncronas.

Exemplo

ChaveDescrição
VONAGE_API_KEY

Your Vonage API key (see it on your dashboard).

VONAGE_API_SECRET

Your Vonage API secret (also available on your dashboard).

ACCOUNT_ID

The account ID (same as VONAGE_API_KEY) for the account you want to generate reports, or retrieve records for.

REPORT_DIRECTION

Either inbound or outbound

REPORT_PRODUCT

Specifies the product for which reports and records are obtained. Can be one of SMS, VOICE-CALL, WEBSOCKET-CALL, VERIFY-API, NUMBER-INSIGHT, MESSAGES, CONVERSATIONS, or ASR.

DATE_START

Date of time window from when you want to start gathering records in ISO-8601 format.

DATE_END

Date of time window from when you want to stop gathering records in ISO-8601 format.

Escreva o código

Adicione o seguinte ao arquivo ` load-records-sync-dates.sh`:

curl -G --data-urlencode date_start=$DATE_START --data-urlencode date_end=$DATE_END -u "$VONAGE_API_KEY:$VONAGE_API_SECRET" \
     "https://api.nexmo.com/v2/reports/records?account_id=$ACCOUNT_ID&product=$REPORT_PRODUCT&direction=$REPORT_DIRECTION"

Ver código-fonte completo

Execute seu código

Salve este arquivo no seu computador e execute-o:

bash load-records-sync-dates.sh

Pré-requisitos

Install-Package Vonage

Crie um arquivo chamado ` LoadsRecordsByDate.cs ` e insira o seguinte código:

var credentials = Credentials.FromApiKeyAndSecret(VONAGE_API_KEY, VONAGE_API_SECRET);
Client = new VonageClient(credentials);

Ver código-fonte completo

Escreva o código

Adicione o seguinte ao arquivo ` LoadsRecordsByDate.cs`:

var request = LoadRecordsRequest.Build()
    .WithProduct(product)
    .WithAccountId(accountId)
    .WithDirection(direction)
    .WithDateStart(dateStart)
    .WithDateEnd(dateEnd)
    .Create();
var response = await Client.ReportsClient.LoadRecordsAsync(request);

Ver código-fonte completo

Experimente

  1. Defina as variáveis substituíveis para sua Account.

  2. Para este exemplo, defina REPORT_PRODUCT para SMS.

  3. Usando a tabela como orientação, defina os valores para as variáveis restantes.

  4. Execute o script e você receberá uma resposta semelhante à seguinte:

{
  "_links": {
    "self": {
      "href": "https://api.nexmo.com/v2/reports/records?account_id=abcd1234&product=SMS&direction=outbound&date_start=2020-06-04T08%3A00%3A00Z&date_end=2020-06-04T14%3A00%3A00Z&status=delivered"
    }
  },
  "request_id": "450b434a-ff4f-40f1-80a5-8d6e24d91234",
  "request_status": "SUCCESS",
  "received_at": "2020-06-04T12:22:25+0000",
  "price": 0.0,
  "currency": "EUR",
  "direction": "outbound",
  "product": "SMS",
  "account_id": "abcd1234",
  "date_start": "2020-06-04T08:00:00+0000",
  "date_end": "2020-06-04T14:00:00+0000",
  "include_subaccounts": false,
  "status": "delivered",
  "include_message": false,
  "items_count": 2,
  "records": [
    {
      "account_id": "abcd1234",
      "message_id": "12000000E506AC66",
      "client_ref": null,
      "direction": "outbound",
      "from": "Acme Inc.",
      "to": "447700123456",
      "network": "23410",
      "network_name": "Telefonica UK Limited",
      "country": "GB",
      "country_name": "United Kingdom",
      "date_received": "2020-06-04T12:21:35+0000",
      "date_finalized": "2020-06-04T12:21:37+0000",
      "latency": "2274",
      "status": "delivered",
      "error_code": "0",
      "error_code_description": "Delivered",
      "currency": "EUR",
      "total_price": "0.03330000"
    },
    ...
  ]
}

Veja também