ahmadnassri/httpsnippet

View on GitHub
src/targets/java/asynchttp/client.ts

Summary

Maintainability
A
0 mins
Test Coverage
/**
 * @description
 * Asynchronous Http and WebSocket Client library for Java
 *
 * @author
 * @windard
 *
 * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
 */

import { CodeBuilder } from '../../../helpers/code-builder';
import { escapeForDoubleQuotes } from '../../../helpers/escape';
import { Client } from '../../targets';

export const asynchttp: Client = {
  info: {
    key: 'asynchttp',
    title: 'AsyncHttp',
    link: 'https://github.com/AsyncHttpClient/async-http-client',
    description: 'Asynchronous Http and WebSocket Client library for Java',
  },
  convert: ({ method, allHeaders, postData, fullUrl }, options) => {
    const opts = {
      indent: '  ',
      ...options,
    };
    const { blank, push, join } = new CodeBuilder({ indent: opts.indent });

    push('AsyncHttpClient client = new DefaultAsyncHttpClient();');

    push(`client.prepare("${method.toUpperCase()}", "${fullUrl}")`);

    // Add headers, including the cookies
    Object.keys(allHeaders).forEach(key => {
      push(`.setHeader("${key}", "${escapeForDoubleQuotes(allHeaders[key])}")`, 1);
    });

    if (postData.text) {
      push(`.setBody(${JSON.stringify(postData.text)})`, 1);
    }

    push('.execute()', 1);
    push('.toCompletableFuture()', 1);
    push('.thenAccept(System.out::println)', 1);
    push('.join();', 1);
    blank();
    push('client.close();');

    return join();
  },
};