From 49df5da6ffb708444b53e62b26672213de523136 Mon Sep 17 00:00:00 2001 From: Markus Cozowicz Date: Mon, 16 Oct 2023 16:59:16 +0200 Subject: [PATCH] factor accesstoken runtime-dependency wrapper out --- .../synapse/ml/fabric/TokenLibrary.scala | 27 +++++++++++++++++++ .../logging/fabric/CertifiedEventClient.scala | 24 ++--------------- 2 files changed, 29 insertions(+), 22 deletions(-) create mode 100644 core/src/main/scala/com/microsoft/azure/synapse/ml/fabric/TokenLibrary.scala diff --git a/core/src/main/scala/com/microsoft/azure/synapse/ml/fabric/TokenLibrary.scala b/core/src/main/scala/com/microsoft/azure/synapse/ml/fabric/TokenLibrary.scala new file mode 100644 index 0000000000..740aded489 --- /dev/null +++ b/core/src/main/scala/com/microsoft/azure/synapse/ml/fabric/TokenLibrary.scala @@ -0,0 +1,27 @@ +// Copyright (C) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See LICENSE in project root for information. + +package com.microsoft.azure.synapse.ml.fabric + +import scala.reflect.runtime.currentMirror +import scala.reflect.runtime.universe._ +object TokenLibrary { + def getAccessToken: String = { + val objectName = "com.microsoft.azure.trident.tokenlibrary.TokenLibrary" + val mirror = currentMirror + val module = mirror.staticModule(objectName) + val obj = mirror.reflectModule(module).instance + val objType = mirror.reflect(obj).symbol.toType + val methodName = "getAccessToken" + val methodSymbols = objType.decl(TermName(methodName)).asTerm.alternatives + val argType = typeOf[String] + val selectedMethodSymbol = methodSymbols.find { m => + m.asMethod.paramLists match { + case List(List(param)) => param.typeSignature =:= argType + case _ => false + } + }.getOrElse(throw new NoSuchMethodException(s"Method $methodName with argument type $argType not found")) + val methodMirror = mirror.reflect(obj).reflectMethod(selectedMethodSymbol.asMethod) + methodMirror("pbi").asInstanceOf[String] + } +} diff --git a/core/src/main/scala/com/microsoft/azure/synapse/ml/logging/fabric/CertifiedEventClient.scala b/core/src/main/scala/com/microsoft/azure/synapse/ml/logging/fabric/CertifiedEventClient.scala index 8c832301f9..d7ec734651 100644 --- a/core/src/main/scala/com/microsoft/azure/synapse/ml/logging/fabric/CertifiedEventClient.scala +++ b/core/src/main/scala/com/microsoft/azure/synapse/ml/logging/fabric/CertifiedEventClient.scala @@ -3,14 +3,13 @@ package com.microsoft.azure.synapse.ml.logging.fabric +import com.microsoft.azure.synapse.ml.fabric.TokenLibrary import org.apache.spark.sql.SparkSession import spray.json.DefaultJsonProtocol.{StringJsonFormat, _} import spray.json._ import java.time.Instant import java.util.UUID -import scala.reflect.runtime.currentMirror -import scala.reflect.runtime.universe._ import com.microsoft.azure.synapse.ml.logging.common.PlatformDetails.runningOnFabric @@ -33,28 +32,9 @@ object CertifiedEventClient extends RESTUtils { private lazy val CertifiedEventUri = getCertifiedEventUri - private def getAccessToken: String = { - val objectName = "com.microsoft.azure.trident.tokenlibrary.TokenLibrary" - val mirror = currentMirror - val module = mirror.staticModule(objectName) - val obj = mirror.reflectModule(module).instance - val objType = mirror.reflect(obj).symbol.toType - val methodName = "getAccessToken" - val methodSymbols = objType.decl(TermName(methodName)).asTerm.alternatives - val argType = typeOf[String] - val selectedMethodSymbol = methodSymbols.find { m => - m.asMethod.paramLists match { - case List(List(param)) => param.typeSignature =:= argType - case _ => false - } - }.getOrElse(throw new NoSuchMethodException(s"Method $methodName with argument type $argType not found")) - val methodMirror = mirror.reflect(obj).reflectMethod(selectedMethodSymbol.asMethod) - methodMirror("pbi").asInstanceOf[String] - } - private def getHeaders: Map[String, String] = { Map( - "Authorization" -> s"Bearer $getAccessToken", + "Authorization" -> s"Bearer ${TokenLibrary.getAccessToken}", "RequestId" -> UUID.randomUUID().toString, "Content-Type" -> "application/json", "x-ms-workload-resource-moniker" -> UUID.randomUUID().toString