Ejemplo n.º 1
0
        public static void AddComment(string checkinId, string text, Action success, Action <Exception> failure)
        {
            var client = (new FourSquareWebClient()).GetWrappedClientTemporary();
            var uuri   = FourSquareWebClient.BuildFourSquareUri(
                "checkins/" + checkinId + "/addcomment",
                GeoMethodType.None,
                "text",
                text);
            // real app will... %20%26%20 for space & space ( & )
            var uri    = uuri.Uri;
            var newUri = FourSquareWebClient.CreateServiceRequest(uri, true);

            client.UploadStringCompleted += (x, xe) =>
                                            //client.DownloadStringCompleted += (x, xe) =>
            {
                Exception e = null;
                if (xe.Error != null)
                {
                    e = xe.Error;
                }
                else
                {
                    string rs = xe.Result;
                    try
                    {
                        var json = FourSquareDataLoaderBase <LoadContext> .ProcessMetaAndNotificationsReturnJson(rs);
                    }
                    catch (Exception ee)
                    {
                        e = new UserIntendedException("There was a problem adding your comment, please try again later.", ee);
                    }
                }
                client = null;

                // Result now if there is not a photo.
                if (e != null)
                {
                    failure(e);
                }
                else
                {
                    success();
                }
            };

            // POST request.
            client.UploadStringAsync(newUri, string.Empty);
            //client.DownloadStringAsyncWithPost(newUri, string.Empty);
        }
        private void ExecuteOffThread()
        {
            var client = (new FourSquareWebClient()).GetWrappedClientTemporary();
            var newUri = FourSquareWebClient.CreateServiceRequest(Uri, UseCredentials);

            //client.DownloadStringCompleted += DownloadStringCompleted;

            // 1. Try posting string.
            // 2. Try multipart post.
            // 3. Regular get request.

            if (PostString != null)
            {
                client.UploadStringCompleted += client_UploadStringCompleted;
                client.UploadStringAsync(newUri, PostString);
                //client.DownloadStringAsyncWithPost(r, PostString);
            }
            else if (PostBytes != null && PostBytes.Length > 0)
            {
                // TODO: PHOTO UPLOAD: Add aprams for NAME (photo) and FILENAME
                string uploadFilename = "image.jpg";

                string boundary = Guid.NewGuid().ToString();
                string mpt      = "multipart/form-data; boundary=" + boundary;

                var ms = new MemoryStream();

                byte[] headerBytes    = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
                byte[] footerBytes    = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
                string keyValueFormat = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";

                if (PostParameters != null)
                {
                    foreach (string key in PostParameters.Keys)
                    {
                        string formitem = string.Format(
                            CultureInfo.InvariantCulture,
                            keyValueFormat, key, PostParameters[key]);
                        byte[] formitembytes = Encoding.UTF8.GetBytes(formitem);
                        ms.Write(formitembytes, 0, formitembytes.Length);
                    }
                }
                ms.Write(headerBytes, 0, headerBytes.Length);

                string hdt = string.Format(
                    CultureInfo.InvariantCulture,
                    "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n",
                    "photo",
                    uploadFilename);
                byte[] hb = Encoding.UTF8.GetBytes(hdt);
                ms.Write(hb, 0, hb.Length);
                ms.Write(PostBytes, 0, PostBytes.Length);

                // this worked when i wrote only header bytes length!

                ms.Write(footerBytes, 0, footerBytes.Length);

                byte[] finalData = ms.ToArray();
                ms.Close();

                var legacyClient = new LegacyWebClient();
                var r            = legacyClient.CreateServiceRequest(Uri, UseCredentials);
                legacyClient.DownloadStringCompleted += OnLegacyClientDownloadStringCompleted;
                legacyClient.DownloadStringAsyncWithPost(r, finalData, mpt);
            }
            else
            {
                client.DownloadStringCompleted += DownloadStringCompleted;
                client.DownloadStringAsync(newUri);
            }
        }
Ejemplo n.º 3
0
        // DESIGN: Not a very good place for a web service call like this.
        public static void AddNewTip(string venueId, string tipText, Stream photo, Action <Tip, Exception> result)
        {
            // NOTE: Official API supports an extra "url" parameter to associate
            var client = (new FourSquareWebClient()).GetWrappedClientTemporary();
            var uuri   = FourSquareWebClient.BuildFourSquareUri(
                "tips/add",
                GeoMethodType.None,
                "venueId",
                venueId,
                "text",
                tipText);
            Uri uri    = uuri.Uri;
            var newUri = FourSquareWebClient.CreateServiceRequest(uri, true);

            client.UploadStringCompleted += (x, xe) =>
            {
                Exception e = null;
                Tip       t = null;

                if (xe.Error != null)
                {
                    e = xe.Error;
                }
                else
                {
                    string rs = xe.Result;
                    try
                    {
                        var json = FourSquareDataLoaderBase <LoadContext> .ProcessMetaAndNotificationsReturnJson(rs);

                        Tip tip = Tip.ParseJson(json["tip"], typeof(Venue), venueId);

                        if (photo != null)
                        {
                            // Result comes after the photo upload.
                            var req = Model.PhotoAddLoadContext.AddPhotoToTip(tip.TipId, false, false);
                            req.SetPhotoBytes(photo);

                            FourSquare.Instance.AddPhoto(req,

                                                         (pic) =>
                            {
                                // that's it..
                                //RefreshVenue(null);
                                result(tip, null);
                            },

                                                         (fail) =>
                            {
                                result(null, fail);
                            });
                        }
                    }
                    catch (Exception ee)
                    {
                        e = new UserIntendedException("There was a problem adding the tip, please try again later.", ee);
                    }
                }
                client = null;

                // Result now if there is not a photo.
                if (photo == null)
                {
                    result(t, e);
                }
            };

            // POST request.
            client.UploadStringAsync(newUri, string.Empty);
            //client.DownloadStringAsyncWithPost(r, string.Empty);
        }