API providers reference
API providers reference
API providers allow you to use all the features of the Support SDK without the UI. They let you interact with Zendesk on behalf of an end user.
Before you start
Before you start, the following information is useful to know:
- All API providers assume that you have correctly initialized the SDK, including providing a valid identity.
- You can use the following API providers:
- RequestProvider
- Fetch a list of requests and create new requests
- Used to fetch a list of comments for a given request and to add additional comments
- HelpCenterProvider
- Used to fetch categories, sections, and articles
- Used to search for articles
- Used to vote on articles
- UploadProvider
- Used to upload attachments
- Used to delete attachments
- UserProvider
- Used to add or remove end-user tags
- Used to set user-fields to an end-user
- PushRegistrationProvider
- Used to register and unregister clients for push notifications
- Supports webhook and Urban Airship
- RequestProvider
- API providers return a result asynchronously through a callback provided in a request.
- On success,
onSuccess(T result)
被称为包含类型T对象的结果。On failure,onError(ErrorResponse error)
passes the error that occurred. If a calling context is destroyed before the request is completed, you won't receive a callback.
RequestProvider
Get an instance of the RequestProvider as follows:
RequestProviderprovider=ZendeskConfig.INSTANCE.provider().requestProvider();
Create a request (ticket)
The following example uses the provider'screateRequest()
method to create a request:
CreateRequestrequest=newCreateRequest();
request.setSubject("Ticket subject");
request.setDescription("Ticket description");
request.setTags(Arrays.asList("printer","fire"));
provider.createRequest(request,newZendeskCallback<CreateRequest>(){
@Override
publicvoidonSuccess(CreateRequest createRequest){
// Handle the success
}
@Override
publicvoidonError(ErrorResponse错误Response){
// Handle the error
// Log the error
Logger.e("MyLogTag",errorResponse);
}
});
Create a request (ticket) with an attachment
The following example usescreateRequest()
to create a request with an attachment.
In the example, replace{attachmentToken}with an actual attachment token retrieved fromuploadAttachment ()
in theUploadProvider.
CreateRequestcreateRequest=newCreateRequest();
createRequest.setAttachments(Arrays.asList("{attachmentToken}"));
createRequest.setSubject("Ticket subject");
createRequest.setDescription("Ticket description");
provider.createRequest(createRequest,newZendeskCallback<CreateRequest>(){
@Override
publicvoidonSuccess(CreateRequest result){
// Handle success
}
@Override
publicvoidonError(ErrorResponse错误){
// Handle error
}
});
The RequestProvider has many more methods. SeeRequestProviderin the Javadoc. Using them is very similar to the examples above.
Sending additional data in a request
You can send additional data in a request using metadata or custom fields.
Using metadata
TheCreateRequest
class has avoid setMetadata(Map
method that can be used to add metadata to Requests. Seesetting metadatain the core API documentation for more details about metadata.
Here is an example of how to set some metadata on a request. In the example the keys of the metadata are "time_spent" and "account" and the values are "4m12s" and "integration".
CreateRequestcreateRequest=newCreateRequest();
createRequest.setSubject("Subject");
createRequest.setDescription("Description");
HashMap<String,String>metadata=newHashMap<String,String>(){{
put("time_spent","4m12s");
put("account","integrations");
}};
createRequest.setMetadata(metadata);
RequestProviderrequestProvider=ZendeskConfig.INSTANCE.provider().requestProvider();
requestProvider.createRequest(createRequest,newZendeskCallback<CreateRequest>(){
@Override
publicvoidonSuccess(CreateRequest createRequest){
Logger.i(LOG_TAG,"Request created...");
}
@Override
publicvoidonError(ErrorResponse错误Response){
Logger.e(LOG_TAG,errorResponse);
}
});
You can retrieve metadata using theTicket Audits APIor byside-loadingit with theTickets API. Here's a snippet of the API response from the Audits API showing the metadata inmetadata -> custom -> sdk
{
"audits":[
{
...
"metadata":{
"custom":{
"sdk":{
"account":"integrations",
"time_spent":"4m12s"
}
},
},
...
}
],
...
}
Using custom fields and custom forms
TheCreateRequest
class has avoid setCustomFields(List
method that you can use to add custom fields to a request. Custom fields can be added in the Zendesk Supportadmin interfaceorAPI. Each custom field has an ID that's displayed when an admin views the field in the Zendesk Support admin interface. Ask a Support admin to note down this ID to use it in the Support SDK. Custom fields must be visible and editable for end users to work with the Support SDK.
Here is an example of how to set a custom field:
CreateRequestcreateRequest=newCreateRequest();
createRequest.setSubject("Subject");
createRequest.setDescription("Description");
List<CustomField>customFields=newArrayList<CustomField>(){{
LongcustomFieldId=1234567L;
StringcustomFieldValue="Android 5.0";
CustomFieldandroidVersionCustomField=newCustomField(customFieldId,customFieldValue);
add(androidVersionCustomField);
}};
createRequest.setCustomFields(customFields);
RequestProviderrequestProvider=ZendeskConfig.INSTANCE.provider().requestProvider();
requestProvider.createRequest(createRequest,newZendeskCallback<CreateRequest>(){
@Override
publicvoidonSuccess(CreateRequest createRequest){
Logger.i(LOG_TAG,"Request created...");
}
@Override
publicvoidonError(ErrorResponse错误Response){
Logger.e(LOG_TAG,errorResponse);
}
});
If your Zendesk Support instance supports custom forms, you can specify the form that will be shown with thevoid setTicketFormId(Long ticketFormId)
method in theCreateRequest
class. Here is an example:
CreateRequestcreateRequest=newCreateRequest();
createRequest.setSubject("Subject");
createRequest.setDescription("Description");
createRequest.setTicketFormId(1234567L);
RequestProviderrequestProvider=ZendeskConfig.INSTANCE.provider().requestProvider();
requestProvider.createRequest(createRequest,newZendeskCallback<CreateRequest>(){
@Override
publicvoidonSuccess(CreateRequest createRequest){
Logger.i(LOG_TAG,"Request created...");
}
@Override
publicvoidonError(ErrorResponse错误Response){
Logger.e(LOG_TAG,errorResponse);
}
});
HelpCenterProvider
Using theHelpCenterProvider
is very similar to using theRequestProvider
. You can call the following methods from the list inHelpCenterProviderin the Javadoc.
UploadProvider
Using theUploadProvider
is very similar to using theRequestProvider
. You can call the following methods from the list atUploadProvider.
UserProvider
Using theUserProvider
is very similar to using theRequestProvider
. You can call the following methods from the list atUserProvider.
PushRegistrationProvider
Using thePushRegistrationProvider
is very similar to using theRequestProvider
. You can call the following methods from the list atPushRegistrationProvider.