@@ -13,6 +13,14 @@ import com.turo.pushy.apns.auth.ApnsSigningKey;
1313import com .turo .pushy .apns .auth .AuthenticationToken ;
1414import java .time .Instant
1515import java .util .Date
16+ import com .gu .liveactivities .ChannelApiClient .authenticationToken
17+
18+ object ChannelApiClient {
19+
20+ var authenticationToken : Option [String ] = None
21+
22+ var issueDate : Option [Date ] = None
23+ }
1624
1725class ChannelApiClient {
1826
@@ -31,7 +39,15 @@ class ChannelApiClient {
3139 private val message = " {\" message-storage-policy\" : 1, \" push-type\" : \" LiveActivity\" }"
3240
3341 private def getAccessToken (): String = {
34- return " invalid-token-for-testing"
42+ ChannelApiClient .authenticationToken match {
43+ case Some (token) => token
44+ case None => {
45+ val authenticationToken = generateToken()
46+ ChannelApiClient .authenticationToken = Some (authenticationToken)
47+ ChannelApiClient .issueDate = Some (new Date ())
48+ authenticationToken
49+ }
50+ }
3551 }
3652
3753 private def generateToken (): String = {
@@ -46,9 +62,7 @@ class ChannelApiClient {
4662
4763 def createChannel (): Future [String ] = {
4864 println(" Creating channel" )
49- val authToken = generateToken()
50- println(s " Generated auth token: $authToken" )
51-
65+ val authToken = getAccessToken()
5266 val request : HttpRequest = HttpRequest .newBuilder(new URI (url))
5367 .version(HttpClient .Version .HTTP_2 )
5468 .header(" Authorization" , authToken)
@@ -60,11 +74,46 @@ class ChannelApiClient {
6074 httpClient.sendAsync(request, HttpResponse .BodyHandlers .ofString()).whenComplete((response, err) => {
6175 if (response == null ) {
6276 p.failure(err)
77+ } else if (response.statusCode() >= 200 &&
78+ response.statusCode() < 300 &&
79+ response.headers().firstValue(" apns-channel-id" ).isPresent()) {
80+ val channelId = response.headers().firstValue(" apns-channel-id" ).get()
81+ println(s " Channel created successfully with channel ID $channelId" )
82+ p.success(channelId)
6383 } else {
64- println(s " Received response with status code ${response.statusCode()} and body ${response.body()}" )
65- p.success( response.body( ))
84+ println(s " Failed to create channel with status code ${response.statusCode()} and body ${response.body()}" )
85+ p.failure( new Exception ( s " Failed to create channel with status code ${ response.statusCode()} and body ${response.body()} " ))
6686 }
6787 })
6888 p.future
6989 }
90+
91+ def closeChannel (channelId : String ): Future [Unit ] = {
92+ println(s " Closing channel $channelId" )
93+ val authToken = getAccessToken()
94+ val request : HttpRequest = HttpRequest .newBuilder(new URI (url))
95+ .version(HttpClient .Version .HTTP_2 )
96+ .header(" Authorization" , authToken)
97+ .header(" Content-Type" , mediaType)
98+ .header(" apns-channel-id" , channelId)
99+ .DELETE ()
100+ .timeout(Duration .ofSeconds(60 ))
101+ .build()
102+ val p = Promise [Unit ]()
103+ httpClient.sendAsync(request, HttpResponse .BodyHandlers .ofString()).whenComplete((response, err) => {
104+ if (response == null ) {
105+ println(s " Failed to close channel $channelId due to error ${err.getMessage}" )
106+ p.failure(err)
107+ } else if (response.statusCode() >= 200 &&
108+ response.statusCode() < 300 ) {
109+ println(s " Channel closed successfully with channel ID $channelId" )
110+ p.success(())
111+ } else {
112+ println(s " Failed to close channel with status code ${response.statusCode()} and body ${response.body()}" )
113+ p.failure(new Exception (s " Failed to close channel with status code ${response.statusCode()} and body ${response.body()}" ))
114+ }
115+ })
116+ p.future
117+ }
118+
70119}
0 commit comments