Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"shell",
"snippet",
"swift",
"swift",
"scala",
"unirest",
"xhr",
"xmlhttprequest"
Expand Down Expand Up @@ -93,4 +93,4 @@
"stringify-object": "3.3.0",
"yargs": "^17.4.0"
}
}
}
50 changes: 50 additions & 0 deletions src/targets/scala/sttp/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { CodeBuilder } from '../../../helpers/code-builder';
import { escapeForDoubleQuotes } from '../../../helpers/escape';
import { Client } from '../../targets';

export const sttp: Client = {
info: {
key: 'sttp',
title: 'sttp',
link: 'https://sttp.softwaremill.com/',
description: 'The Scala HTTP Client API you always wanted',
},
convert: ({ allHeaders, method, fullUrl, headersObj, cookies, postData }) => {
const { push, join } = new CodeBuilder();
const formattedMethod = method.toLowerCase();
const supportedMethods = ['get', 'post', 'put', 'delete', 'patch', 'head', 'options'];

if (!supportedMethods.includes(formattedMethod)) {
return 'Method not supported';
}

push('import sttp.client4.*');
push('');
push('val backend = DefaultSyncBackend()');

let requestChain = `basicRequest.${formattedMethod}(uri"${fullUrl}")`;

// Add headers
Object.keys(headersObj).forEach(key => {
const escapedVal = escapeForDoubleQuotes(headersObj[key]);
requestChain += `\n .header("${escapeForDoubleQuotes(key)}", "${escapedVal}")`;
});

// Add cookies (sttp has native .cookie support)
cookies.forEach(({ name, value }) => {
requestChain += `\n .cookie("${escapeForDoubleQuotes(name)}", "${escapeForDoubleQuotes(value)}")`;
});

// Add request body
if (postData && postData.text) {
const escapedBody = escapeForDoubleQuotes(postData.text);
requestChain += `\n .body("${escapedBody}")`;
}

push(`val response = ${requestChain}`);
push(' .send(backend)');
push('');

return join();
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import sttp.client4.*

val backend = DefaultSyncBackend()
val response = basicRequest.post(uri"http://mockbin.com/har")
.header("content-type", "application/x-www-form-urlencoded")
.body("foo=bar&hello=world")
.send(backend)
7 changes: 7 additions & 0 deletions src/targets/scala/sttp/fixtures/application-json.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import sttp.client4.*

val backend = DefaultSyncBackend()
val response = basicRequest.post(uri"http://mockbin.com/har")
.header("content-type", "application/json")
.body("{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}],\"boolean\":false}")
.send(backend)
7 changes: 7 additions & 0 deletions src/targets/scala/sttp/fixtures/cookies.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import sttp.client4.*

val backend = DefaultSyncBackend()
val response = basicRequest.post(uri"http://mockbin.com/har")
.cookie("foo", "bar")
.cookie("bar", "baz")
.send(backend)
1 change: 1 addition & 0 deletions src/targets/scala/sttp/fixtures/custom-method.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Method not supported
10 changes: 10 additions & 0 deletions src/targets/scala/sttp/fixtures/full.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import sttp.client4.*

val backend = DefaultSyncBackend()
val response = basicRequest.post(uri"http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value")
.header("accept", "application/json")
.header("content-type", "application/x-www-form-urlencoded")
.cookie("foo", "bar")
.cookie("bar", "baz")
.body("foo=bar")
.send(backend)
8 changes: 8 additions & 0 deletions src/targets/scala/sttp/fixtures/headers.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import sttp.client4.*

val backend = DefaultSyncBackend()
val response = basicRequest.get(uri"http://mockbin.com/har")
.header("accept", "application/json")
.header("x-foo", "Bar")
.header("quoted-value", "\"quoted\" 'string'")
.send(backend)
5 changes: 5 additions & 0 deletions src/targets/scala/sttp/fixtures/https.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import sttp.client4.*

val backend = DefaultSyncBackend()
val response = basicRequest.get(uri"https://mockbin.com/har")
.send(backend)
7 changes: 7 additions & 0 deletions src/targets/scala/sttp/fixtures/jsonObj-multiline.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import sttp.client4.*

val backend = DefaultSyncBackend()
val response = basicRequest.post(uri"http://mockbin.com/har")
.header("content-type", "application/json")
.body("{\n \"foo\": \"bar\"\n}")
.send(backend)
7 changes: 7 additions & 0 deletions src/targets/scala/sttp/fixtures/jsonObj-null-value.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import sttp.client4.*

val backend = DefaultSyncBackend()
val response = basicRequest.post(uri"http://mockbin.com/har")
.header("content-type", "application/json")
.body("{\"foo\":null}")
.send(backend)
7 changes: 7 additions & 0 deletions src/targets/scala/sttp/fixtures/multipart-data.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import sttp.client4.*

val backend = DefaultSyncBackend()
val response = basicRequest.post(uri"http://mockbin.com/har")
.header("content-type", "multipart/form-data; boundary=---011000010111000001101001")
.body("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"bar\"\r\n\r\nBonjour le monde\r\n-----011000010111000001101001--\r\n")
.send(backend)
7 changes: 7 additions & 0 deletions src/targets/scala/sttp/fixtures/multipart-file.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import sttp.client4.*

val backend = DefaultSyncBackend()
val response = basicRequest.post(uri"http://mockbin.com/har")
.header("content-type", "multipart/form-data; boundary=---011000010111000001101001")
.body("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\n\r\n-----011000010111000001101001--\r\n")
.send(backend)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import sttp.client4.*

val backend = DefaultSyncBackend()
val response = basicRequest.post(uri"http://mockbin.com/har")
.header("Content-Type", "multipart/form-data")
.send(backend)
7 changes: 7 additions & 0 deletions src/targets/scala/sttp/fixtures/multipart-form-data.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import sttp.client4.*

val backend = DefaultSyncBackend()
val response = basicRequest.post(uri"http://mockbin.com/har")
.header("Content-Type", "multipart/form-data; boundary=---011000010111000001101001")
.body("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n-----011000010111000001101001--\r\n")
.send(backend)
5 changes: 5 additions & 0 deletions src/targets/scala/sttp/fixtures/nested.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import sttp.client4.*

val backend = DefaultSyncBackend()
val response = basicRequest.get(uri"http://mockbin.com/har?foo%5Bbar%5D=baz%2Czap&fiz=buz&key=value")
.send(backend)
5 changes: 5 additions & 0 deletions src/targets/scala/sttp/fixtures/query.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import sttp.client4.*

val backend = DefaultSyncBackend()
val response = basicRequest.get(uri"http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value")
.send(backend)
5 changes: 5 additions & 0 deletions src/targets/scala/sttp/fixtures/short.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import sttp.client4.*

val backend = DefaultSyncBackend()
val response = basicRequest.get(uri"http://mockbin.com/har")
.send(backend)
7 changes: 7 additions & 0 deletions src/targets/scala/sttp/fixtures/text-plain.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import sttp.client4.*

val backend = DefaultSyncBackend()
val response = basicRequest.post(uri"http://mockbin.com/har")
.header("content-type", "text/plain")
.body("Hello World")
.send(backend)
14 changes: 14 additions & 0 deletions src/targets/scala/target.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Target } from '../targets';
import { sttp } from './sttp/client';

export const scala: Target = {
info: {
key: 'scala',
title: 'Scala',
extname: '.scala',
default: 'sttp',
},
clientsById: {
sttp,
},
};
5 changes: 3 additions & 2 deletions src/targets/targets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { ruby } from './ruby/target';
import { rust } from './rust/target';
import { shell } from './shell/target';
import { swift } from './swift/target';
import { scala } from './scala/target';

export type TargetId = keyof typeof targets;

Expand Down Expand Up @@ -79,6 +80,7 @@ export const targets = {
rust,
shell,
swift,
scala
};

export const isTarget = (target: Target): target is Target => {
Expand Down Expand Up @@ -131,8 +133,7 @@ export const isTarget = (target: Target): target is Target => {

if (!Object.prototype.hasOwnProperty.call(target.clientsById, target.info.default)) {
throw new Error(
`target ${target.info.key} is configured with a default client ${
target.info.default
`target ${target.info.key} is configured with a default client ${target.info.default
}, but no such client was found in the property \`clientsById\` (found ${JSON.stringify(
Object.keys(target.clientsById),
)})`,
Expand Down