Get Country Change Events

Country change events are events triggered whenever a subscriber changes their current country location. It will be triggered once per SIM card that changes its currently registered network's country.

The event contains both the new (current) country, and the old (previous) country, with both the ISO country code, and a readable country name.

Prerequisites

  1. An OAuth 2.0 client
  2. A client access token

Required Scope

  • subscription.country_change:read

Code Dependencies

<dependency>
  <groupId>com.wgtwo.api.v1.grpc</groupId>
  <artifactId>subscription</artifactId>
  <version>1.10.1</version>
</dependency>

Code

If targeting production, you would need to add authentication to the sample code.


#!/usr/bin/env bash
grpcurl \
  -d '
  {
    "stream_configuration": {
      "regular": {},
      "disable_explicit_ack": {}
    }
  }
  ' \
  sandbox.api.shamrock.wgtwo.com:443 \
  wgtwo.subscription.v1.SubscriptionEventService/StreamCountryChangeEvents

package com.example.countrychange

import com.wgtwo.api.v1.subscription.SubscriptionEventServiceGrpc
import com.wgtwo.api.v1.subscription.SubscriptionEventsProto.AckCountryChangeEventRequest
import com.wgtwo.api.v1.subscription.SubscriptionEventsProto.StreamCountryChangeEventsRequest
import com.wgtwo.api.v1.subscription.SubscriptionEventsProto.StreamCountryChangeEventsResponse
import io.grpc.ManagedChannelBuilder
import io.grpc.StatusRuntimeException
import java.util.concurrent.Executors

private const val MAX_IN_FLIGHT = 10

private val channel = ManagedChannelBuilder.forAddress("sandbox.api.shamrock.wgtwo.com", 443).build()
private val stub = SubscriptionEventServiceGrpc.newBlockingStub(channel)
private val executor = Executors.newFixedThreadPool(MAX_IN_FLIGHT)

fun main() {
    while (!channel.isShutdown) {
        try {
            subscribe()
        } catch (e: StatusRuntimeException) {
            println("Got exception: ${e.status} - Reconnecting in 1 second")
            Thread.sleep(1000)
        }
    }
}

private fun subscribe() {
    println("Starting subscription")
    val request = StreamCountryChangeEventsRequest.newBuilder().apply {
        streamConfigurationBuilder.maxInFlight = MAX_IN_FLIGHT
    }.build()

    stub.streamCountryChangeEvents(request).forEach { response ->
        // Using an executor to handle up to MAX_IN_FLIGHT messages in parallel
        executor.submit {
            handleResponse(response)
            ack(response)
        }
    }
}

private fun handleResponse(response: StreamCountryChangeEventsResponse) {
    println("Got response:
$response")
}

private fun ack(response: StreamCountryChangeEventsResponse) {
    val ackInfo = response.metadata.ackInfo
    val request = AckCountryChangeEventRequest.newBuilder().setAckInfo(ackInfo).build()
    stub.ackCountryChangeEvent(request)
}

Example Result

{
  "countryChangeEvent": {
    "previous": {
      "code": "SE",
      "name": "Sweden"
    },
    "current": {
      "code": "NO",
      "name": "Norway"
    }
  }
}
{
  "countryChangeEvent": {
    "previous": {
      "code": "NO",
      "name": "Norway"
    },
    "current": {
      "code": "SE",
      "name": "Sweden"
    }
  }
}
{
  "countryChangeEvent": {
    "previous": {
      "code": "SE",
      "name": "Sweden"
    },
    "current": {
      "code": "US",
      "name": "US"
    }
  }
}
country_change_event {
  previous {
    code: "US"
    name: "United States"
  }
  current {
    code: "SE"
    name: "Sweden"
  }
}

# Ack success

country_change_event {
  previous {
    code: "SE"
    name: "Sweden"
  }
  current {
    code: "NO"
    name: "Norway"
  }
}

# Ack success

country_change_event {
  previous {
    code: "NO"
    name: "Norway"
  }
  current {
    code: "SE"
    name: "Sweden"
  }
}

# Ack success

Read More