Custom API CallsJava


Manual Requests


Every API call you can make to MediaFire API may not be available from the ResponseGenerator class. There may be some API calls you wish to make that are not listed, or you may want to make a Request using GET instead of POST. In the circumstance described above, you can construct a Request yourself

API calls not listed in MFApi class


Rather than creating an MFRequest object using the constructor MFRequest(MFHost, MFApi, Map), an object can be created using the MFRequestBuilder class.

HostObject hostObject = new HostObject("http", "www", "mediafire.com", "post");
ApiObject apiObject = new ApiObject("system", "get_status.php");
InstructionsObject instructionsObject = new InstructionsObject(BorrowTokenType.NONE, SignatureType.NO_SIGNATURE_REQUIRED, ReturnTokenType.NONE, true);
VersionObject versionObject = new VersionObject("1.2");
Request request = new Request(hostObject, apiObject, instructionsObject, versionObject);
request.addQueryParameter("response_format", "json");

Request objects are constructed using a HostObject, ApiObject, InstructionsObject, and VersionObject.

Examples

device/get_trash

An example of an API not used in the MFApi class is the call to /api/device/get_trash.php. Below is an example call to /api/device/get_trash.php using MFRequestBuilder:

MFRequestBuilder mfRequestBuilder = new MFRequestBuilder(MFHost.LIVE_HTTP, "/api/device/get_trash.php");

// set request parameters for this request
Map requestParameters = new LinkedHashMap();
requestParameters.put("device_id", "my_device_x");
requestParameters.put("data_only", "yes");
requestParameters.put("version", "1.0");
mfRequestBuilder.requestParameters(requestParameters);

// a token is required for this API call
mfRequestBuilder.isTokenRequired(true);

// device/get_trash requires a session token. MFTokenFarm uses V2 tokens.
mfRequestBuilder.typeOfTokenToBorrow(MFApi.TokenType.V2);

// V2 tokens required a special kind of signature calculated, this is done by MFTokenFarm
mfRequestBuilder.typeOfSignatureToAdd(MFApi.TokenType.V2);

// V2 tokens can be reused so return token type is V2
mfRequestBuilder.typeOfTokenToReturn(MFApi.TokenType.V2);

// the query can use POST
mfRequestBuilder.isQueryPostable(true);

// create the request using build()
MFRequest mfRequest = mfRequestBuilder.build();

// receive a response from doRequest()
MFResponse mfResponse = mfTokenFarm.getMFHttpRunner().doRequest(mfRequest);

// do something with the response.


Tokens and the Token class

Some MediaFire API requests require the use of Tokens. This is represented in the SDK through the abstract class Token. The Java SDK uses 2 types of tokens: SessionToken and ActionToken.

  • SessionToken represents a Version 2 MediaFire Session Token.
  • ActionToken represents image and upload action tokens.

If you require using a Version 1 MediaFire Session Token, subclass Token and use a Version 1 MediaFire Session Token. Note that the process you use will be slightly more complex than making a single call to doRequest()

Ensure you understand how to use MediaFire requests and the use of these Session Tokens. Additionally, you will have to create your own ApiClient for Version 1 Session Tokens.

Important Note About Responses

The com.mediafire.sdk.api_responses class does not have response classes for every single MediaFire API call. This is not a problem, though! You can always write your own subclasses of ApiResponse (from com.mediafire.sdk.api_responses.ApiResponse.class). Google's Gson library is used to parse JSON responses. Review Google's documentation if you need to know how to write your own response class.

If your only concern is whether requests were successful or not, simply use the ApiResponse class when utilizing the getResponseObject() method from the MFResponse class.