Beispiel #1
0
        public async Task <PandaDocHttpResponse <PandaDocBearerToken> > Login(string username, string password)
        {
            if (username == null)
            {
                throw new ArgumentNullException("username");
            }
            if (password == null)
            {
                throw new ArgumentNullException("password");
            }

            var values = new Dictionary <string, string>
            {
                { "grant_type", "password" },
                { "username", username },
                { "password", password },
                { "client_id", settings.ClientId },
                { "client_secret", settings.ClientSecret },
                { "scope", "read write read+write" }
            };

            var content = new FormUrlEncodedContent(values);

            HttpResponseMessage httpResponse = await httpClient.PostAsync(settings.AuthUri + "oauth2/access_token", content);

            PandaDocHttpResponse <PandaDocBearerToken> response = await httpResponse.ToPandaDocResponseAsync <PandaDocBearerToken>();

            return(response);
        }
Beispiel #2
0
        public static async Task <PandaDocHttpResponse <T> > ToPandaDocResponseAsync <T>(this HttpResponseMessage httpResponse)
        {
            if (httpResponse == null)
            {
                throw new ArgumentNullException("httpResponse");
            }

            var responseContent = await httpResponse.Content.ReadAsStringAsync();

            var response = new PandaDocHttpResponse <T>
            {
                Content             = responseContent,
                IsSuccessStatusCode = httpResponse.IsSuccessStatusCode,
                StatusCode          = httpResponse.StatusCode,
                Headers             = httpResponse.Headers,
                HttpResponse        = httpResponse,
                Errors = new Dictionary <string, string>()
            };

            if (httpResponse.IsSuccessStatusCode)
            {
                response.Value = await httpResponse.Content.ReadAsAsync <T>();
            }
            else
            {
                ExtractErrors(responseContent, response);
            }

            return(response);
        }
Beispiel #3
0
        public async Task <PandaDocHttpResponse> DeleteDocument(string uuid)
        {
            HttpResponseMessage httpResponse = await httpClient.DeleteAsync(settings.ApiUri + "public/v1/documents/" + uuid);

            PandaDocHttpResponse response = await httpResponse.ToPandaDocResponseAsync();

            return(response);
        }
Beispiel #4
0
        public async Task <PandaDocHttpResponse <GetDocumentResponse> > GetDocument(string uuid)
        {
            var httpResponse = await httpClient.GetAsync(settings.ApiUri + "public/v1/documents/" + uuid);

            PandaDocHttpResponse <GetDocumentResponse> response = await httpResponse.ToPandaDocResponseAsync <GetDocumentResponse>();

            return(response);
        }
Beispiel #5
0
        public async Task <PandaDocHttpResponse <GetDocumentsResponse> > GetDocuments()
        {
            HttpResponseMessage httpResponse = await httpClient.GetAsync(settings.ApiUri + "public/v1/documents");

            PandaDocHttpResponse <GetDocumentsResponse> response = await httpResponse.ToPandaDocResponseAsync <GetDocumentsResponse>();

            return(response);
        }
Beispiel #6
0
        public async Task <PandaDocHttpResponse <ShareDocumentResponse> > ShareDocument(string uuid, ShareDocumentRequest request)
        {
            HttpContent httpContent = new ObjectContent <ShareDocumentRequest>(request, JsonFormatter);

            HttpResponseMessage httpResponse = await httpClient.PostAsync(settings.ApiUri + "public/v1/documents/" + uuid + "/session", httpContent);

            PandaDocHttpResponse <ShareDocumentResponse> response = await httpResponse.ToPandaDocResponseAsync <ShareDocumentResponse>();

            return(response);
        }
Beispiel #7
0
        public async Task <PandaDocHttpResponse <CreateDocumentResponse> > CreateDocument(CreateDocumentRequest request)
        {
            HttpContent httpContent = new ObjectContent <CreateDocumentRequest>(request, JsonFormatter);

            HttpResponseMessage httpResponse = await httpClient.PostAsync(settings.ApiUri + "public/v1/documents", httpContent);

            PandaDocHttpResponse <CreateDocumentResponse> response = await httpResponse.ToPandaDocResponseAsync <CreateDocumentResponse>();

            return(response);
        }
Beispiel #8
0
        private static void ExtractErrors(string responseContent, PandaDocHttpResponse response)
        {
            var data = JsonConvert.DeserializeObject <Dictionary <string, dynamic> >(responseContent);

            if (data.ContainsKey("type"))
            {
                var errorType = data["type"].ToString();

                if (data.ContainsKey("details"))
                {
                    var errorDetails = data["details"].ToString();
                    response.Errors.Add(errorType, errorDetails);
                }

                if (data.ContainsKey("detail"))
                {
                    var detail = data["detail"].ToString();
                    response.Errors.Add(errorType, detail);
                }
            }
        }