All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.proximax.xpx.service.remote.RemoteUploadApi Maven / Gradle / Ivy

There is a newer version: 0.1.0-beta.10
Show newest version
/*
 * Copyright 2018 ProximaX Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Proximax P2P Storage REST API
 * Proximax P2P Storage REST API
 *
 * OpenAPI spec version: v0.0.1
 * Contact: [email protected]
 *
 * NOTE: This class is auto generated by the swagger code generator program.
 * https://github.com/swagger-api/swagger-codegen.git
 * Do not edit the class manually.
 */


package io.proximax.xpx.service.remote;

import com.google.gson.reflect.TypeToken;
import io.proximax.*;
import io.proximax.xpx.exceptions.ApiException;
import io.proximax.xpx.exceptions.PathUploadNotSupportedException;
import io.proximax.xpx.model.UploadBytesBinaryRequestParameter;
import io.proximax.xpx.model.UploadTextRequestParameter;
import io.proximax.xpx.service.intf.UploadApi;
import org.apache.commons.codec.binary.Base64;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.util.Collections.emptyMap;


/**
 * The Class RemoteUploadApi.
 */
public class RemoteUploadApi implements UploadApi {
	
	/** The api client. */
	private final ApiClient apiClient;

    /**
     * Instantiates a new remote upload api.
     *
     * @param apiClient the api client
     */
    public RemoteUploadApi(ApiClient apiClient) {
        this.apiClient = apiClient;
    }

    /**
     * Gets the api client.
     *
     * @return the api client
     */
    public ApiClient getApiClient() {
        return apiClient;
    }

    /**
     * Calls the garbage clean up and tries to unpin the given hash
     * This endpoint can be used to generates the datahash and uploads the file in the process.
     * @param multihash The pinned multihash (required)
     * @return String
     * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
     */
    public String deletePinnedContent(String multihash) throws ApiException {
        Object localVarPostBody = null;

        // create path and map variables
        String localVarPath = "/upload/cleanup";

        List localVarQueryParams = new ArrayList();
        if (multihash != null)
            localVarQueryParams.addAll(apiClient.parameterToPairs("", "multihash", multihash));

        Map localVarHeaderParams = new HashMap();

        Map localVarFormParams = new HashMap();

        final String[] localVarAccepts = {
                "application/json"
        };
        final String localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
        if (localVarAccept != null) localVarHeaderParams.put("Accept", localVarAccept);

        final String[] localVarContentTypes = {
                "application/json"
        };
        final String localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);
        localVarHeaderParams.put("Content-Type", localVarContentType);

        String[] localVarAuthNames = new String[] {  };
        com.squareup.okhttp.Call call =  apiClient.buildCall(localVarPath, "POST", localVarQueryParams,
                localVarPostBody, localVarHeaderParams, localVarFormParams, localVarAuthNames, null);

        Type localVarReturnType = new TypeToken(){}.getType();
        ApiResponse resp =  apiClient.execute(call, localVarReturnType);
        return resp.getData();
    }

    /**
     * Uploads a Base64 encoded byte[] binary file to the IPFS/P2P Storage Network
     * This endpoint can be used to generate the data that will be injected to the NEM Blockchain.
     * @return Object
     * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
     */
    public byte[] uploadBytesBinary(byte[] binaryData, String name, String contentType, String keywords, String metadata) throws ApiException {
        UploadBytesBinaryRequestParameter localVarPostBody = new UploadBytesBinaryRequestParameter()
				.contentType(contentType)
				.keywords(keywords)
				.metadata(metadata)
				.name(name)
				.data(binaryData);

        // create path and map variables
        String localVarPath = "/upload/bytes/binary";

        List localVarQueryParams = new ArrayList();

        Map localVarHeaderParams = new HashMap();

        final String[] localVarAccepts = {
                "*/*"
        };
        final String localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
        if (localVarAccept != null) localVarHeaderParams.put("Accept", localVarAccept);

        final String[] localVarContentTypes = {
                "application/json"
        };
        final String localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);
        localVarHeaderParams.put("Content-Type", localVarContentType);

        String[] localVarAuthNames = new String[] {  };


        com.squareup.okhttp.Call call = apiClient.buildCall(localVarPath, "POST", localVarQueryParams,
                localVarPostBody, localVarHeaderParams, emptyMap(), localVarAuthNames, null);
        Type localVarReturnType = new TypeToken(){}.getType();
//        final ApiResponse response = apiClient.execute(call, byte[].class);
        ApiResponse response = apiClient.execute(call, localVarReturnType);
        return response.getData();
    }

    /**
     * Upload the Text to the IPFS/P2P Storage Network
     * This endpoint can be used to generate the data that will be injected to the NEM Blockchain.
     * @return Object
     * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
     */
    public byte[] uploadText(byte[] textInBytes, String name, String contentType, String encoding, String keywords, String metadata) throws ApiException {
        UploadTextRequestParameter localVarPostBody = new UploadTextRequestParameter()
				.contentType(contentType)
				.encoding(encoding)
				.keywords(keywords)
				.metadata(metadata)
				.name(name)
				.text(Base64.encodeBase64String(textInBytes));

        // create path and map variables
        String localVarPath = "/upload/text";

        List localVarQueryParams = new ArrayList();

        Map localVarHeaderParams = new HashMap();

        Map localVarFormParams = new HashMap();

        final String[] localVarAccepts = {
                "*/*"
        };
        final String localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
        if (localVarAccept != null) localVarHeaderParams.put("Accept", localVarAccept);

        final String[] localVarContentTypes = {
                "application/json"
        };
        final String localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);
        localVarHeaderParams.put("Content-Type", localVarContentType);

        String[] localVarAuthNames = new String[] {  };

        com.squareup.okhttp.Call call =  apiClient.buildCall(localVarPath, "POST", localVarQueryParams,
                localVarPostBody, localVarHeaderParams, emptyMap(), localVarAuthNames, null);
        Type localVarReturnType = new TypeToken(){}.getType();
//        ApiResponse response = apiClient.execute(call, byte[].class);
        ApiResponse response = apiClient.execute(call, localVarReturnType);
        return response.getData();
    }

    @Override
    public byte[] uploadPath(String path, String name, String keywords, String metadata) {
        throw new PathUploadNotSupportedException("Path upload is not supported for remote upload");
    }
}