Package com.mongodb.stitch.android.core
Interface StitchAppClient
-
- All Superinterfaces:
AutoCloseable
,Closeable
- All Known Implementing Classes:
StitchAppClientImpl
public interface StitchAppClient extends Closeable
StitchAppClient is the main class used to work with Stitch.Obtain an instance of this class from the
Stitch
singleton.User login is possible with the
StitchAuth
fromgetAuth()
.Use
callFunction(java.lang.String,java.util.List<?>)
to call Stitch Functions.This class can also access different services like local and remote MongoDB, AWS, and HTTP. Use the
RemoteMongoClient
service via<T>getServiceClient(com.mongodb.stitch.android.core.services.internal.NamedServiceClientFactory<T>,java.lang.String)
for reading and writing on a remote database.- See Also:
Stitch
,StitchAuth
,RemoteMongoClient
-
-
Example
// Get the default app client. This is automatically initialized by Stitch // if there is a `stitch_client_app_id` value in the values/strings.xml file. StitchAppClient appClient = Stitch.getDefaultAppClient(); // Log in anonymously. Some form of login is required before we can read documents. appClient.getAuth().loginWithCredential(new AnonymousCredential()).addOnCompleteListener(new OnCompleteListener<StitchUser>() { @Override public void onComplete(@NonNull Task<StitchUser> task) { if (!task.isSuccessful()) { Log.e(TAG, "Failed to log in!", task.getException()); return; } // Get the Atlas client. RemoteMongoClient mongoClient = appClient.getServiceClient(RemoteMongoClient.factory, "mongodb-atlas"); RemoteMongoDatabase db = mongoClient.getDatabase("video"); RemoteMongoCollection<Document> movieDetails = db.getCollection("movieDetails"); // Find 20 documents movieDetails.find() .projection(new Document().append("title", 1).append("year", 1)) .limit(20) .forEach(document -> { // Print documents to the log. Log.i(TAG, "Got document: " + document.toString()); }); } });
-
-
-
Method Summary
Modifier and Type Method Description Task<Void>
callFunction(String name, List<?> args)
Calls the specified Stitch function.<ResultT> Task<ResultT>
callFunction(String name, List<?> args, Class<ResultT> resultClass)
Calls the specified Stitch function, and decodes the response into an instance of the specified type.<ResultT> Task<ResultT>
callFunction(String name, List<?> args, Class<ResultT> resultClass, CodecRegistry codecRegistry)
Calls the specified Stitch function, and decodes the response into an instance of the specified type.Task<Void>
callFunction(String name, List<?> args, Long requestTimeout)
Calls the specified Stitch function.<ResultT> Task<ResultT>
callFunction(String name, List<?> args, Long requestTimeout, Class<ResultT> resultClass)
Calls the specified Stitch function, and decodes the response into an instance of the specified type.<ResultT> Task<ResultT>
callFunction(String name, List<?> args, Long requestTimeout, Class<ResultT> resultClass, CodecRegistry codecRegistry)
Calls the specified Stitch function, and decodes the response into an instance of the specified type.<ResultT> Task<ResultT>
callFunction(String name, List<?> args, Long requestTimeout, Decoder<ResultT> resultDecoder)
<ResultT> Task<ResultT>
callFunction(String name, List<?> args, Decoder<ResultT> resultDecoder)
void
close()
Closes the client and shuts down all background operations.StitchAuth
getAuth()
Gets the authentication component of the app.StitchPush
getPush()
Gets the push notifications component of the app.<T> T
getServiceClient(NamedServiceClientFactory<T> factory, String serviceName)
Gets a client for the given named service.<T> T
getServiceClient(ServiceClientFactory<T> factory)
Gets a client for the given service.StitchServiceClient
getServiceClient(String serviceName)
Gets a general purpose client for the given named service.
-
-
-
Method Detail
-
getAuth
StitchAuth getAuth()
Gets the authentication component of the app. This is used for logging in and managing users.- Returns:
- the authentication component of the app.
-
getPush
StitchPush getPush()
Gets the push notifications component of the app. This is used for registering for push notifications.- Returns:
- the push notifications component of the app.
-
getServiceClient
<T> T getServiceClient(NamedServiceClientFactory<T> factory, String serviceName)
Gets a client for the given named service.- Type Parameters:
T
- the type of client to be returned by the factory.- Parameters:
factory
- the factory that will create a client for the service.serviceName
- the name of the service.- Returns:
- a client to interact with the service.
-
getServiceClient
<T> T getServiceClient(ServiceClientFactory<T> factory)
Gets a client for the given service. Only some services offer a factory that requires no service name.- Type Parameters:
T
- the type of client to be returned by the factory.- Parameters:
factory
- the factory that will create a client for the service.- Returns:
- a client to interact with the service.
-
getServiceClient
StitchServiceClient getServiceClient(String serviceName)
Gets a general purpose client for the given named service.- Parameters:
serviceName
- the name of the service.- Returns:
- a client to interact with the service.
-
callFunction
Task<Void> callFunction(String name, List<?> args)
Calls the specified Stitch function.- Parameters:
name
- the name of the Stitch function to call.args
- the arguments to pass to the function.- Returns:
- a
Task
that completes when the function call completes.
-
callFunction
Task<Void> callFunction(String name, List<?> args, Long requestTimeout)
Calls the specified Stitch function. Also accepts a timeout in milliseconds. Use this for functions that may run longer than the client-wide default timeout (15 seconds by default).- Parameters:
name
- the name of the Stitch function to call.args
- the arguments to pass to the function.requestTimeout
- the number of milliseconds the client should wait for a response from the server before failing with an error.- Returns:
- a
Task
containing the decoded value.
-
callFunction
<ResultT> Task<ResultT> callFunction(String name, List<?> args, Class<ResultT> resultClass)
Calls the specified Stitch function, and decodes the response into an instance of the specified type. The response will be decoded using the codec registry specified when the client was configured. If no codec registry was configured, a default codec registry will be used. The default codec registry supports the mappings specified here- Type Parameters:
ResultT
- the type into which the Stitch response will be decoded.- Parameters:
name
- the name of the Stitch function to call.args
- he arguments to pass to the function.resultClass
- the class that the response should be decoded as.- Returns:
- a
Task
containing the decoded value.
-
callFunction
<ResultT> Task<ResultT> callFunction(String name, List<?> args, Long requestTimeout, Class<ResultT> resultClass)
Calls the specified Stitch function, and decodes the response into an instance of the specified type. The response will be decoded using the codec registry specified when the client was configured. If no codec registry was configured, a default codec registry will be used. The default codec registry supports the mappings specified here Also accepts a timeout in milliseconds. Use this for functions that may run longer than the client-wide default timeout (15 seconds by default).- Type Parameters:
ResultT
- the type into which the Stitch response will be decoded.- Parameters:
name
- the name of the Stitch function to call.args
- the arguments to pass to the function.requestTimeout
- the number of milliseconds the client should wait for a response from the server before failing with an error.resultClass
- the class that the response should be decoded as.- Returns:
- a
Task
containing the decoded value.
-
callFunction
<ResultT> Task<ResultT> callFunction(String name, List<?> args, Decoder<ResultT> resultDecoder)
Calls the specified Stitch function, and decodes the response into a value using the providedDecoder
orCodec
.- Type Parameters:
ResultT
- the type into which the response will be decoded.- Parameters:
name
- the name of the Stitch function to call.args
- the arguments to pass to the function.resultDecoder
- theDecoder
orCodec
to use to decode the response into a value.- Returns:
- a
Task
containing the decoded value.
-
callFunction
<ResultT> Task<ResultT> callFunction(String name, List<?> args, Long requestTimeout, Decoder<ResultT> resultDecoder)
Calls the specified Stitch function, and decodes the response into a value using the providedDecoder
orCodec
. Also accepts a timeout in milliseconds. Use this for functions that may run longer than the client-wide default timeout (15 seconds by default).- Type Parameters:
ResultT
- the type into which the response will be decoded.- Parameters:
name
- the name of the Stitch function to call.args
- the arguments to pass to the function.requestTimeout
- the number of milliseconds the client should wait for a response from the server before failing with an error.resultDecoder
- theDecoder
orCodec
to use to decode the response into a value.- Returns:
- a
Task
containing the decoded value.
-
callFunction
<ResultT> Task<ResultT> callFunction(String name, List<?> args, Class<ResultT> resultClass, CodecRegistry codecRegistry)
Calls the specified Stitch function, and decodes the response into an instance of the specified type. The response will be decoded using the codec registry given.- Type Parameters:
ResultT
- the type into which the Stitch response will be decoded.- Parameters:
name
- the name of the Stitch function to call.args
- the arguments to pass to the function.resultClass
- the class that the response should be decoded as.codecRegistry
- the codec registry used for de/serialization of the function call.- Returns:
- a
Task
containing the decoded value.
-
callFunction
<ResultT> Task<ResultT> callFunction(String name, List<?> args, Long requestTimeout, Class<ResultT> resultClass, CodecRegistry codecRegistry)
Calls the specified Stitch function, and decodes the response into an instance of the specified type. The response will be decoded using the codec registry given. Also accepts a timeout in milliseconds. Use this for functions that may run longer than the client-wide default timeout (15 seconds by default).- Type Parameters:
ResultT
- the type into which the Stitch response will be decoded.- Parameters:
name
- the name of the Stitch function to call.args
- the arguments to pass to the function.requestTimeout
- the number of milliseconds the client should wait for a response from the server before failing with an error.resultClass
- the class that the response should be decoded as.codecRegistry
- the codec registry used for de/serialization of the function call.- Returns:
- a
Task
containing the decoded value.
-
close
void close() throws IOException
Closes the client and shuts down all background operations.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
- if any thread shutdown errors happen.
-
-