Example #1
0
        public dynamic DoRequest(string method,
                                 string endPoint,
                                 SortedDictionary <string, string> formData = null)
        {
            if (formData == null)
            {
                formData = new SortedDictionary <string, string>();
            }

            var request = WebRequest.Create(m_session.ApiUrl + endPoint);

            request.Method = method;
            request.Headers.Add("Device", m_session.DeviceData);
            if (method.Equals("POST") || method.Equals("PUT") || method.Equals("DELETE"))
            {
                request.ContentType = "application/x-www-form-urlencoded";
                var postDataAsBytes = MySession.PostDataAsByteArray(formData);
                request.ContentLength = postDataAsBytes.Length;
                var authorization = m_session.Authorization(method, endPoint, formData);
                request.Headers.Add("Authorization", authorization);
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(postDataAsBytes, 0, postDataAsBytes.Length);
                }
            }
            else
            {
                var authorization = m_session.Authorization(method, endPoint, formData);
                request.Headers.Add("Authorization", authorization);
            }

            // Finally lets do the request
            var responseData = DoRequestToJSON(request);

            if (!ResponseOk(responseData))
            {
                throw new SmartVisFaceException("Failed to perform request: " + responseData["message"]);
            }

            if (((IDictionary <String, object>)responseData).ContainsKey("task"))
            {
                return(responseData["task"]);
            }
            return(responseData["data"]);
        }
Example #2
0
        public dynamic DoFaceLogImage(SortedDictionary <string, string> formData = null, string imagePath = null, bool waitForTask = true)
        {
            if (formData == null)
            {
                formData = new SortedDictionary <string, string>();
            }
            string method   = "POST";
            string endPoint = "/face_log_image";

            string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");

            string formdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";
            Stream memStream        = new System.IO.MemoryStream();

            if (formData != null)
            {
                foreach (string key in formData.Keys)
                {
                    string formitem      = string.Format(formdataTemplate, key, formData[key]);
                    byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
                    memStream.Write(formitembytes, 0, formitembytes.Length);
                }
            }

            var postDataAsBytes = MySession.PostDataAsByteArray(formData);
            var request         = (HttpWebRequest)WebRequest.Create(m_session.ApiUrl + endPoint);

            request.Method    = method;
            request.KeepAlive = true;
            request.Headers.Add("Device", m_session.DeviceData);
            var authorization = m_session.Authorization("POST", endPoint, formData);

            request.Headers.Add("Authorization", authorization);
            request.ContentType = "multipart/form-data; boundary=" + boundary;

            // Do we have to post an image
            var boundarybytes    = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
            var endBoundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--");


            string headerTemplate =
                "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
                "Content-Type: application/octet-stream\r\n\r\n";

            memStream.Write(boundarybytes, 0, boundarybytes.Length);
            var header      = string.Format(headerTemplate, "image", "image.jpg");
            var headerbytes = System.Text.Encoding.UTF8.GetBytes(header);

            memStream.Write(headerbytes, 0, headerbytes.Length);

            using (var fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
            {
                var buffer    = new byte[1024];
                var bytesRead = 0;
                while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                {
                    memStream.Write(buffer, 0, bytesRead);
                }
            }

            memStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
            request.ContentLength = memStream.Length;

            using (Stream requestStream = request.GetRequestStream())
            {
                memStream.Position = 0;
                byte[] tempBuffer = new byte[memStream.Length];
                memStream.Read(tempBuffer, 0, tempBuffer.Length);
                memStream.Close();
                requestStream.Write(tempBuffer, 0, tempBuffer.Length);
            }

            // Finally lets do the request
            var responseData = DoRequestToJSON(request);

            if (!ResponseOk(responseData))
            {
                throw new SmartVisFaceException("Failed to perform request: " + responseData["message"]);
            }

            var taskData = responseData["task"];

            if (waitForTask)
            {
                var taskId = responseData["task"]["job_id"];
                taskData = WaitForTask(taskId, endPoint);
            }
            return(taskData);
        }