Add getting microsoft todo data

This commit is contained in:
kolaente 2020-12-18 00:14:17 +01:00
parent 47b404ffdd
commit c1dfe3a825
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 32 additions and 4 deletions

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package microsoft_todo
package microsofttodo
import (
"bytes"
@ -73,7 +73,7 @@ type list struct {
IsOwner bool `json:"isOwner"`
IsShared bool `json:"isShared"`
WellknownListName string `json:"wellknownListName"`
tasks []*task `json:"-"` // This field does not exist in the api, we're just using it to return a structure with everything at once
Tasks []*task `json:"-"` // This field does not exist in the api, we're just using it to return a structure with everything at once
}
type listsResponse struct {
@ -121,13 +121,12 @@ func getMicrosoftGraphAuthToken(code string) (accessToken string, err error) {
return token.AccessToken, err
}
func makeAuthenticatedGetRequest(token, urlPart string, v interface{}, urlParams url.Values) error {
func makeAuthenticatedGetRequest(token, urlPart string, v interface{}) error {
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "https://graph.microsoft.com/v1.0/me/todo/"+urlPart, nil)
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+token)
req.URL.RawQuery = urlParams.Encode()
resp, err := http.DefaultClient.Do(req)
if err != nil {
@ -156,6 +155,35 @@ func makeAuthenticatedGetRequest(token, urlPart string, v interface{}, urlParams
}
func getMicrosoftTodoData(token string) (microsoftTodoData []*list, err error) {
microsoftTodoData = []*list{}
lists := &listsResponse{}
err = makeAuthenticatedGetRequest(token, "lists", lists)
if err != nil {
log.Errorf("[Microsoft Todo Migration] Could not get lists: %s", err)
return
}
log.Debugf("[Microsoft Todo Migration] Got %d lists", len(lists.Value))
for _, list := range lists.Value {
tasksResponse := &tasksResponse{}
err = makeAuthenticatedGetRequest(token, "lists/"+list.ID+"/tasks", tasksResponse)
if err != nil {
log.Errorf("[Microsoft Todo Migration] Could not get tasks for list %s: %s", list.ID, err)
return
}
log.Debugf("[Microsoft Todo Migration] Got %d tasks for list %s", len(tasksResponse.Value), list.ID)
list.Tasks = tasksResponse.Value
microsoftTodoData = append(microsoftTodoData, list)
}
log.Debugf("[Microsoft Todo Migration] Got all tasks for %d lists", len(lists.Value))
return
}