Récupérer les pièces jointes et les métadonnées des pièces jointes
L’API NowAttachmentService vous permet d’effectuer des opérations CRUD sur les pièces jointes et de récupérer les métadonnées de pièce jointe à partir de votre ServiceNow instance.
- Téléchargez des pièces jointes sur votre ServiceNow instance et associez-les à un enregistrement spécifique.
- Téléchargez une ou plusieurs pièces jointes.
- Supprimer les pièces jointes.
- Validez une pièce jointe en comparant le hachage calculé de la pièce jointe au hachage attendu.
- Téléchargez les métadonnées de la pièce jointe. Ces métadonnées sont générées par votre ServiceNow instance lorsqu’une pièce jointe est chargée.
Pour plus d’informations sur l’utilisation des pièces jointes, consultez API des pièces jointes.
/**
* 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 }
}
}Ce qui suit montre comment obtenir un objet d’appel qui extrait les métadonnées pour des pièces jointes particulières. Cela crée un objet exécutable capable d’effectuer la demande spécifiée et de renvoyer Response<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")
}
}Ce qui suit montre comment obtenir un objet d’appel qui récupère les métadonnées pour plusieurs pièces jointes. Cela crée un objet exécutable capable d’effectuer la demande spécifiée et de renvoyer la réponse < 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")
}
}Pagination des métadonnées de pièce jointe
Vous pouvez utiliser les méthodes NowAttachmentService pour télécharger les métadonnées de pièce jointe pour une ou plusieurs pièces jointes. Lors du téléchargement de métadonnées à partir de plusieurs pièces jointes, vous pouvez utiliser la NowAttachmentService : attachmentMetadataPaginator(filter : Filter ? = null, limit : Int ? = null) méthode, qui renvoie un objet Paginator qui vous permet d’itérer facilement sur la quantité potentiellement importante de données renvoyées.
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()
}
}- premier()
- hasNext()
- hasPrevious
- isBusy
- last()
- suivant()
- observe()
- précédent()
- réinitialiser()