API

class API

This API serves as the interface between the rest of the app and the CultureMesh servers. When another part of the app needs to request information, it calls API methods to obtain it. Similarly, API methods should be used to store, send, and update information. The API then handles requesting it from the CultureMesh servers.

Fields

API_URL_BASE

static final String API_URL_BASE

Base of the URL all API endpoints use. For example, the /token endpoint has the URL API_URL_BASE + "/token".

CURRENT_USER

static final String CURRENT_USER

Identifier for the currently-signed-in user’s ID. If no user is signed-in, this key should be removed from the preferences Example: settings.getLong(API.CURRENT_USER, -1).

FEED_ITEM_COUNT_SIZE

static final String FEED_ITEM_COUNT_SIZE

The number of items (e.g. org.codethechange.culturemesh.models.Posts or Events to fetch with each paginated request

HOSTING

static final String HOSTING

LOGIN_TOKEN

static final String LOGIN_TOKEN

Settings identifier for the currently cached login token for the user. May be missing or expired. Expiration is tracked using API.TOKEN_REFRESH.

NO_MAX_PAGINATION

static final String NO_MAX_PAGINATION

SELECTED_NETWORK

static final String SELECTED_NETWORK

Identifier for the user’s currently selected Network. This is used to save the network the user was last viewing so that network can be re-opened when the user navigates back. Example: settings.getLong(API.SELECTED_NETWORK, -1).

SELECTED_USER

static final String SELECTED_USER

The SharedPreferences key for communicating to ViewProfileActivity which user we are viewing.

SETTINGS_IDENTIFIER

static final String SETTINGS_IDENTIFIER

Identifier for the app’s shared preferences. Example: SharedPreferences settings = getSharedPreferences(API.SETTINGS_IDENTIFIER, MODE_PRIVATE)

TOKEN_REFRESH

static final int TOKEN_REFRESH

Number of milliseconds to use a login token before refreshing it. Note that this is not how long the token is valid, just how often to refresh it. Refresh time must be shorter than the validity time.

See also: API.LOGIN_TOKEN

TOKEN_RETRIEVED

static final String TOKEN_RETRIEVED

Settings identifier for when the current login token was retrieved. Stored as the number of milliseconds since the epoch.

See also: API.LOGIN_TOKEN

USER_EMAIL

static final String USER_EMAIL

Identifier for the currently-signed-in user’s email. If no user is signed-in, this key should be removed from the preferences Example: settings.getLong(API.USER_EMAIL, -1).

Methods

genBasicAuth

static String genBasicAuth(String email, String password)

Generate from a username/email and password the string to put in the header of a request as the value of the Authorization token in order to perform Basic Authentication. For example: headers.put("Authorization", genBasicAuth(email, password)). A login token can be used if it is passed as the email, in which case the password is ignored by the server.

Parameters:
  • email – Email or username of account to login as; can also be a login token
  • password – Password to login with
Returns:

Value that should be passed in the header as the value of Authorization

genBasicAuth

static String genBasicAuth(String token)

Generate from a login token the string to put in the header of a request as the value of the Authorization token in order to perform Basic Authentication. For example: headers.put("Authorization", genBasicAuth(token)).

Parameters:
  • token – Login token to authenticate to server
Returns:

Value that should be passed in the header as the value of Authorization

getCredentials

static String getCredentials()

Use this method to append our credentials to our server requests. For now, we are using a static API key. In the future, we are going to want to pass session tokens.

Returns:credentials string to be appended to request url as a param.