Esempio n. 1
0
        public void retrieveApiCatalogToEstablishOAuthProviderDetails(string endPoint)
        {
            //Hammock.Authentication.OAuth.OAuthCredentials credentials = createOAuthCredentials(OAuthType.ProtectedResource, null, null, null, null);

            Hammock.RestClient client = new Hammock.RestClient()
            {
                Authority = endPoint,
                //Credentials = credentials
            };

            client.AddHeader("Accept", "application/json");
            client.AddHeader("authorization", $"Basic {GetBase64EncodedClientCredentials()}");

            Hammock.RestRequest request = new Hammock.RestRequest()
            {
                Path = ""
            };

            request.AddHeader("Accept", "application/vnd.deere.axiom.v3+json");
            using (Hammock.RestResponse response = client.Request(request))
            {
                //Logger.Log(response);
                MemoryStream stream1           = new MemoryStream();
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ApiCatalog));

                stream1.Position = 0;
                ApiCatalog apiCatalog = (ApiCatalog)ser.ReadObject(response.ContentStream);

                links = linksFrom(apiCatalog);
            }
        }
        public void uploadFile()
        {
            SampleApp.Sources.generated.v3.File apiFile = new SampleApp.Sources.generated.v3.File();
            apiFile.name = "greatFileFromBrian.zip";

            Hammock.RestClient client = getRestClient();

            Hammock.RestRequest request = new Hammock.RestRequest()
            {
                Path   = fileUploadLink.uri,
                Method = WebMethod.Put
            };

            request.AddHeader("Accept", "application/vnd.deere.axiom.v3+json");
            request.AddHeader("Content-Type", "application/zip");
            MemoryStream ms = Serialize1 <SampleApp.Sources.generated.v3.File>(apiFile);

            request.AddPostContent(ms.ToArray());
            Hammock.RestResponse response = client.Request(request);


            /*final RestRequest fileUploadRequest = oauthRequestTo(newFileLocation)
             *      .method("PUT")
             *      .addHeader(new HttpHeader("Accept", "application/vnd.deere.axiom.v3+json"))
             *      .addHeader(new HttpHeader("Content-Type", "application/zip"))
             *      .body(Files.newInputStreamSupplier(findFile("wdtTestFile.zip")))
             *      .build();
             *
             * final RestResponse fileUploadResponse = fileUploadRequest.fetchResponse();
             * checkThat("PUT Response", fileUploadResponse.getResponseCode(), isEqualTo(NO_CONTENT));*/
        }
        private int makeHeadRequestToGetFileSize()
        {
            Hammock.Authentication.OAuth.OAuthCredentials credentials = OAuthWorkFlow.createOAuthCredentials(OAuthType.ProtectedResource, ApiCredentials.TOKEN.token,
                                                                                                             ApiCredentials.TOKEN.secret, null, null);


            Hammock.RestClient client = new Hammock.RestClient()
            {
                Authority   = "",
                Credentials = credentials
            };

            Hammock.RestRequest request = new Hammock.RestRequest()
            {
                Path   = firstFileSelfUri,
                Method = WebMethod.Head
            };

            request.AddHeader("Accept", "application/zip");
            Hammock.RestResponse response = client.Request(request);

            /*if (!hasResponseCode(OK).matches(headRes)) {
             *  firstFileSelfUri = null;
             *  //fail(format("HEAD request to %s returned bad response code", firstFileSelfUri));
             * }*/
            //checkThat("Content-Length header", headRes.getHeaderFields().contains("Content-Length"), isTrue());
            return(Convert.ToInt32(response.Headers["Content-Length"]));
            //return Integer.valueOf(headRes.getHeaderFields().valueOf("Content-Length"));
        }
Esempio n. 4
0
        public void getRequestToken()
        {
            Hammock.Authentication.OAuth.OAuthCredentials credentials = createOAuthCredentials(OAuthType.RequestToken, null, null, null, "oob");


            Hammock.RestClient client = new Hammock.RestClient()
            {
                Authority   = "",
                Credentials = credentials
            };

            Hammock.RestRequest request = new Hammock.RestRequest()
            {
                Path = links["oauthRequestToken"].uri
            };

            Hammock.RestResponse response = client.Request(request);

            reqToken = response.Content.Split('&')[0];


            authUri   = cleanAuthorizationUri(links["oauthAuthorizeRequestToken"].uri) + "?" + reqToken;
            reqToken  = reqToken.Split('=')[1];
            reqSecret = response.Content.Split('&')[1].Split('=')[1];
        }
        public void retrieveFleetDetails()
        {
            OAuthCredentials credentials = OAuthWorkFlow.createOAuthCredentials(OAuthType.ProtectedResource, ApiCredentials.TOKEN.token,
                                                                                ApiCredentials.TOKEN.secret, null, null);


            Hammock.RestClient client = new Hammock.RestClient()
            {
                Authority   = "",
                Credentials = credentials
            };

            Hammock.RestRequest request = new Hammock.RestRequest()
            {
                Path = BASE_URI + "Fleet"
            };

            request.AddHeader("Accept", "application/xml");

            Hammock.RestResponse response = client.Request(request);
            request.Path = BASE_URI + response.Headers.Get("Location");

            System.Diagnostics.Debug.WriteLine("");

            makeRecursiveApiCallTillLastPageOfApiResponse(request, credentials, client);
        }
Esempio n. 6
0
        public void retrieveApiCatalogToEstablishOAuthProviderDetails()
        {
            Hammock.Authentication.OAuth.OAuthCredentials credentials = createOAuthCredentials(OAuthType.ProtectedResource, null, null, null, null);

            Hammock.RestClient client = new Hammock.RestClient()
            {
                Authority   = "https://apicert.soa-proxy.deere.com/platform/",
                Credentials = credentials
            };

            Hammock.RestRequest request = new Hammock.RestRequest()
            {
                Path = ""
            };

            request.AddHeader("Accept", "application/vnd.deere.axiom.v3+json");
            Hammock.RestResponse response = client.Request(request);

            MemoryStream stream1           = new MemoryStream();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ApiCatalog));

            stream1.Position = 0;
            ApiCatalog apiCatalog = (ApiCatalog)ser.ReadObject(response.ContentStream);

            links = linksFrom(apiCatalog);
        }
        public void retrieveMetadataForFile()
        {
            Dictionary <String, Link> linksFromFirstFile = OAuthWorkFlow.linksFrom(files.page[3]);

            firstFileSelfUri = linksFromFirstFile["self"].uri;

            Hammock.Authentication.OAuth.OAuthCredentials credentials = OAuthWorkFlow.createOAuthCredentials(OAuthType.ProtectedResource, ApiCredentials.TOKEN.token,
                                                                                                             ApiCredentials.TOKEN.secret, null, null);


            Hammock.RestClient client = new Hammock.RestClient()
            {
                Authority   = "",
                Credentials = credentials
            };

            Hammock.RestRequest request = new Hammock.RestRequest()
            {
                Path = firstFileSelfUri
            };

            request.AddHeader("Accept", "application/vnd.deere.axiom.v3+json");
            Hammock.RestResponse response = client.Request(request);

            SampleApp.Sources.generated.v3.File firstFileDetails = Deserialise <SampleApp.Sources.generated.v3.File>(response.ContentStream);

            filename = firstFileDetails.name;
        }
        public void retrieveApiCatalog()
        {
            Hammock.Authentication.OAuth.OAuthCredentials credentials = OAuthWorkFlow.createOAuthCredentials(OAuthType.ProtectedResource, ApiCredentials.TOKEN.token,
                                                                                                             ApiCredentials.TOKEN.secret, null, null);


            Hammock.RestClient client = new Hammock.RestClient()
            {
                Authority   = "",
                Credentials = credentials
            };

            Hammock.RestRequest request = new Hammock.RestRequest()
            {
                Path = "https://apicert.soa-proxy.deere.com/platform/"
            };

            request.AddHeader("Accept", "application/vnd.deere.axiom.v3+json");
            Hammock.RestResponse response = client.Request(request);

            ApiCatalog apiCatalog = Deserialise <ApiCatalog>(response.ContentStream);

            links = OAuthWorkFlow.linksFrom(apiCatalog);

            getFiles();

            retrieveMetadataForFile();

            downloadFileContentsAndComputeMd5();
            downloadFileInPiecesAndComputeMd5();
        }
        public void getFiles()
        {
            Hammock.Authentication.OAuth.OAuthCredentials credentials = OAuthWorkFlow.createOAuthCredentials(OAuthType.ProtectedResource, ApiCredentials.TOKEN.token,
                                                                                                             ApiCredentials.TOKEN.secret, null, null);


            Hammock.RestClient client = new Hammock.RestClient()
            {
                Authority   = "",
                Credentials = credentials
            };

            Hammock.RestRequest request = new Hammock.RestRequest()
            {
                Path = links["files"].uri
            };

            request.AddHeader("Accept", "application/vnd.deere.axiom.v3+json");
            Hammock.RestResponse response = client.Request(request);

            CollectionPageDeserializer ds = new CollectionPageDeserializer();

            files = ds.deserialize <SampleApp.Sources.generated.v3.File>(response.Content);

            Console.WriteLine("done");
        }
        public void downloadFileContentsAndComputeMd5()
        {
            Hammock.Authentication.OAuth.OAuthCredentials credentials = OAuthWorkFlow.createOAuthCredentials(OAuthType.ProtectedResource, ApiCredentials.TOKEN.token,
                                                                                                             ApiCredentials.TOKEN.secret, null, null);


            Hammock.RestClient client = new Hammock.RestClient()
            {
                Authority   = "",
                Credentials = credentials
            };

            Hammock.RestRequest request = new Hammock.RestRequest()
            {
                Path = firstFileSelfUri
            };

            request.AddHeader("Accept", "application/zip");
            Hammock.RestResponse response = client.Request(request);


            checkFilenameInContentDispositionHeader(response);

            using (var md5 = MD5.Create())
            {
                using (var stream = response.ContentStream)
                {
                    md5FromSinglePieceDownload = md5.ComputeHash(stream);
                }
            }
        }
 private void onLoaded(Hammock.RestRequest request, Hammock.RestResponse response, object userState)
 {
     if (response != null)
     {
         if (CurrentArgs != null)
         {
             var juser = JsonConvert.DeserializeObject <TwitterUtils.FollowerList>(response.Content);
             if (juser != null && juser.ids.Count > 0)
             {
                 for (int f = 0; f < juser.ids.Count; f++)
                 {
                     CurrentArgs.FriendIds.Add(juser.ids[f]);
                 }
             }
             Done(CurrentArgs);
             CurrentArgs = null;
         }
     }
     else
     {
         if (CurrentArgs != null)
         {
             Cancel(CurrentArgs);
             CurrentArgs = null;
         }
     }
 }
Esempio n. 12
0
 private void PostTweetRequestCallback(Hammock.RestRequest request, Hammock.RestResponse response, object obj)
 {
     Deployment.Current.Dispatcher.BeginInvoke(() =>
     {
         if (response.StatusCode == HttpStatusCode.OK)
         {
             string toastMessage = "TWEET_POSTED_SUCCESSFULLY";
             ShellToast toast    = new ShellToast();
             toast.Title         = "Background Agent Sample";
             toast.Content       = toastMessage;
             toast.Show();
             completedUpdates++;
             if (completedUpdates == scheduledUpdates)
             {
                 notify();
             }
             //MessageBox.Show("TWEET_POSTED_SUCCESSFULLY");
         }
         else if (response.StatusCode == HttpStatusCode.Forbidden)
         {
             string toastMessage = "TWEET_POST_ERR_UPDATE_LIMIT";
             ShellToast toast    = new ShellToast();
             toast.Title         = "Background Agent Sample";
             toast.Content       = toastMessage;
             toast.Show();
             completedUpdates++;
             if (completedUpdates == scheduledUpdates)
             {
                 notify();
             }
             //MessageBox.Show("TWEET_POST_ERR_UPDATE_LIMIT");
         }
         else
         {
             string toastMessage = "TWEET_POST_ERR_FAILED";
             ShellToast toast    = new ShellToast();
             toast.Title         = "Background Agent Sample";
             toast.Content       = toastMessage;
             toast.Show();
             completedUpdates++;
             if (completedUpdates == scheduledUpdates)
             {
                 notify();
             }
             //MessageBox.Show("TWEET_POST_ERR_FAILED");
         }
     });
 }
        public void getCurrentUser()
        {
            Hammock.RestClient client = getRestClient();

            Hammock.RestRequest request = new Hammock.RestRequest()
            {
                Path = links["currentUser"].uri
            };

            request.AddHeader("Accept", "application/vnd.deere.axiom.v3+json");
            Hammock.RestResponse response = client.Request(request);

            Resource currentUser = Download.Deserialise <User>(response.ContentStream);

            userOrganizations = OAuthWorkFlow.linksFrom(currentUser)["organizations"].uri;
        }
        public void makeRecursiveApiCallTillLastPageOfApiResponse(Hammock.RestRequest request, OAuthCredentials credentials, Hammock.RestClient client)
        {
            Hammock.RestResponse response = client.Request(request);
            Fleet fleet = Deserialise <Fleet>(response.ContentStream);

            printFirstEquipmentDetailsFromEachPage(fleet);
            List <Links> links = fleet.Links;

            for (int i = 0; i < links.Count; i++)
            {
                if (links[i].Rel.Equals("next"))
                {
                    request.Path = links[i].Href;
                    makeRecursiveApiCallTillLastPageOfApiResponse(request, credentials, client);
                }
            }
        }
        private void getChunkFromStartAndRecurse(int start, int chunkSize, int fileSize
                                                 //,DigestOutputStream byteDigest
                                                 )
        {
            int maxRange = fileSize - 1;
            int end      = Math.Min(start + chunkSize, maxRange);

            Hammock.Authentication.OAuth.OAuthCredentials credentials = OAuthWorkFlow.createOAuthCredentials(OAuthType.ProtectedResource, ApiCredentials.TOKEN.token,
                                                                                                             ApiCredentials.TOKEN.secret, null, null);


            Hammock.RestClient client = new Hammock.RestClient()
            {
                Authority   = "",
                Credentials = credentials
            };

            Hammock.RestRequest request = new Hammock.RestRequest()
            {
                Path   = firstFileSelfUri,
                Method = WebMethod.Get
            };

            request.AddHeader("Accept", "application/zip");
            request.AddHeader("Range", "bytes=" + start + "-" + end);
            Hammock.RestResponse response = client.Request(request);


            using (var md5 = MD5.Create())
            {
                using (var stream = response.ContentStream)
                {
                    md5FromMultiplePieceDownload = md5.ComputeHash(stream);
                }
            }

            checkFilenameInContentDispositionHeader(response);

            // copy(rangeResponse.getBody(), byteDigest);

            if (start + chunkSize < maxRange)
            {
                getChunkFromStartAndRecurse(start + chunkSize + 1, chunkSize, fileSize);
            }
        }
        public void exchangeRequestTokenForAccessToken()
        {
            Hammock.Authentication.OAuth.OAuthCredentials credentials = createOAuthCredentials(OAuthType.AccessToken, reqToken, HttpUtility.UrlDecode(reqSecret), verifier, null);

            Hammock.RestClient client = new Hammock.RestClient()
            {
                Authority = "",
                Credentials = credentials
            };

            Hammock.RestRequest request = new Hammock.RestRequest()
            {
                Path = links["oauthAccessToken"].uri
            };

            Hammock.RestResponse response = client.Request(request);

            Console.WriteLine("Token:" + response.Content.Split('&')[0].Split('=')[1] + " \n Token Secret:" + response.Content.Split('&')[1].Split('=')[1]);
        }
Esempio n. 17
0
        public void exchangeRequestTokenForAccessToken()
        {
            Hammock.Authentication.OAuth.OAuthCredentials credentials = createOAuthCredentials(OAuthType.AccessToken, reqToken, HttpUtility.UrlDecode(reqSecret), verifier, null);


            Hammock.RestClient client = new Hammock.RestClient()
            {
                Authority   = "",
                Credentials = credentials
            };

            Hammock.RestRequest request = new Hammock.RestRequest()
            {
                Path = links["oauthAccessToken"].uri
            };

            Hammock.RestResponse response = client.Request(request);

            Console.WriteLine("Token:" + response.Content.Split('&')[0].Split('=')[1] + " \n Token Secret:" + response.Content.Split('&')[1].Split('=')[1]);
        }
        public void getUserOrganizations()
        {
            Hammock.RestClient client = getRestClient();

            Hammock.RestRequest request = new Hammock.RestRequest()
            {
                Path = userOrganizations
            };

            request.AddHeader("Accept", "application/vnd.deere.axiom.v3+json");
            Hammock.RestResponse response = client.Request(request);

            CollectionPageDeserializer ds = new CollectionPageDeserializer();

            CollectionPage <Organization> organizations = ds.deserialize <SampleApp.Sources.generated.v3.Organization>(response.Content);


            Dictionary <String, Link> linksFromFirst = OAuthWorkFlow.linksFrom(organizations.page[0]);

            fileUploadLink = linksFromFirst["uploadFile"];
        }
        public void deleteUploadedFile()
        {
            SampleApp.Sources.generated.v3.File apiFile = new SampleApp.Sources.generated.v3.File();
            Hammock.RestClient client = getRestClient();

            Hammock.RestRequest request = new Hammock.RestRequest()
            {
                Path   = newFileLocation,
                Method = WebMethod.Delete
            };

            request.AddHeader("Accept", "application/vnd.deere.axiom.v3+json");
            Hammock.RestResponse response = client.Request(request);

            /*  final RestRequest deleteFileRequest = oauthRequestTo(newFileLocation)
             *        .method("DELETE")
             *        .addHeader(new HttpHeader("Accept", "application/vnd.deere.axiom.v3+json"))
             *        .build();
             *
             * final RestResponse deleteResponse = deleteFileRequest.fetchResponse();
             * checkThat("DELETE Response", deleteResponse.getResponseCode(), isEqualTo(NO_CONTENT));*/
        }
Esempio n. 20
0
        public void getRequestToken()
        {
            Hammock.Authentication.OAuth.OAuthCredentials credentials = createOAuthCredentials(OAuthType.RequestToken, null, null, null,
                                                                                               "https://developer.deere.com/oauth/auz/grants/provider/authcomplete");

            Hammock.RestClient client = new Hammock.RestClient()
            {
                Authority   = "",
                Credentials = credentials
            };

            Hammock.RestRequest request = new Hammock.RestRequest()
            {
                Path = links["oauthRequestToken"].uri
            };

            Hammock.RestResponse response = client.Request(request);
            reqToken  = response.Content.Split('&')[0];
            authUri   = links["oauthAuthorizeRequestToken"].uri + "?" + reqToken;
            reqToken  = reqToken.Split('=')[1];
            reqSecret = response.Content.Split('&')[1].Split('=')[1];
        }
Esempio n. 21
0
        // Twitter ------------------------------------------------------------------------------------------------->>

        public void postOnTwitter(string status, string twtoken, string twsecret)
        {
            string     toastMessage = "TW Started";
            ShellToast toast        = new ShellToast();

            toast.Title   = ".";
            toast.Content = toastMessage;
            toast.Show();
            scheduledUpdates++;
            var credentials = new OAuthCredentials
            {
                Type              = OAuthType.ProtectedResource,
                SignatureMethod   = OAuthSignatureMethod.HmacSha1,
                ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
                ConsumerKey       = AppSettings.consumerKey,
                ConsumerSecret    = AppSettings.consumerKeySecret,
                Token             = twtoken,
                TokenSecret       = twsecret,
                Version           = "1.0"
            };

            var restClient = new Hammock.RestClient
            {
                Authority = "https://api.twitter.com",
                HasElevatedPermissions = true
            };

            var restRequest = new Hammock.RestRequest
            {
                Credentials = credentials,
                Path        = "/1.1/statuses/update.json",
                Method      = WebMethod.Post
            };
            Random rd = new Random();

            statusstring = rd.Next(10000).ToString() + rd.Next(100000).ToString();
            restRequest.AddParameter("status", status);
            restClient.BeginRequest(restRequest, new Hammock.RestCallback(PostTweetRequestCallback));
        }
        public void retrieveApiCatalog()
        {
            Hammock.RestClient client = getRestClient();

            Hammock.RestRequest request = new Hammock.RestRequest()
            {
                Path = "https://apicert.soa-proxy.deere.com/platform/"
            };

            request.AddHeader("Accept", "application/vnd.deere.axiom.v3+json");
            Hammock.RestResponse response = client.Request(request);

            ApiCatalog apiCatalog = Download.Deserialise <ApiCatalog>(response.ContentStream);

            links = OAuthWorkFlow.linksFrom(apiCatalog);


            getCurrentUser();
            getUserOrganizations();
            addFile();
            uploadFile();
            deleteUploadedFile();
        }
        public void addFile()
        {
            SampleApp.Sources.generated.v3.File apiFile = new SampleApp.Sources.generated.v3.File();
            apiFile.name = "greatFileFromBrian.zip";

            Hammock.RestClient client = getRestClient();

            Hammock.RestRequest request = new Hammock.RestRequest()
            {
                Path   = fileUploadLink.uri,
                Method = WebMethod.Post
            };

            request.AddHeader("Accept", "application/vnd.deere.axiom.v3+json");
            request.AddHeader("Content-Type", "application/vnd.deere.axiom.v3+json");

            String s = JsonConvert.SerializeObject(apiFile);

            request.AddPostContent(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(apiFile)));
            Hammock.RestResponse response = client.Request(request);

            newFileLocation = response.Headers["Location"];
        }
        public void retrieveApiCatalogToEstablishOAuthProviderDetails()
        {
            Hammock.Authentication.OAuth.OAuthCredentials credentials = createOAuthCredentials(OAuthType.ProtectedResource, null, null, null, null);

            Hammock.RestClient client = new Hammock.RestClient()
            {
                Authority = "https://apicert.soa-proxy.deere.com/platform/",
                Credentials = credentials
            };

            Hammock.RestRequest request = new Hammock.RestRequest()
            {
                Path = ""
            };

            request.AddHeader("Accept", "application/vnd.deere.axiom.v3+json");
            Hammock.RestResponse response = client.Request(request);

            MemoryStream stream1 = new MemoryStream();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ApiCatalog));

            stream1.Position = 0;
            ApiCatalog apiCatalog = (ApiCatalog)ser.ReadObject(response.ContentStream);

            links = linksFrom(apiCatalog);
        }
        public void getRequestToken()
        {
            Hammock.Authentication.OAuth.OAuthCredentials credentials = createOAuthCredentials(OAuthType.RequestToken, null, null, null, "oob");

             Hammock.RestClient client = new Hammock.RestClient()
            {
                Authority = "",
                Credentials = credentials
            };

            Hammock.RestRequest request = new Hammock.RestRequest()
            {
                Path = links["oauthRequestToken"].uri
            };

            Hammock.RestResponse response = client.Request(request);

            reqToken = response.Content.Split('&')[0];

            authUri = cleanAuthorizationUri(links["oauthAuthorizeRequestToken"].uri) + "?" + reqToken;
            reqToken =reqToken.Split('=')[1];
            reqSecret = response.Content.Split('&')[1].Split('=')[1];
        }