@ -22,6 +22,11 @@ const (
)
type (
Interconnect struct {
Client * IClient
DBConnector * DBConnector
}
IClient struct {
client * http . Client
tokens * map [ string ] string
@ -100,13 +105,58 @@ type (
}
)
func ( iClient * IClient ) GetTimeAllTimeEntriesForUserThroughDate ( userId string , endDate string ) * [ ] TimeEntry {
//<editor-fold name="interconnect">
/ *= == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == =
interconnect
== == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == * /
func NewInsightConnect ( ) * Interconnect {
connect := Interconnect { }
connect . Client = NewIClient ( )
connect . DBConnector = & DBConnector { }
return & connect
}
func ( ic * Interconnect ) ResetTables ( ) {
ic . DBConnector . CreateTables ( )
}
func ( ic * Interconnect ) UpdateUsers ( ) {
users := ic . Client . GetUsers ( )
ic . DBConnector . UpdateUsers ( users )
}
func ( ic * Interconnect ) UpdateTimeEntries ( ) {
users := ic . DBConnector . FetchUsers ( )
for _ , userPtr := range * users {
user := * userPtr
entries := ic . Client . GetTimeAllTimeEntriesForUserThroughDate ( user . Id , "2022-04-28" )
ic . DBConnector . UpdateTimeEntries ( entries )
}
}
//</editor-fold>
//<editor-fold name="IClient">
/ *= == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == =
IClient
== == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == * /
func NewIClient ( ) * IClient {
c := IClient {
client : & http . Client { } ,
tokens : cacheTokens ( ) ,
}
return & c
}
func ( iClient * IClient ) GetTimeAllTimeEntriesForUserThroughDate ( userId string , endDate string ) * [ ] * TimeEntry {
urlString := fmt . Sprintf ( timeEntryForUserInRangeEndpoint , endDate , userId )
log . Println ( urlString )
req := iClient . createRequest ( urlString , insightTokenFile )
rawBytes := iClient . doGet ( req )
container := make ( [ ] TimeEntry , 0 , 1000 ) //do we want to make the default result size parametric?
log . Println ( string ( * rawBytes ) )
container := make ( [ ] * TimeEntry , 0 , 1000 ) //do we want to make the default result size parametric?
err := json . Unmarshal ( * rawBytes , & container )
if err != nil {
log . Fatal ( err )
@ -152,33 +202,6 @@ func (iClient *IClient) getRawUsers() *[]User {
return & container
}
func NewIClient ( ) * IClient {
c := IClient {
client : & http . Client { } ,
tokens : cacheTokens ( ) ,
}
return & c
}
func cacheTokens ( ) * map [ string ] string {
files , err := os . ReadDir ( "./tokens/" )
ret := make ( map [ string ] string )
if err != nil {
panic ( err )
}
for _ , file := range files {
subPath := fmt . Sprintf ( "./tokens/%s" , file . Name ( ) )
fileContents , err := os . ReadFile ( subPath )
if err != nil {
log . Fatal ( err )
}
ret [ file . Name ( ) ] = string ( fileContents )
}
return & ret
}
func ( iClient * IClient ) doGet ( req * http . Request ) * [ ] byte {
resp , err := iClient . client . Do ( req )
if err != nil {
@ -205,3 +228,30 @@ func (iClient *IClient) createRequest(urlEndpoint string, tokenFile string) *htt
return request
}
//</editor-fold>
//<editor-fold name="utility functions">
/ *= == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == =
utility functions
== == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == * /
func cacheTokens ( ) * map [ string ] string {
files , err := os . ReadDir ( "./tokens/" )
ret := make ( map [ string ] string )
if err != nil {
panic ( err )
}
for _ , file := range files {
subPath := fmt . Sprintf ( "./tokens/%s" , file . Name ( ) )
fileContents , err := os . ReadFile ( subPath )
if err != nil {
log . Fatal ( err )
}
ret [ file . Name ( ) ] = string ( fileContents )
}
return & ret
}
//</editor-fold>