Recupere anexos e metadados de anexo
. NowAttachmentService A API permite que você execute operações CRUD em anexos e recupere metadados de anexo do seu ServiceNow instância.
- Carregue anexos para seu ServiceNow e as associou a um registro específico.
- Baixe um ou mais anexos.
- Excluir anexos.
- Valide um anexo comparando o hash calculado do anexo com o hash esperado.
- Baixar metadados do anexo. Esses metadados são gerados pelo seu ServiceNow instância quando um anexo é carregado.
Para obter informações adicionais sobre como trabalhar com anexos, consulte API de anexo .
/**
* 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 }
}
}A seguir, é mostrado como obter um Chamada objeto que busca metadados para um anexo específico. Ele cria um objeto executável que é capaz de executar a solicitação especificada e retornar <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")
}
}A seguir, é mostrado como obter um Chamada objeto que busca metadados para vários anexos. Ele cria um objeto executável que é capaz de executar a solicitação especificada e retornar ListList<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")
}
}Paginação de metadados de anexo
Você pode usar NowAttachmentService métodos para baixar metadados de anexo para um ou mais anexos. Ao baixar metadados de vários anexos, convém usar NowAttachmentService - attachmentMetadataPaginator(filtro: Filtro? Nulo, limite: Int? nulo), que retorna um objeto Paginator que permite iterar facilmente a quantidade potencialmente grande de dados retornados.
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()
}
}- primeiro()
- HasNext()
- TemAnterior
- É Busy
- last()
- next()
- observar()
- anterior()
- redefinir()