Requests return dynamic
This commit is contained in:
@@ -1,2 +1 @@
|
||||
import requests_externs.Response;
|
||||
import requests_externs.Requests;
|
||||
|
@@ -2,8 +2,8 @@ package nat.systems;
|
||||
|
||||
import kiss.Prelude;
|
||||
import kiss.List;
|
||||
import requests_externs.Response;
|
||||
import nat.System;
|
||||
import haxe.Json;
|
||||
|
||||
@:build(kiss.Kiss.build())
|
||||
class MediaWikiSystem extends System {}
|
||||
|
@@ -12,21 +12,25 @@
|
||||
// TODO make this an externMethod --
|
||||
// but mediaWikiUrl, headers, and maxLag will still have to be specified, not as args
|
||||
// unless all vars and props are passed to externmethods by default
|
||||
(method :Response queryProp [:Array<String> titles :Array<String> props]
|
||||
(#extern Response python
|
||||
(object
|
||||
hxmlFile "extern-files/python/args.hxml"
|
||||
importHxFile "extern-files/python/import.hx"
|
||||
langProjectFile "extern-files/python/requirements.txt")
|
||||
[:Array<String> titles _ :Array<String> props _ :String mediaWikiUrl _ :String maxLag (if maxLag (Std.string maxLag) "1") :Map<String,String> headers _]
|
||||
(Requests.get mediaWikiUrl
|
||||
[
|
||||
=>"action" "query"
|
||||
=>"titles" (titles.join "|")
|
||||
=>"prop" (props.join "|")
|
||||
=>"maxlag" maxLag
|
||||
=>"format" "json"
|
||||
]
|
||||
(object headers headers))))
|
||||
(method :Dynamic queryProp [:Array<String> titles :Array<String> props]
|
||||
(Json.parse
|
||||
(#extern String python
|
||||
(object
|
||||
hxmlFile "extern-files/python/args.hxml"
|
||||
importHxFile "extern-files/python/import.hx"
|
||||
langProjectFile "extern-files/python/requirements.txt")
|
||||
[:Array<String> titles _ :Array<String> props _ :String mediaWikiUrl _ :String maxLag (if maxLag (Std.string maxLag) "1") :Map<String,String> headers _]
|
||||
(let [response
|
||||
(Requests.get mediaWikiUrl
|
||||
[
|
||||
=>"action" "query"
|
||||
=>"titles" (titles.join "|")
|
||||
=>"prop" (props.join "|")
|
||||
=>"maxlag" maxLag
|
||||
=>"format" "json"
|
||||
]
|
||||
(object headers headers))]
|
||||
(assert response.ok)
|
||||
response.text))))
|
||||
|
||||
(var headers [=>"User-Agent" "NatArchiveTool/0.0.0 (https://github.com/NQNStudios/kisslang/tree/main/projects/nat-archive-tool; natquaylenelson@gmail.com) Requests/2.26.0"])
|
@@ -31,6 +31,4 @@
|
||||
(assert (= name "Adventure"))))
|
||||
|
||||
(let [wikipedia (new MediaWikiSystem "https://en.wikipedia.org/w/api.php" null null 1)]
|
||||
//~(wikipedia.queryProp ["Phoenix Wright"] ["images"])
|
||||
0
|
||||
)
|
||||
~(wikipedia.queryProp ["Phoenix Wright"] ["images"]))
|
@@ -3,7 +3,6 @@ package requests_externs;
|
||||
import haxe.extern.EitherType;
|
||||
import python.Dict;
|
||||
import python.KwArgs;
|
||||
import requests_externs.Response;
|
||||
|
||||
typedef NativeRequestKwArgs = {
|
||||
?headers:Dict<String, String>
|
||||
@@ -15,12 +14,12 @@ typedef RequestKwArgs = {
|
||||
|
||||
@:pythonImport("requests")
|
||||
extern class NativeRequests {
|
||||
public static function get(url:String, params:Dict<String, String>, ?kwArgs:KwArgs<RequestKwArgs>):NativeResponse;
|
||||
public static function get(url:String, params:Dict<String, String>, ?kwArgs:KwArgs<NativeRequestKwArgs>):Dynamic;
|
||||
}
|
||||
|
||||
class Requests {
|
||||
public static function get(url:String, params:Map<String, String>, ?kwArgs:KwArgs<RequestKwArgs>):NativeResponse {
|
||||
return NativeRequests.get(url, mapToDict(params), kwArgs);
|
||||
public static function get(url:String, params:Map<String, String>, ?kwArgs:RequestKwArgs):Dynamic {
|
||||
return NativeRequests.get(url, mapToDict(params), kwArgsToNativeKwArgs(kwArgs));
|
||||
}
|
||||
|
||||
static function mapToDict(?map:Map<String, String>) {
|
||||
@@ -33,7 +32,9 @@ class Requests {
|
||||
return dict;
|
||||
}
|
||||
|
||||
static function kwArgsToNativeKwArgs(kwArgs:RequestKwArgs) {
|
||||
static function kwArgsToNativeKwArgs(?kwArgs:RequestKwArgs) {
|
||||
if (kwArgs == null)
|
||||
return null;
|
||||
return {
|
||||
headers: mapToDict(kwArgs.headers)
|
||||
};
|
||||
|
@@ -1,6 +0,0 @@
|
||||
package requests_externs;
|
||||
|
||||
@:pythonImport("requests.Response")
|
||||
extern class NativeResponse {}
|
||||
|
||||
typedef Response = {};
|
Reference in New Issue
Block a user