Request module
This commit is contained in:
3
Makefile
3
Makefile
@@ -27,8 +27,9 @@ TARGET = kyn
|
||||
all: $(TARGET)
|
||||
|
||||
# Link the object files to create the executable
|
||||
# At this point, we do -lcurl to link curl for the request lib
|
||||
$(TARGET): $(OBJS)
|
||||
$(CXX) $(CXXFLAGS) -o $(TARGET) $(OBJS)
|
||||
$(CXX) $(CXXFLAGS) -lcurl -o $(TARGET) $(OBJS)
|
||||
|
||||
# Compile .cpp files to .o files in the build directory
|
||||
$(BUILD_DIR)/%.o: %.cpp
|
||||
|
||||
@@ -22,6 +22,7 @@ InstructionType strToInstructionType(std::string in) {
|
||||
else if (in == "file") return InstructionType::File;
|
||||
else if (in == "struct") return InstructionType::Struct;
|
||||
else if (in == "assert") return InstructionType::Assert;
|
||||
else if (in == "request") return InstructionType::Request;
|
||||
else return InstructionType::Variable;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#include <map>
|
||||
|
||||
enum class InstructionType {
|
||||
None, Print, Println, Math, Let, Variable, Exit, If, While, Input, Compare, Function, Struct, Return, Concat, Split, File, Assert
|
||||
None, Print, Println, Math, Let, Variable, Exit, If, While, Input, Compare, Function, Struct, Return, Concat, Split, File, Assert, Request
|
||||
};
|
||||
|
||||
enum class ValueType {
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include "../modules/split/split.h"
|
||||
#include "../modules/concat/concat.h"
|
||||
#include "../modules/file/file.h"
|
||||
#include "../modules/request/request.h"
|
||||
#include "../modules/assert/assert.h"
|
||||
|
||||
// Forward declaration for mutual recursion
|
||||
@@ -257,6 +258,8 @@ Value execute(Instruction inst) {
|
||||
return modules::concat(inst.args);
|
||||
case InstructionType::File:
|
||||
return modules::file(inst.args);
|
||||
case InstructionType::Request:
|
||||
return modules::request(inst.args);
|
||||
case InstructionType::Exit:
|
||||
return modules::exit(inst.args);
|
||||
case InstructionType::Return: {
|
||||
|
||||
69
src/modules/request/request.cpp
Normal file
69
src/modules/request/request.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
#include "request.h"
|
||||
#include "../../defs/defs.h"
|
||||
#include "../../error/error.h"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <curl/curl.h>
|
||||
|
||||
enum class RequestType {
|
||||
GET, POST
|
||||
};
|
||||
|
||||
size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* userp) {
|
||||
size_t totalSize = size * nmemb;
|
||||
userp->append((char*)contents, totalSize);
|
||||
return totalSize;
|
||||
}
|
||||
|
||||
std::string curlWrapper(std::string url, RequestType requestType, std::string data = "") {
|
||||
CURL* curl = curl_easy_init();
|
||||
std::string response;
|
||||
if (curl) {
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
|
||||
curl_easy_setopt(curl, CURLOPT_USERAGENT, "kyn-libcurl-agent/1.0");
|
||||
|
||||
if (requestType == RequestType::POST) {
|
||||
curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
|
||||
}
|
||||
|
||||
CURLcode res = curl_easy_perform(curl);
|
||||
if (res != CURLE_OK) {
|
||||
error("Request failed: " + std::string(curl_easy_strerror(res)));
|
||||
response = "KYNERR";
|
||||
}
|
||||
|
||||
curl_easy_cleanup(curl);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
Value modules::request(std::vector<Value> args) {
|
||||
if (args.size() < 2) {
|
||||
error("Not enough args for request module");
|
||||
return Value("Error");
|
||||
}
|
||||
if (args[1].valtype != ValueType::String) {
|
||||
error("Expecting a string to identify request URL");
|
||||
return Value("Error");
|
||||
}
|
||||
if (args[0].valtype == ValueType::Identifier) {
|
||||
if (args[0].string_val == "get") {
|
||||
std::string response = curlWrapper(args[1].string_val, RequestType::GET);
|
||||
if (response == "KYNERR") return Value("Error");
|
||||
return Value(response);
|
||||
} else if (args[0].string_val == "post") {
|
||||
if (args.size() < 3 || args[2].valtype != ValueType::String) {
|
||||
error("post submodule of request expects a string as 2nd arg");
|
||||
return Value("Error");
|
||||
}
|
||||
std::string response = curlWrapper(args[1].string_val, RequestType::POST, args[2].string_val);
|
||||
if (response == "KYNERR") return Value("Error");
|
||||
return Value(response);
|
||||
}
|
||||
}
|
||||
error("Unknown submodule for request. Only 'get' and 'post' are available.");
|
||||
return Value("Error");
|
||||
}
|
||||
8
src/modules/request/request.h
Normal file
8
src/modules/request/request.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include "../../defs/defs.h"
|
||||
|
||||
namespace modules {
|
||||
Value request(std::vector<Value> args);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
let counter = 0
|
||||
|
||||
while compare $counter <= 1000 {
|
||||
while compare $counter <= 10000 {
|
||||
println $counter
|
||||
counter = (math 1 + $counter)
|
||||
}
|
||||
|
||||
33
tests/test.kyn
Normal file
33
tests/test.kyn
Normal file
@@ -0,0 +1,33 @@
|
||||
println "thing"
|
||||
|
||||
if compare 5 == 10 {
|
||||
println "uh oh its broken"
|
||||
} else {
|
||||
println "its working"
|
||||
}
|
||||
|
||||
struct Dingus {
|
||||
name = <String>
|
||||
age = <Int>
|
||||
fun init name age {
|
||||
self name = name
|
||||
self age = age
|
||||
}
|
||||
|
||||
fun tostring {
|
||||
return (concat name ", " age)
|
||||
}
|
||||
}
|
||||
|
||||
let myDingus = (Dingus "dingus" 23)
|
||||
|
||||
fun myFn param1 param2 andSoOn {
|
||||
println param1
|
||||
return param2
|
||||
}
|
||||
|
||||
println (myFn 123 "hi" 3.14)
|
||||
|
||||
let favNumbers = [7, 14, 21]
|
||||
|
||||
println (file read "README.md")
|
||||
Reference in New Issue
Block a user