1 /**
2 A helper to handle dictionaries which look like JSONRPC documents.
3 
4 Copyright:
5 Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur.  
6 Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md)  
7 Copyright (c) 2017-2018 Godot-D contributors  
8 
9 License: $(LINK2 https://opensource.org/licenses/MIT, MIT License)
10 
11 
12 */
13 module godot.jsonrpc;
14 import std.meta : AliasSeq, staticIndexOf;
15 import std.traits : Unqual;
16 import godot.d.traits;
17 import godot.core;
18 import godot.c;
19 import godot.d.bind;
20 import godot.d.reference;
21 import godot.globalenums;
22 import godot.object;
23 import godot.classdb;
24 /**
25 A helper to handle dictionaries which look like JSONRPC documents.
26 
27 $(D url=https://www.jsonrpc.org/)JSON-RPC$(D /url) is a standard which wraps a method call in a $(D JSON) object. The object has a particular structure and identifies which method is called, the parameters to that function, and carries an ID to keep track of responses. This class implements that standard on top of $(D Dictionary); you will have to convert between a $(D Dictionary) and $(D JSON) with other functions.
28 */
29 @GodotBaseClass struct JSONRPC
30 {
31 	package(godot) enum string _GODOT_internal_name = "JSONRPC";
32 public:
33 @nogc nothrow:
34 	union { /** */ godot_object _godot_object; /** */ GodotObject _GODOT_base; }
35 	alias _GODOT_base this;
36 	alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses);
37 	package(godot) __gshared bool _classBindingInitialized = false;
38 	package(godot) static struct GDNativeClassBinding
39 	{
40 		__gshared:
41 		@GodotName("make_notification") GodotMethod!(Dictionary, String, Variant) makeNotification;
42 		@GodotName("make_request") GodotMethod!(Dictionary, String, Variant, Variant) makeRequest;
43 		@GodotName("make_response") GodotMethod!(Dictionary, Variant, Variant) makeResponse;
44 		@GodotName("make_response_error") GodotMethod!(Dictionary, long, String, Variant) makeResponseError;
45 		@GodotName("process_action") GodotMethod!(Variant, Variant, bool) processAction;
46 		@GodotName("process_string") GodotMethod!(String, String) processString;
47 		@GodotName("set_scope") GodotMethod!(void, String, GodotObject) setScope;
48 	}
49 	/// 
50 	pragma(inline, true) bool opEquals(in JSONRPC other) const
51 	{ return _godot_object.ptr is other._godot_object.ptr; }
52 	/// 
53 	pragma(inline, true) typeof(null) opAssign(typeof(null) n)
54 	{ _godot_object.ptr = n; return null; }
55 	/// 
56 	pragma(inline, true) bool opEquals(typeof(null) n) const
57 	{ return _godot_object.ptr is n; }
58 	/// 
59 	size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; }
60 	mixin baseCasts;
61 	/// Construct a new instance of JSONRPC.
62 	/// Note: use `memnew!JSONRPC` instead.
63 	static JSONRPC _new()
64 	{
65 		static godot_class_constructor constructor;
66 		if(constructor is null) constructor = _godot_api.godot_get_class_constructor("JSONRPC");
67 		if(constructor is null) return typeof(this).init;
68 		return cast(JSONRPC)(constructor());
69 	}
70 	@disable new(size_t s);
71 	/// 
72 	enum ErrorCode : int
73 	{
74 		/**
75 		
76 		*/
77 		parseError = -32700,
78 		/**
79 		
80 		*/
81 		internalError = -32603,
82 		/**
83 		
84 		*/
85 		invalidParams = -32602,
86 		/**
87 		A method call was requested but no function of that name existed in the JSONRPC subclass.
88 		*/
89 		methodNotFound = -32601,
90 		/**
91 		
92 		*/
93 		invalidRequest = -32600,
94 	}
95 	/// 
96 	enum Constants : int
97 	{
98 		parseError = -32700,
99 		internalError = -32603,
100 		invalidParams = -32602,
101 		methodNotFound = -32601,
102 		invalidRequest = -32600,
103 	}
104 	/**
105 	Returns a dictionary in the form of a JSON-RPC notification. Notifications are one-shot messages which do not expect a response.
106 	- `method`: Name of the method being called.
107 	- `params`: An array or dictionary of parameters being passed to the method.
108 	*/
109 	Dictionary makeNotification(VariantArg1)(in String method, in VariantArg1 params)
110 	{
111 		checkClassBinding!(typeof(this))();
112 		return ptrcall!(Dictionary)(GDNativeClassBinding.makeNotification, _godot_object, method, params);
113 	}
114 	/**
115 	Returns a dictionary in the form of a JSON-RPC request. Requests are sent to a server with the expectation of a response. The ID field is used for the server to specify which exact request it is responding to.
116 	- `method`: Name of the method being called.
117 	- `params`: An array or dictionary of parameters being passed to the method.
118 	- `id`: Uniquely identifies this request. The server is expected to send a response with the same ID.
119 	*/
120 	Dictionary makeRequest(VariantArg1, VariantArg2)(in String method, in VariantArg1 params, in VariantArg2 id)
121 	{
122 		checkClassBinding!(typeof(this))();
123 		return ptrcall!(Dictionary)(GDNativeClassBinding.makeRequest, _godot_object, method, params, id);
124 	}
125 	/**
126 	When a server has received and processed a request, it is expected to send a response. If you did not want a response then you need to have sent a Notification instead.
127 	- `result`: The return value of the function which was called.
128 	- `id`: The ID of the request this response is targeted to.
129 	*/
130 	Dictionary makeResponse(VariantArg0, VariantArg1)(in VariantArg0 result, in VariantArg1 id)
131 	{
132 		checkClassBinding!(typeof(this))();
133 		return ptrcall!(Dictionary)(GDNativeClassBinding.makeResponse, _godot_object, result, id);
134 	}
135 	/**
136 	Creates a response which indicates a previous reply has failed in some way.
137 	- `code`: The error code corresponding to what kind of error this is. See the $(D errorcode) constants.
138 	- `message`: A custom message about this error.
139 	- `id`: The request this error is a response to.
140 	*/
141 	Dictionary makeResponseError(VariantArg2)(in long code, in String message, in VariantArg2 id = Variant.nil) const
142 	{
143 		checkClassBinding!(typeof(this))();
144 		return ptrcall!(Dictionary)(GDNativeClassBinding.makeResponseError, _godot_object, code, message, id);
145 	}
146 	/**
147 	Given a Dictionary which takes the form of a JSON-RPC request: unpack the request and run it. Methods are resolved by looking at the field called "method" and looking for an equivalently named function in the JSONRPC object. If one is found that method is called.
148 	To add new supported methods extend the JSONRPC class and call $(D processAction) on your subclass.
149 	`action`: The action to be run, as a Dictionary in the form of a JSON-RPC request or notification.
150 	*/
151 	Variant processAction(VariantArg0)(in VariantArg0 action, in bool recurse = false)
152 	{
153 		checkClassBinding!(typeof(this))();
154 		return ptrcall!(Variant)(GDNativeClassBinding.processAction, _godot_object, action, recurse);
155 	}
156 	/**
157 	
158 	*/
159 	String processString(in String action)
160 	{
161 		checkClassBinding!(typeof(this))();
162 		return ptrcall!(String)(GDNativeClassBinding.processString, _godot_object, action);
163 	}
164 	/**
165 	
166 	*/
167 	void setScope(in String _scope, GodotObject target)
168 	{
169 		checkClassBinding!(typeof(this))();
170 		ptrcall!(void)(GDNativeClassBinding.setScope, _godot_object, _scope, target);
171 	}
172 }