첨부 파일 및 첨부 파일 메타데이터 검색
NowAttachmentService API를 사용하면 첨부 파일에 대해 CRUD 작업을 수행하고 인스턴스에서 첨부 파일 메타데이터를 검색할 수 있습니다ServiceNow.
주:
NowAttachment API는 기본 ServiceNow 시스템의 게스트 사용자에 대해 작동합니다.
이 API를 사용하여 다음을 수행할 수 있습니다.
- 첨부 파일을 ServiceNow 인스턴스에 업로드하고 특정 기록에 연결합니다.
- 하나 이상의 첨부 파일을 다운로드합니다.
- 첨부 파일을 삭제합니다.
- 첨부 파일의 계산된 해시를 예상 해시와 비교하여 첨부 파일의 유효성을 확인합니다.
- 첨부 파일 메타데이터를 다운로드합니다. 이 메타데이터는 첨부 파일이 업로드될 때 인스턴스에서 생성됩니다 ServiceNow .
첨부 파일 작업에 대한 자세한 내용은 첨부 파일 API를 참조하십시오.
다음은 NowAttachmentService를 인스턴스화하는 방법을 보여 줍니다.
/**
* Helper class used to handle different Now service instances.
* It has an application scope or is a Singleton.
*/
@Singleton
class SdkManager @Inject constructor() {
private var nowAttachmentService: NowAttachmentService? = null
/**
* Create the NowAttachmentService 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.
* NowAttachmentService should be created after initializing the NowSDK
*/
suspend fun getNowAttachmentService(): NowAttachmentService? {
if (nowAttachmentService != null) return nowAttachmentService
return NowDataSDK.makeAttachmentService(URL("https://instance-name.service-now.com"))
.getOrThrow()
.also { this.nowAttachmentService = it }
}
}다음은 특정 첨부 파일에 대한 메타데이터를 가져오는 Call 개체를 가져오는 방법을 보여 줍니다. 지정된 요청을 수행하고 응답<NowAttachmentMetadata>을 반환할 수 있는 실행 가능한 객체를 작성합니다.
suspend fun fetchMetadata(sysId: String) {
val response = runCatching {
sdkManager.getNowAttachmentService()?.attachmentMetadata(sysId)?.execute()
}
if (response.isSuccess) {
val nowAttachmentMetadata = response.getOrNull()?.body
} else {
Log.e("NowSDK", "result not successful")
}
}다음은 여러 첨부 파일에 대한 메타데이터를 가져오는 Call 개체를 가져오는 방법을 보여 줍니다. 지정된 요청을 수행하고 Response< List<NowAttachmentMetadata>>를 반환할 수 있는 실행 가능한 개체를 만듭니다.
suspend fun fetchMultipleMetadata() {
val filterQuery = "content_type=text/plain"
val limit = 5
val response = runCatching {
sdkManager.getNowAttachmentService()?.attachmentMetadata(Filter(filterQuery), limit)?.execute()
}
if (response.isSuccess) {
val attachmentMetadataList = response.getOrNull()?.body
} else {
Log.e("NowSDK", "result not successful")
}
}첨부 파일 메타데이터 페이지 매김
NowAttachmentService 메서드를 사용하여 하나 이상의 첨부 파일에 대한 첨부 파일 메타데이터를 다운로드할 수 있습니다. 여러 첨부 파일에서 메타데이터를 다운로드할 때 반환되는 많은 양의 데이터를 쉽게 반복할 수 있는 Paginator 개체를 반환하는 메서드를 사용할 NowAttachmentService - attachmentMetadataPaginator(filter: Filter? = null, limit: Int? = null) 수 있습니다.
그런 다음 Paginator 메서드를 사용하여 반환된 메타데이터를 반복하고 observe() 콜백에서 응답을 수신할 수 있습니다.
suspend fun createAttachmentMetadataPaginator() {
val filterQuery: String = "content_type=text/plain"
val filter = filterQuery.let(::Filter)
val limit = 10
val paginator = sdkmanager.getNowAttachmentService()?.attachmentMetadataPaginator(filter, limit)
?.observe(object : PaginatorCallBack<NowAttachmentMetadata> {
override fun onSuccess(response: Response<List<NowAttachmentMetadata>>) {
// Handle response
}
override fun onFailure(e: NowDataError) {
// Handle error
}
})
?: throw Exception("Response is null")
// Use paginator operators to navigate. Example
while (paginator.hasNext() && !paginator.isBusy()) {
paginator.next()
}
}반환된 Paginator 개체는 반환된 레코드를 페이징할 수 있는 다음과 같은 메서드를 제공합니다. 먼저 observe() 함수를 호출하여 Paginator의 콜백을 설정해야 합니다.
- 첫 번째()
- hasNext()
- hasPrevious
- isBusy
- 마지막()
- 다음()
- 관찰하다()
- 이전()
- 재설정()
주:
일부 Paginator 메서드는 가져올 페이지가 더 이상 없는 경우와 같이 예외를 throw할 수 있습니다.