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; return address;
} }
@Override
public Object unwrap() {
return value();
}
@Override @Override
public int compareTo(Obj other) { public int compareTo(Obj other) {
return other instanceof ColonDef return other instanceof ColonDef
@ -191,6 +196,11 @@ public class Fcl {
return address; return address;
} }
@Override
public Object unwrap() {
return value();
}
@Override @Override
public int compareTo(Obj other) { public int compareTo(Obj other) {
return other instanceof Var return other instanceof Var
@ -270,6 +280,11 @@ public class Fcl {
return value; return value;
} }
@Override
public Object unwrap() {
return value();
}
@Override @Override
public int compareTo(Obj other) { public int compareTo(Obj other) {
return other instanceof Val return other instanceof Val
@ -278,7 +293,6 @@ public class Fcl {
} }
} }
public Fcl(FclStack stack, int heapSize, Transcript transcript) { public Fcl(FclStack stack, int heapSize, Transcript transcript) {
this.stack = stack; this.stack = stack;
this.heap = new Object[heapSize]; 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.FclStack;
import com.vectron.fcl.exceptions.InterOpFailed; import com.vectron.fcl.exceptions.InterOpFailed;
import com.vectron.fcl.types.Bool; import com.vectron.fcl.types.Bool;
import com.vectron.fcl.types.Dic;
import com.vectron.fcl.types.JvmObj; import com.vectron.fcl.types.JvmObj;
import com.vectron.fcl.types.Nil; import com.vectron.fcl.types.Nil;
import com.vectron.fcl.types.Num; import com.vectron.fcl.types.Num;
@ -114,6 +115,8 @@ class MethodSpec {
params.add(value.doubleValue()); params.add(value.doubleValue());
else if (clazz == String.class) else if (clazz == String.class)
params.add((String)value.value()); params.add((String)value.value());
else if (clazz == Dic.class)
params.add((Dic)value);
else if (clazz == Map.class) else if (clazz == Map.class)
params.add((Map)value.value()); params.add((Map)value.value());
else if (clazz == List.class) else if (clazz == List.class)
@ -133,6 +136,7 @@ class MethodSpec {
case 'm': return Map.class; case 'm': return Map.class;
case 't': return List.class; case 't': return List.class;
case 'O': return Obj.class; case 'O': return Obj.class;
case 'M': return Dic.class;
default: default:
throw new InterOpFailed("Invalid type spec: " + type); throw new InterOpFailed("Invalid type spec: " + type);
} }

View file

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

View file

@ -52,6 +52,14 @@ public class Dic implements Obj {
return value; 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 @Override
public int compareTo(Obj o) { public int compareTo(Obj o) {
return -1; return -1;

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -48,7 +48,12 @@ public class Quot implements Obj {
@Override @Override
public Object value() { public Object value() {
throw new TypeMismatched(this, "value"); throw new TypeMismatched(this, "value");
} }
@Override
public Object unwrap() {
return value();
}
@Override @Override
public Num asNum() { public Num asNum() {

View file

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

View file

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

View file

@ -1,12 +1,29 @@
'Content-Type' val: CONTENT-TYPE
'application/json' val: APPLICATION/JSON
: http-get ( u -- n b ) : http-get ( u -- n b )
'com.vectron.forthcalc.support.HttpClient/get/s' jvm-call-static -> response 'com.vectron.forthcalc.support.HttpClient/get/s' jvm-call-static -> response
response 'body' jvm-call-method response 'body' jvm-call-method
response 'code' jvm-call-method response 'code' jvm-call-method ;
;
: http-post ( d u -- n b ) : http-post ( d u -- n b )
-> url -> request -> 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 '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

@ -11,4 +11,6 @@
tbl elem count 1+ put tbl elem count 1+ put
then then
} each } each
tbl ; 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()); 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() { private String transcript() {
return transcript.content(); return transcript.content();
} }