示例#1
0
    public static IEnumerator Post(string endpointUrl, string bodyJsonString, PostDelegate callback)
    {
        var request = new UnityWebRequest(baseUrl + endpointUrl, "POST");

        byte[] bodyRaw = Encoding.UTF8.GetBytes(bodyJsonString);
        request.uploadHandler   = (UploadHandler) new UploadHandlerRaw(bodyRaw);
        request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
        request.SetRequestHeader("Content-Type", "application/json");

        yield return(request.Send());

        Debug.Log("Status Code: " + request.responseCode);
        callback?.Invoke(request.responseCode.ToString());
    }
    private IEnumerator Posting(string action, object body, PostDelegate onComplete)
    {
        var jsonBody = JsonUtility.ToJson(body);

        using (var request = UnityWebRequest.Post(BaseUri + action, jsonBody))
        {
            request.SetRequestHeader("Content-Type", "application/json");
            byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonBody);
            request.uploadHandler = new UploadHandlerRaw(bodyRaw);
            request.SendWebRequest();
            while (!request.isDone)
            {
                yield return(null);
            }

            onComplete(request.isHttpError);
        }
    }
示例#3
0
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            try
            {
                PreDelegate?.Invoke(InnerSubjectName, binder, args);

                base.TryInvokeMember(binder, args, out result);

                PostDelegate?.Invoke(InnerSubjectName, binder, args);

                return(true);
            }
            catch (Exception e)
            {
                CatchDelegate?.Invoke(InnerSubjectName, binder, args, e);
                throw;
            }
        }
示例#4
0
        // *************** Asynchronous Post *********************
        public static void RestPostAsync <TR, TI>(string url, TI data,
                                                  RestCallBack <TR> callback, ClientConfiguration configuration)
        {
            var post = new PostDelegate <TR, TI>(RestPost <TR, TI>);

            post.BeginInvoke(url, data, configuration,
                             ar =>
            {
                var result  = (AsyncResult)ar;
                var del     = (PostDelegate <TR, TI>)result.AsyncDelegate;
                var value   = default(TR);
                Exception e = null;

                try { value = del.EndInvoke(result); }
                catch (Exception ex) { e = ex; }

                callback?.Invoke(e, value);
            }, null);
        }
        /// <summary>
        /// Create a connection to a Snowplow collector
        /// <param name="host">The hostname of the collector (not including the scheme, e.g. http://)</param>
        /// <param name="protocol">The protocol to use. HTTP or HTTPS</param>
        /// <param name="port">The port number the collector is listening on</param>
        /// <param name="method">The request method to use. GET or POST</param>
        /// <param name="postMethod">Internal use</param>
        /// <param name="getMethod">Internal use</param>
        /// <param name="byteLimitPost">The maximum amount of bytes we can expect to work</param>
        /// <param name="byteLimitGet">The maximum amount of bytes we can expect to work</param>
        /// <param name="l">Send log messages using this logger</param>
        /// </summary>
        public SnowplowHttpCollectorEndpoint(string host, HttpProtocol protocol = HttpProtocol.HTTP, int?port = null, HttpMethod method = HttpMethod.GET,
                                             PostDelegate postMethod            = null, GetDelegate getMethod = null, int byteLimitPost = 40000, int byteLimitGet = 40000, ILogger l = null)
        {
            if (Uri.IsWellFormedUriString(host, UriKind.Absolute))
            {
                var uri = new Uri(host);
                var endpointWithoutScheme = uri.Host;
                _collectorUri = getCollectorUri(endpointWithoutScheme, protocol, port, method);
            }
            else
            {
                _collectorUri = getCollectorUri(host, protocol, port, method);
            }

            _method        = method;
            _postMethod    = postMethod ?? DefaultPostMethod;
            _getMethod     = getMethod ?? DefaultGetMethod;
            _byteLimitPost = byteLimitPost;
            _byteLimitGet  = byteLimitGet;
            _logger        = l ?? new NoLogging();
        }
        // *************** Asynchronous Post *********************
        internal void RestPostAsync(string url, string data,
                                    RestCallBack <string> callback, ClientConfiguration configuration)
        {
            var post = new PostDelegate <string, string>(RestPost);

            post.BeginInvoke(url, data, configuration,
                             ar =>
            {
                var result  = (AsyncResult)ar;
                var del     = (PostDelegate <string, string>)result.AsyncDelegate;
                var value   = string.Empty;
                Exception e = null;

                try { value = del.EndInvoke(result); }
                catch (Exception ex) { e = ex; }

                if (callback != null)
                {
                    callback(e, value);
                }
            }, null);
        }
 public void Post(string action, object body, PostDelegate onComplete)
 {
     StartCoroutine(Posting(action, body, onComplete));
 }