<commit_before>﻿
using BackupAsana.DTOs;
using BackupAsana.ExportDTOs;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SimplHelpers;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace BackupAsana
{
    public class AsanaBackup
    {
        string userToken;
        string baseDirectory;
        bool overrideExistingFiles;

        public AsanaBackup(string userAuthToken, string _baseDirectory, bool _overrideExistingFiles)
        {
            this.userToken = "Bearer " +  userAuthToken;
            baseDirectory = _baseDirectory;
            this.overrideExistingFiles = _overrideExistingFiles;
            if (!Directory.Exists(baseDirectory))
                Directory.CreateDirectory(baseDirectory);
        }

        class MakeWebRequestResDTO
        {
            public int StatusCode { get; set; }
            public string Content { get; set; }
        }

        async Task<MakeWebRequestResDTO> MakeWebRequestAsync(string url)
        {
            try
            {
                //TODO: send token from the currentAPI user
                var request = (HttpWebRequest)WebRequest.Create(url);
                request.Headers.Add("Authorization", userToken);
                request.Method = "GET";
                request.ContentType = "application/x-www-form-urlencoded";
                
                using (var response = (HttpWebResponse)(await request.GetResponseAsync()))
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                        {
                            return new MakeWebRequestResDTO
                            {
                                StatusCode = 200,
                                Content = reader.ReadToEnd()
                            };
                        }
                    }
                    return new MakeWebRequestResDTO
                    {
                        StatusCode = (int)response.StatusCode
                        
                    };
                }
            }
            catch (Exception e)
            {

            }

            return new MakeWebRequestResDTO
            {
                StatusCode = 500
            };
            
        }


        public async Task BackupProjects(long? workspaceID)
        {
            var url = "https://app.asana.com/api/1.0/projects/";
            if (workspaceID.HasValue)
                url = url.AddQueryParam("workspace", workspaceID.ToString());

            var projectsResponse = await MakeWebRequestAsync(url);

            if (projectsResponse.StatusCode != 200)
                return;

            var projectsFilePath = Path.Combine(baseDirectory, "projects.json");

            if (File.Exists(projectsFilePath))
                File.Delete(projectsFilePath);

            File.WriteAllText(projectsFilePath, projectsResponse.Content);

            var projects = JsonConvert.DeserializeObject<IdNameValuesHolder>(projectsResponse.Content);

            foreach (var project in projects.Data)<commit_msg>Added users backup<commit_after>﻿
using BackupAsana.DTOs;
using BackupAsana.ExportDTOs;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SimplHelpers;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace BackupAsana
{
    public class AsanaBackup
    {
        string userToken;
        string baseDirectory;
        bool overrideExistingFiles;

        public AsanaBackup(string userAuthToken, string _baseDirectory, bool _overrideExistingFiles)
        {
            this.userToken = "Bearer " +  userAuthToken;
            baseDirectory = _baseDirectory;
            this.overrideExistingFiles = _overrideExistingFiles;
            if (!Directory.Exists(baseDirectory))
                Directory.CreateDirectory(baseDirectory);
        }

        class MakeWebRequestResDTO
        {
            public int StatusCode { get; set; }
            public string Content { get; set; }
        }

        async Task<MakeWebRequestResDTO> MakeWebRequestAsync(string url)
        {
            try
            {
                //TODO: send token from the currentAPI user
                var request = (HttpWebRequest)WebRequest.Create(url);
                request.Headers.Add("Authorization", userToken);
                request.Method = "GET";
                request.ContentType = "application/x-www-form-urlencoded";
                
                using (var response = (HttpWebResponse)(await request.GetResponseAsync()))
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                        {
                            return new MakeWebRequestResDTO
                            {
                                StatusCode = 200,
                                Content = reader.ReadToEnd()
                            };
                        }
                    }
                    Console.WriteLine("Status code {0} on URL: {1}", response.StatusCode, url);

                    return new MakeWebRequestResDTO
                    {
                        StatusCode = (int)response.StatusCode
                        
                    };
                }
            }
            catch (Exception e)
            {

            }

            return new MakeWebRequestResDTO
            {
                StatusCode = 500
            };
            
        }


        public async Task BackupProjects(long? workspaceID)
        {
            var projectsUrl = "https://app.asana.com/api/1.0/projects/";
            var usersUrl = "https://app.asana.com/api/1.0/users";
            if (workspaceID.HasValue)
            {
                projectsUrl = projectsUrl.AddQueryParam("workspace", workspaceID.ToString());
                usersUrl = SimplHelpers.UrlHelper.Combine("https://app.asana.com/api/1.0/workspaces/", workspaceID.Value.ToString(), "users");
            }
            var projectsResponse = await MakeWebRequestAsync(projectsUrl);

            if (projectsResponse.StatusCode != 200)
                return;

            var projectsFilePath = Path.Combine(baseDirectory, "projects.json");

            if (File.Exists(projectsFilePath))
                File.Delete(projectsFilePath);

            File.WriteAllText(projectsFilePath, projectsResponse.Content);

            var usersResponse = await MakeWebRequestAsync(usersUrl);
            if (usersResponse.StatusCode != 200)
                return;

            //Backup users
            var usersFilePath = Path.Combine(baseDirectory, "users.json");
            if (File.Exists(usersFilePath))
                File.Delete(usersFilePath);

            File.WriteAllText(usersFilePath, usersResponse.Content);

            var projects = JsonConvert.DeserializeObject<IdNameValuesHolder>(projectsResponse.Content);

            foreach (var project in projects.Data)
{'fraction_non_alphanumeric': 0.07895449620801734, 'fraction_numerical': 0.0044691224268689055, 'mean_word_length': 1.5544794188861986, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2612863', 'n_tokens_mistral': 1787, 'n_tokens_neox': 1716, 'n_words': 355}