인스턴스에서 공용 REST API와 ServiceNow 상호 작용
Mobile SDK 애플리케이션이 인스턴스에서 공용 REST API ServiceNow 를 호출할 수 있도록 하는 Android 기능을 제공합니다.
NowAPIService API를 사용하면 기본 ServiceNow 공용 REST API와 상호작용하거나 인스턴스 내에 사용자 지정 REST API를 작성하고 애플리케이션에서 호출할 Android 수 있습니다.
REST API를 ServiceNow 호출하기 전에makeNowAPIService() 메서드를 호출하여 서비스의 인스턴스를 만들어야 합니다. 서비스 인스턴스가 콜백에 반환되고, 성공하지 않으면 오류가 throw됩니다.
다음은 NowAPIService 개체를 초기화하는 방법을 보여 줍니다.
/**
* Helper class used to handle different Now service instances. It has an application scope or is Singleton
*/
@Singleton
class SdkManager @Inject constructor() {
private var nowApiService: NowAPIService? = null
/**
* Create the NowAPIService once in the lifetime of the application inside the Application class or another manager class
* that will be injected into other classes via dagger/hilt.
* NowAPIService should be created after initializing the NowSDK
*/
suspend fun getNowApiService(): NowAPIService? {
if (nowApiService != null) return nowApiService
return NowDataSDK.makeAPIService(URL("https://instance-name.service-now.com")).getOrThrow()
.also { this.nowApiService = it }
}
}NowAPIService 객체가 초기화되면 NowAPIService data() 메서드를 사용하여 호출할 REST 엔드포인트 및 모든 관련 매개변수를 지정합니다.
suspend fun makeNowApiCall() {
val apiService = sdkManager.getNowApiService()
val apiPath = "api/now/table/sn_customerservice_case"
val endpoint = NowAPIService.Endpoint(HttpMethod.GET, apiPath, true)
val fieldNames = "sys_id,number,short_description,number,priority,state," +
"opened_at,account.name,account.number,contact.name,contact.email," +
"contact_type,assignment_group.name,assigned_to.name"
val queryParamsMap = mapOf("sysparm_fields" to fieldNames, "sysparm_limit" to "10")
val queryParams = QueryParams.Builder().addAll(queryParamsMap).build()
val response = runCatching {
apiService?.data(endpoint = endpoint, queryParams = queryParams)?.execute()
}
if (response.isSuccess) {
val resultString = response.getOrNull()?.body?.let { String(it) }
} else {
// Handle error
}
}