Paraphrase file with GroupDocs.Rewriter SDK

Learn more

Although you can directly call the GroupDocs.Rewriter Cloud REST API to send file and fetch the result, there is a much easier way to implement required functionality in your applications. We provide software development kits (SDKs) for all popular programming languages. They wrap up all routine operations such as establishing connections, sending API requests, and parsing responses into a few simple methods. It makes interaction with GroupDocs.Rewriter Cloud services much easier, allowing you to focus on business logic rather than technical details.

using GroupDocs.Rewriter.Cloud.Sdk.Api;
using GroupDocs.Rewriter.Cloud.Sdk.Client;
using GroupDocs.Rewriter.Cloud.Sdk.Client.Auth;
using GroupDocs.Rewriter.Cloud.Sdk.Model;
using Configuration = GroupDocs.Rewriter.Cloud.Sdk.Client.Configuration;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;
using System.Net.Http;
using HttpStatusCode = System.Net.HttpStatusCode;
namespace GroupDocs.Rewriter.Cloud.Sdk
{
	  public class FileRewriter
	  {
		    public FileRewriter()
		    {
            Configuration config = new Configuration();
			      config.OAuthFlow = OAuthFlow.APPLICATION;
			      config.OAuthClientId = "YOU_CLIENT_ID";
			      config.OAuthClientSecret = "YOU_CLIENT_SECRET";
            config.BasePath = "https://api.groupdocs.cloud/v2.0/rewriter";
            ParaphraseApi api = new ParaphraseApi(conf);
            FileApi fileApi = new FileApi(conf);
            string sourceLanguage = "en";
            string format = "Pdf";
            string path = "/path/to/myFile.pdf";
            byte[] file = File.ReadAllBytes(path);
            MemoryStream ms = new MemoryStream(file);
            string url = fileApi.FileUploadPost(format, ms);
            ParaphraseFileResponse response = new ParaphraseFileResponse();
            ParaphraseFileRequest request = new ParaphraseFileRequest(
                format: ParaphraseFileRequest.FormatEnum.Pdf,
                outputFormat: SupportedConversionsFormats.Pdf,
                language: sourceLanguage,
                diversityDegree: DegreeEnum.Off,
                savingMode: FileSavingMode.Files,
                url: url);
            StatusResponse responseId = await api.ParaphraseDocumentPostAsync(request);
            try
            {
                if (responseId.Status.ToString() == "Accepted")
                {
                    while (true)
                    {
                        response = await api.ParaphraseDocumentRequestIdGetAsync(responseId.Id);
                        if (response.Status.ToString() == "OK")
                            break;
                        else
                            Thread.Sleep(2000);
                    }
                    using (var client = new HttpClient())
                    {
                        string saveName = $"/path/to/result.pdf";
                        var result = await client.GetByteArrayAsync(response.Url);
                        File.WriteAllBytes(saveName, result);
                    }
                }
                else
                {
                    response = new ParaphraseFileResponse() { Status = responseId.Status, Message = responseId.Message };
                    Console.WriteLine("File error: " + response.Message);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("File exception: " + ex.ToString());
            }                
        }
    }
}

Visit our GitHub repository for a working code and sample files: https://github.com/groupdocs-rewriter-cloud/groupdocs-rewriter-cloud-dotnet

import time
import groupdocs_rewriter_cloud
from groupdocs_rewriter_cloud.api.paraphrase_api import ParaphraseApi  
from groupdocs_rewriter_cloud.rest import ApiException
from groupdocs_rewriter_cloud.models import HttpStatusCode, FileSavingMode, ParaphraseFileRequest, ParaphraseSupportedFormats, SupportedConversionsFormats

api = ParaphraseApi()
file_api = FileApi()
api.api_client.configuration.client_id = "CLIENT_ID"
api.api_client.configuration.client_secret = "CLIENT_SECRET"
language = "en"
file_format = "Pdf"
file_path = "/path/to/myFile.pdf"
file_url = file_api.file_upload_post(format=file_format, file=file_path)
request = ParaphraseFileRequest(language=language, url=file_url, format=ParaphraseSupportedFormats.PDF, outputFormat=SupportedConversionsFormats.PDF)
status = api.paraphrase_document_post(request)
if status.status == groupdocs_rewriter_cloud.models.HttpStatusCode.ACCEPTED:
    while True:
        file_response = api.paraphrase_document_request_id_get(status.id)
        if file_response.status == groupdocs_rewriter_cloud.models.HttpStatusCode.OK:
            print(f'[{file_response.status}] {file_response.message}')
            break
        time.sleep(2)

Visit our GitHub repository for a working code and sample files: https://github.com/groupdocs-rewriter-cloud/groupdocs-rewriter-cloud-python

package com.groupdocs;
// Import classes:

import com.groupdocs.model.*;
import org.openapitools.client.api.ParaphraseApi;
import org.openapitools.client.api.FileApi;
import java.io.File;

public class Demo {
    public static void main(String[] args) {
        // Get Client Id and Client Secret from https://dashboard.groupdocs.cloud        
        String basePath = "https://api.groupdocs.cloud/v2.0/rewriter";
        String cliendId = "YOUR_CLIENT_ID";
        String clientSecret = "YOUR_CLIENT_SECRET";
        ApiClient defaultClient = new ApiClient(basePath, cliendId, clientSecret, null);

        // Upload file
        FileApi fileApi = new FileApi(defaultClient);
        String fileName = "FILE_PATH";
        File fileToUpload = new File(fileName);
        String file_url = null;

        try {
            String file_url = fileApi.fileUploadPost("FILE_FORMAT", fileToUpload);
        }
        catch(ApiException e){
                System.err.println("Exception when calling FileApi#fileUploadPost");
                System.err.println("Status code: " + e.getCode());
                System.err.println("Reason: " + e.getResponseBody());
                System.err.println("Response headers: " + e.getResponseHeaders());
                e.printStackTrace();
        }  

        // Create instance of the ParaphraseApi if the file was uploaded successfully
        if (file_url != null){        
            ParaphraseApi apiInstance = new ParaphraseApi(defaultClient);

            ParaphraseFileRequest fileRequest = new ParaphraseFileRequest();

            fileRequest.setLanguage("en");
            fileRequest.setFormat(ParaphraseSupportedFormats.PDF);
            fileRequest.setOutputFormat(SupportedConversionsFormats.PDF);
            fileRequest.setSavingMode(FileSavingMode.FILES);
            fileRequest.setUrl(file_url);
            fileRequest.setOrigin("");

            try {
                StatusResponse response = apiInstance.paraphraseDocumentPost(fileRequest);
                String response_id = response.getId();
                if (!response.getStatus().toString().equals("500")) {
                    while (true) {
                        ParaphraseFileResponse paraphraseResponse = apiInstance.paraphraseDocumentRequestIdGet(response_id);
                        if (paraphraseResponse.getStatus().toString().equals("200")) {
                            System.out.println(paraphraseResponse);
                            break;
                        }
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            } catch (ApiException e) {
                System.err.println("Exception when calling ParaphraseApi#paraphraseDocumentPost");
                System.err.println("Status code: " + e.getCode());
                System.err.println("Reason: " + e.getResponseBody());
                System.err.println("Response headers: " + e.getResponseHeaders());
                e.printStackTrace();
            }
        }
    }
}

Visit our GitHub repository for a working code and sample files: https://github.com/groupdocs-rewriter-cloud/groupdocs-rewriter-cloud-java