tm.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "time"
  6. )
  7. // TM stores information about TM.
  8. type TM struct {
  9. NumEntries, AccessLevel int
  10. Client, Domain, FriendlyName, Project, SourceLangCode, Subject, TMGuid, TMOwner, TargetLangCode string
  11. }
  12. func getQuery(url string) *http.Response {
  13. resp, err := http.Get(url)
  14. if err != nil {
  15. log.Printf("Error getting query: %s", err)
  16. }
  17. return resp
  18. }
  19. func (app *Application) getTMs(language string) []TM {
  20. tmURL := app.BaseURL + "tms/"
  21. queryURL := tmURL + app.AuthString
  22. if language != "" {
  23. queryURL += "&targetLang=" + language
  24. }
  25. resp := getQuery(queryURL)
  26. defer resp.Body.Close()
  27. var results []TM
  28. if resp.StatusCode == http.StatusBadRequest {
  29. time.Sleep(app.Delay)
  30. status, err := app.login()
  31. if !status || err != nil {
  32. log.Printf("Couldn't log in: %s", err)
  33. return results
  34. }
  35. return app.getTMs(language)
  36. }
  37. err := jsonDecoder(resp.Body, &results)
  38. if err != nil {
  39. log.Printf("Error decoding results: %s", err)
  40. }
  41. return results
  42. }