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");
        }
        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"));
        }
        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);
                }
            }
        }
        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);
        }
        private Hammock.RestClient getRestClient()
        {
            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
            };
            return(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);
            }
        }