Open Sddilora opened 4 months ago
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
// DeskTimeData models the JSON structure returned from the API
type Desk
TimeData struct {
DeskTimeTime int `json:"desktimeTime"` // The amount of time in seconds the employee has spent on desktime
}
func fetchDeskTimeData(apiKey string) {
// Constructing the API URL
url := fmt.Sprintf("https://desktime.com/api/v2/json/employee?apiKey=%s", apiKey)
// Making an HTTP GET request
resp, err := http.Get(url)
if err != nil {
fmt.Println("HTTP request failed:", err)
return
}
defer resp.Body.Close()
// Reading the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Failed to read response body:", err)
return
}
// Parsing the JSON response
var data DeskTimeData
if err := json.Unmarshal(body, &data); err != nil {
fmt.Println("Failed to parse JSON response:", err)
return
}
// Calculating the DeskTime in hours and minutes
hours := data.DeskTimeTime / 3600
minutes := (data.DeskTimeTime % 3600) / 60
// Printing the DeskTime value
fmt.Printf("DeskTime for the employee: %d hours, %d minutes\n", hours, minutes)
}
func main() {
// Your API key
apiKey := "YOUR_API_KEY"
// Fetching the DeskTime data
fetchDeskTimeData(apiKey)
}
This Go code utilizes the Desktime API to access the daily work hours of a specified user, converting the total time spent in seconds to a more readable format in hours and minutes, and then printing it to the terminal. It effectively fetches and parses JSON formatted data from the API.
However, Desktime does not support retrieving data from a specific date range and can only retrieve daily information.
Could you please modify the API key retrieval to be from a JSON file so that we can iterate over our team members?
main.go file
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
)
// DeskTimeData models the JSON structure returned from the API
type DeskTimeData struct {
DeskTimeTime int `json:"desktimeTime"` // The amount of time in seconds the employee has spent on desktime
}
// User struct to model the JSON structure for user API keys
type User struct {
Name string `json:"name"`
ApiKey string `json:"apiKey"`
}
func fetchDeskTimeData(apiKey string) {
// Constructing the API URL
url := fmt.Sprintf("https://desktime.com/api/v2/json/employee?apiKey=%s", apiKey)
// Making an HTTP GET request
resp, err := http.Get(url)
if err != nil {
fmt.Println("HTTP request failed:", err)
return
}
defer resp.Body.Close()
// Reading the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Failed to read response body:", err)
return
}
// Parsing the JSON response
var data DeskTimeData
if err := json.Unmarshal(body, &data); err != nil {
fmt.Println("Failed to parse JSON response:", err)
return
}
// Calculating the DeskTime in hours and minutes
hours := data.DeskTimeTime / 3600
minutes := (data.DeskTimeTime % 3600) / 60
// Printing the DeskTime value
fmt.Printf("DeskTime for the employee: %d hours, %d minutes\n", hours, minutes)
}
func main() {
// Opening JSON file with API keys
jsonFile, err := os.Open("users.json")
if err != nil {
fmt.Println("Error opening JSON file:", err)
return
}
defer jsonFile.Close()
// Reading the JSON file
jsonData, err := ioutil.ReadAll(jsonFile)
if err != nil {
fmt.Println("Error reading JSON data:", err)
return
}
// Parsing the JSON data into User slice
var users []User
if err := json.Unmarshal(jsonData, &users); err != nil {
fmt.Println("Error parsing JSON data:", err)
return
}
// Iterating over users and fetching DeskTime data
for _, user := range users {
fmt.Printf("Fetching DeskTime data for %s\n", user.Name)
fetchDeskTimeData(user.ApiKey)
}
}
users.json file
[
{
"name": "Feyza",
"apiKey": "API_KEY_Feyza"
},
{
"name": "Esma",
"apiKey": "API_KEY_Esma"
}
]
Did you mean like this? @Sddilora
main.go file
package main import ( "encoding/json" "fmt" "io/ioutil" "net/http" "os" ) // DeskTimeData models the JSON structure returned from the API type DeskTimeData struct { DeskTimeTime int `json:"desktimeTime"` // The amount of time in seconds the employee has spent on desktime } // User struct to model the JSON structure for user API keys type User struct { Name string `json:"name"` ApiKey string `json:"apiKey"` } func fetchDeskTimeData(apiKey string) { // Constructing the API URL url := fmt.Sprintf("https://desktime.com/api/v2/json/employee?apiKey=%s", apiKey) // Making an HTTP GET request resp, err := http.Get(url) if err != nil { fmt.Println("HTTP request failed:", err) return } defer resp.Body.Close() // Reading the response body body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("Failed to read response body:", err) return } // Parsing the JSON response var data DeskTimeData if err := json.Unmarshal(body, &data); err != nil { fmt.Println("Failed to parse JSON response:", err) return } // Calculating the DeskTime in hours and minutes hours := data.DeskTimeTime / 3600 minutes := (data.DeskTimeTime % 3600) / 60 // Printing the DeskTime value fmt.Printf("DeskTime for the employee: %d hours, %d minutes\n", hours, minutes) } func main() { // Opening JSON file with API keys jsonFile, err := os.Open("users.json") if err != nil { fmt.Println("Error opening JSON file:", err) return } defer jsonFile.Close() // Reading the JSON file jsonData, err := ioutil.ReadAll(jsonFile) if err != nil { fmt.Println("Error reading JSON data:", err) return } // Parsing the JSON data into User slice var users []User if err := json.Unmarshal(jsonData, &users); err != nil { fmt.Println("Error parsing JSON data:", err) return } // Iterating over users and fetching DeskTime data for _, user := range users { fmt.Printf("Fetching DeskTime data for %s\n", user.Name) fetchDeskTimeData(user.ApiKey) } }
users.json file
[ { "name": "Feyza", "apiKey": "API_KEY_Feyza" }, { "name": "Esma", "apiKey": "API_KEY_Esma" } ]
Did you mean like this? @Sddilora
exactly
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠖⠋⠉⠛⠛⠲⢄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⠃⠀⠀⠀⠀⠀⠀⠀⠱⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⢡⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⡤⠤⠤⠤⠤⠤⣜⡀⠀⠀⠀⠀⠀⠀⠀⠀⢘⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⢀⡤⠚⠉⠀⠀⠀⠀⠀⠀⠀⠀⠉⠓⠦⣄⠀⠀⠀⠀⢠⠞⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⢀⡔⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠐⢦⡙⢦⣠⠞⠁⣀⡠⠤⠒⠒⣲⣶⣒⠒⠒⠤⢄⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⡴⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠲⣙⢖⠉⢀⣀⠤⢦⡾⠟⠋⠙⠷⠤⢄⣀⠈⠉⠒⢤⡀⠀⠀⠀⠀⠀⠀⠀ ⠀⡼⠁⠀⠀⠀⠀⠀⠀⠀⠤⣀⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠻⣆⠁⠀⠀⠟⠀⠀⠀⠀⠀⠀⠀⠈⠉⠲⢤⣶⣾⣦⣀⠀⠀⠀⠀⠀ ⣸⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠙⠲⢤⡀⠀⠀⠀⠀⠀⠀⠀⢻⡀⠀⠀⠀⣀⠤⠀⠀⠀⠀⠀⠀⠀⠀⠉⠀⠀⣿⣏⠳⡀⠀⠀⠀ ⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠓⢤⡀⠀⠀⠀⠀⠀⡇⠀⢀⡾⠋⠉⠉⠑⢄⠀⠀⠀⠀⠀⠀⠀⠀⠘⠃⠀⠙⢆⠀⠀ ⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⡈⠢⡄⠀⠀⡼⠁⠀⣸⠁⣤⣦⣤⡀⠈⡄⠀⠀⡠⠒⠉⠒⠦⡀⠀⠀⠀⠘⡆⠀ ⡇⠀⠀⠀⠀⠀⠀⠀⢤⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢆⠈⢶⠚⠥⠤⣀⡇⢰⣧⣼⣿⣧⠀⡇⠀⢰⢡⡾⣿⣦⡀⠈⢦⠀⠀⠀⠸⡄ ⠸⡄⠀⠀⠀⠀⠀⠀⠀⠀⠉⠓⠢⢄⡀⠀⠀⠀⠀⠀⠀⠀⠁⢸⡆⠀⠀⠀⠉⠚⢿⣏⣿⠇⢀⠇⠀⠸⣾⣶⣾⣿⡇⠀⢸⠀⠀⠀⠀⢣ ⠀⢳⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣍⠢⣄⠀⠀⠀⠀⠀⠀⣸⠥⠖⠲⣤⡀⠀⠀⢫⡀⣠⠞⠀⠀⠀⢹⡿⣦⠿⠁⠀⡸⠀⠀⠀⠀⠘ ⠀⠀⠳⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠳⡈⢳⡀⠀⠀⣀⠔⠁⠀⠀⠀⠱⡌⠀⠀⠀⠏⠀⠀⠀⠀⠀⠀⠙⢤⡤⠖⠉⠉⠁⠀⠀⠀⠀ ⠀⠀⠀⠈⠳⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⠀⡗⠒⠉⠁⠀⠀⠀⠀⠀⠀⣿⣶⢄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠋⠀⢀⣀⣀⠀⠀⠀⠀⢰ ⠀⠀⠀⠀⠀⠀⠙⠲⢄⣀⠀⠀⠀⠀⠀⠀⣔⡿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠋⣿⣆⠉⠓⠢⠤⣄⣀⣀⣀⣀⣀⣠⠼⠚⠁⠁⠀⠀⠀⡆ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠉⠉⠉⠉⠉⢷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣻⣿⣷⣄⡀⠀⠀⠀⢀⣼⡿⠋⠀⠀⠀⠀⠀⠀⢀⡼⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢧⠈⠻⣿⣿⣶⣾⣿⣿⠃⠀⠀⠀⠀⠀⠀⠀⡸⠁⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢣⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠳⣄⣀⣩⣿⣿⣿⠏⠀⠀⠀⠀⠀⠀⢀⡼⠁⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠛⠛⠛⠋⠁⠀⠀⠀⠀⠀⢀⡴⠋⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠑⠢⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⠔⠋⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠒⠤⢄⣀⡀⠀⠀⠀⠀⠀⢀⣀⡤⠴⠒⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
Research and implement the following tasks related to tracking, retrieving, and parsing weekly work hours from Desktime PDFs:
1. Develop an endpoint to directly read weekly work hours from Desktime PDFs. The endpoint should be able to fetch data based on parameters like date range (e.g., getWeeklyDesktime, get-15-21-AprilDesktime).
2. Investigate if Desktime provides a free API for data retrieval.
3. Research if Desktime provides data in JSON format for easier processing.
4. Implement PDF parsing functionality to extract data from Desktime PDFs, allowing for automated data retrieval from the documents.