http json request

This commit is contained in:
zeroflag 2021-06-29 15:43:15 +02:00
parent 386d25482a
commit 2a23efb6f9
16 changed files with 112 additions and 8 deletions

View file

@ -111,6 +111,11 @@ public class Fcl {
return address;
}
@Override
public Object unwrap() {
return value();
}
@Override
public int compareTo(Obj other) {
return other instanceof ColonDef
@ -191,6 +196,11 @@ public class Fcl {
return address;
}
@Override
public Object unwrap() {
return value();
}
@Override
public int compareTo(Obj other) {
return other instanceof Var
@ -270,6 +280,11 @@ public class Fcl {
return value;
}
@Override
public Object unwrap() {
return value();
}
@Override
public int compareTo(Obj other) {
return other instanceof Val
@ -278,7 +293,6 @@ public class Fcl {
}
}
public Fcl(FclStack stack, int heapSize, Transcript transcript) {
this.stack = stack;
this.heap = new Object[heapSize];

View file

@ -3,6 +3,7 @@ package com.vectron.fcl.interop;
import com.vectron.fcl.FclStack;
import com.vectron.fcl.exceptions.InterOpFailed;
import com.vectron.fcl.types.Bool;
import com.vectron.fcl.types.Dic;
import com.vectron.fcl.types.JvmObj;
import com.vectron.fcl.types.Nil;
import com.vectron.fcl.types.Num;
@ -114,6 +115,8 @@ class MethodSpec {
params.add(value.doubleValue());
else if (clazz == String.class)
params.add((String)value.value());
else if (clazz == Dic.class)
params.add((Dic)value);
else if (clazz == Map.class)
params.add((Map)value.value());
else if (clazz == List.class)
@ -133,6 +136,7 @@ class MethodSpec {
case 'm': return Map.class;
case 't': return List.class;
case 'O': return Obj.class;
case 'M': return Dic.class;
default:
throw new InterOpFailed("Invalid type spec: " + type);
}

View file

@ -43,6 +43,11 @@ public class Bool implements Obj, LogicOperand {
return value;
}
@Override
public Object unwrap() {
return value();
}
@Override
public long longValue() {
if (STRICT) throw new TypeMismatched(this, "long");

View file

@ -52,6 +52,14 @@ public class Dic implements Obj {
return value;
}
@Override
public Map<Object,Object> unwrap() {
Map<Object,Object> result = new LinkedHashMap<>();
for (Map.Entry<Obj,Obj> each : value.entrySet())
result.put(each.getKey().unwrap(), each.getValue().unwrap());
return result;
}
@Override
public int compareTo(Obj o) {
return -1;

View file

@ -57,6 +57,11 @@ public class JvmObj implements Obj {
return object;
}
@Override
public Object unwrap() {
return value();
}
@Override
public int compareTo(Obj o) {
return -1;

View file

@ -46,6 +46,14 @@ public class Lst implements Obj, ArithmeticOperand {
return value;
}
@Override
public Object unwrap() {
List<Object> result = new ArrayList<>();
for (Obj each : value)
result.add(each.unwrap());
return result;
}
@Override
public int compareTo(Obj o) {
return -1;

View file

@ -49,6 +49,11 @@ public class Nil implements Obj {
return null;
}
@Override
public Object unwrap() {
return value();
}
@Override
public int compareTo(Obj o) {
return -1;

View file

@ -251,6 +251,11 @@ public class Num implements Obj, LogicOperand, ArithmeticOperand {
return value;
}
@Override
public Object unwrap() {
return value();
}
@Override
public int compareTo(Obj other) {
return other instanceof Num

View file

@ -8,4 +8,5 @@ public interface Obj extends Comparable<Obj> {
Num asNum();
Str asStr();
Object value();
Object unwrap();
}

View file

@ -70,6 +70,11 @@ public class Primitive implements Word {
return code;
}
@Override
public Object unwrap() {
return value();
}
@Override
public String toString() {
return "xt_" + name;

View file

@ -50,6 +50,11 @@ public class Quot implements Obj {
throw new TypeMismatched(this, "value");
}
@Override
public Object unwrap() {
return value();
}
@Override
public Num asNum() {
if (STRICT) throw new TypeMismatched(this, "num");

View file

@ -76,6 +76,11 @@ public class Range implements Obj {
return iterator;
}
@Override
public Object unwrap() {
return value();
}
@Override
public int compareTo(Obj o) {
return -1;

View file

@ -46,6 +46,11 @@ public class Str implements Obj, ArithmeticOperand {
return Num.parse(value);
}
@Override
public Object unwrap() {
return value();
}
@Override
public Str asStr() {
return this;

View file

@ -1,12 +1,29 @@
'Content-Type' val: CONTENT-TYPE
'application/json' val: APPLICATION/JSON
: http-get ( u -- n b )
'com.vectron.forthcalc.support.HttpClient/get/s' jvm-call-static -> response
response 'body' jvm-call-method
response 'code' jvm-call-method
;
response 'code' jvm-call-method ;
: http-post ( d u -- n b )
-> url -> request
request jvmValue url 'com.vectron.forthcalc.support.HttpClient/post/sm' jvm-call-static -> response
request url 'com.vectron.forthcalc.support.HttpClient/post/sM' jvm-call-static -> response
response 'body' jvm-call-method
response 'code' jvm-call-method
;
response 'code' jvm-call-method ;
: http-put ( d u -- n b )
-> url -> request
request url 'com.vectron.forthcalc.support.HttpClient/put/sM' jvm-call-static -> response
response 'body' jvm-call-method
response 'code' jvm-call-method ;
: +json-type ( m -- m ) -> request
request 'headers' at -> headers
headers nil = if
#[ 'content' request
'headers' #[ CONTENT-TYPE APPLICATION/JSON ]# ]#
else
headers CONTENT-TYPE APPLICATION/JSON put
request
then ;

View file

@ -12,3 +12,5 @@
then
} each
tbl ;
: ms ( n -- ) 'java.lang.Thread/sleep/l' jvm-call-static ;

View file

@ -1139,6 +1139,16 @@ public class FclTest {
assertEquals(42, evalPop(" ': tst 42 ; tst' eval").intValue());
}
@Test
public void testHttpHeaders() {
assertEquals(
"#[ 'headers' #[ 'Content-Type' 'application/json' ]# 'content' #[ 'a' 1 ]# ]#",
evalPop("#[ 'a' 1 ]# +json-type").toString());
assertEquals(
"#[ 'content' #[ 'a' 1 ]# 'headers' #[ 'Content-Type' 'application/json' ]# ]#",
evalPop("#[ 'headers' #[ 'Content-Type' 'text/plain' ]# 'content' #[ 'a' 1 ]# ]# +json-type").toString());
}
private String transcript() {
return transcript.content();
}