package joglogapi /** * Generated by API Builder - https://www.apibuilder.io * Service version: 0.0.1-dev * User agent: apibuilder app.apibuilder.io/nicolai-moraru/joglog-api/0.0.1-dev/go_1_5_client */ import ( "bytes" "encoding/json" "errors" "fmt" "io" "io/ioutil" "net/http" "patch" "strconv" "strings" "time" "time.patch" "user" "user.patch" ) const BaseUrl = "http://localhost:8081" const UserAgent = "apibuilder app.apibuilder.io/nicolai-moraru/joglog-api/0.0.1-dev/go_1_5_client" const Version = "0.0.1-dev" const VersionMajor = 0 type Client struct { HttpClient *http.Client Username string Password string BaseUrl string Headers http.Header } type ClientRequestBody struct { contentType string bytes io.Reader } /** * Patch operation */ type patch.Operation string const ( patch.OperationTest patch.Operation = "test" patch.OperationReplace = "replace" patch.OperationUNDEFINED = "UNDEFINED" ) func patch.OperationFromString(value string) patch.Operation { switch strings.TrimSpace(strings.ToLower(value)) { case "test": return patch.OperationTest case "replace": return patch.OperationReplace default: return patch.OperationUNDEFINED } } /** * Integer for ID or string for any other */ type StringOrInteger string const ( StringOrIntegerString StringOrInteger = "string" StringOrIntegerInteger = "integer" StringOrIntegerUNDEFINED = "UNDEFINED" ) func StringOrIntegerFromString(value string) StringOrInteger { switch strings.TrimSpace(strings.ToLower(value)) { case "string": return StringOrIntegerString case "integer": return StringOrIntegerInteger default: return StringOrIntegerUNDEFINED } } /** * Time patch field path (id only for test) */ type time.patch.Path string const ( time.patch.PathDistance time.patch.Path = "distance" time.patch.PathTime = "time" time.patch.PathId = "id" time.patch.PathUNDEFINED = "UNDEFINED" ) func time.patch.PathFromString(value string) time.patch.Path { switch strings.TrimSpace(strings.ToLower(value)) { case "distance": return time.patch.PathDistance case "time": return time.patch.PathTime case "id": return time.patch.PathId default: return time.patch.PathUNDEFINED } } /** * User patch field path (id only for test) */ type user.patch.Path string const ( user.patch.PathUsername user.patch.Path = "username" user.patch.PathPassword = "password" user.patch.PathRole = "role" user.patch.PathId = "id" user.patch.PathUNDEFINED = "UNDEFINED" ) func user.patch.PathFromString(value string) user.patch.Path { switch strings.TrimSpace(strings.ToLower(value)) { case "username": return user.patch.PathUsername case "password": return user.patch.PathPassword case "role": return user.patch.PathRole case "id": return user.patch.PathId default: return user.patch.PathUNDEFINED } } /** * User Role */ type user.Role string const ( user.RoleUser user.Role = "user" // Regular user user.RoleManager = "manager" // User manager user.RoleAdmin = "admin" // The God himself user.RoleUNDEFINED = "UNDEFINED" ) func user.RoleFromString(value string) user.Role { switch strings.TrimSpace(strings.ToLower(value)) { case "user": return user.RoleUser case "manager": return user.RoleManager case "admin": return user.RoleAdmin default: return user.RoleUNDEFINED } } /** * Time entry */ type Time struct { Id int32 `json:"id"` Distance int32 `json:"distance"` Time int32 `json:"time"` CreatedAt string `json:"createdAt"` UpdatedAt string `json:"updatedAt"` } func TimeFromMap(data interface{}) Time { b, err := json.Marshal(data) if err == nil { return TimeFromJson(bytes.NewReader(b)) } else { panic(err) } } func TimeFromJson(bytes io.Reader) Time { var time Time json.NewDecoder(bytes).Decode(&time) return time } /** * Time entry */ type time.Minimal struct { Distance int32 `json:"distance"` Time int32 `json:"time"` } func time.MinimalFromMap(data interface{}) time.Minimal { b, err := json.Marshal(data) if err == nil { return time.MinimalFromJson(bytes.NewReader(b)) } else { panic(err) } } func time.MinimalFromJson(bytes io.Reader) time.Minimal { var timeMinimal time.Minimal json.NewDecoder(bytes).Decode(&timeMinimal) return timeMinimal } /** * Time patch entry */ type time.patch.Entry struct { Op patch.Operation `json:"op"` Path time.patch.Path `json:"path"` Value int32 `json:"value"` } func time.patch.EntryFromMap(data interface{}) time.patch.Entry { b, err := json.Marshal(data) if err == nil { return time.patch.EntryFromJson(bytes.NewReader(b)) } else { panic(err) } } func time.patch.EntryFromJson(bytes io.Reader) time.patch.Entry { var timePatchEntry time.patch.Entry json.NewDecoder(bytes).Decode(&timePatchEntry) return timePatchEntry } /** * User */ type User struct { Id int32 `json:"id"` Username string `json:"username"` Role user.Role `json:"role"` CreatedAt string `json:"createdAt"` UpdatedAt string `json:"updatedAt"` } func UserFromMap(data interface{}) User { b, err := json.Marshal(data) if err == nil { return UserFromJson(bytes.NewReader(b)) } else { panic(err) } } func UserFromJson(bytes io.Reader) User { var user User json.NewDecoder(bytes).Decode(&user) return user } /** * User */ type user.Minimal struct { Username string `json:"username"` Password string `json:"password"` } func user.MinimalFromMap(data interface{}) user.Minimal { b, err := json.Marshal(data) if err == nil { return user.MinimalFromJson(bytes.NewReader(b)) } else { panic(err) } } func user.MinimalFromJson(bytes io.Reader) user.Minimal { var userMinimal user.Minimal json.NewDecoder(bytes).Decode(&userMinimal) return userMinimal } /** * User patch entry */ type user.patch.Entry struct { Op patch.Operation `json:"op"` Path user.patch.Path `json:"path"` Value StringOrInteger `json:"value"` } func user.patch.EntryFromMap(data interface{}) user.patch.Entry { b, err := json.Marshal(data) if err == nil { return user.patch.EntryFromJson(bytes.NewReader(b)) } else { panic(err) } } func user.patch.EntryFromJson(bytes io.Reader) user.patch.Entry { var userPatchEntry user.patch.Entry json.NewDecoder(bytes).Decode(&userPatchEntry) return userPatchEntry } type TimesPostResponse struct { StatusCode int Response *http.Response Error error Time Time Value string } func TimesPost(client Client, timeMinimal time.Minimal) TimesPostResponse { requestUrl := fmt.Sprintf("%s/times", client.BaseUrl) bodyDocument, err := json.Marshal(timeMinimal) if err != nil { return TimesPostResponse{Error: err} } body := ClientRequestBody{contentType: "application/json", bytes: bytes.NewReader(bodyDocument)} request, err := buildRequest(client, "POST", requestUrl, body) if err != nil { return TimesPostResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return TimesPostResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 201: return TimesPostResponse{StatusCode: resp.StatusCode, Response: resp, Time: TimeFromJson(resp.Body)} case 400: return TimesPostResponse{StatusCode: resp.StatusCode, Response: resp, Value: func() string { body, _ := ioutil.ReadAll(resp.Body) return string(body) }()} case 403: return TimesPostResponse{StatusCode: resp.StatusCode, Response: resp, Value: func() string { body, _ := ioutil.ReadAll(resp.Body) return string(body) }()} default: return TimesPostResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } type TimesGetResponse struct { StatusCode int Response *http.Response Error error Times []Time Value string } func TimesGet(client Client) TimesGetResponse { requestUrl := fmt.Sprintf("%s/times", client.BaseUrl) request, err := buildRequest(client, "GET", requestUrl, ClientRequestBody{}) if err != nil { return TimesGetResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return TimesGetResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 200: return TimesGetResponse{StatusCode: resp.StatusCode, Response: resp, Times: func() []Time { var tmp []interface{} json.NewDecoder(resp.Body).Decode(&tmp) var all []Time for _, el := range tmp { all = append(all, TimeFromMap(el)) } return all }()} case 403: return TimesGetResponse{StatusCode: resp.StatusCode, Response: resp, Value: func() string { body, _ := ioutil.ReadAll(resp.Body) return string(body) }()} default: return TimesGetResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } type TimesGetByIdResponse struct { StatusCode int Response *http.Response Error error Time Time Value string } func TimesGetById(client Client, id int32) TimesGetByIdResponse { requestUrl := fmt.Sprintf("%s/times/%s", client.BaseUrl, strconv.FormatInt(id, 10)) request, err := buildRequest(client, "GET", requestUrl, ClientRequestBody{}) if err != nil { return TimesGetByIdResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return TimesGetByIdResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 200: return TimesGetByIdResponse{StatusCode: resp.StatusCode, Response: resp, Time: TimeFromJson(resp.Body)} case 403: return TimesGetByIdResponse{StatusCode: resp.StatusCode, Response: resp, Value: func() string { body, _ := ioutil.ReadAll(resp.Body) return string(body) }()} case 404: return TimesGetByIdResponse{StatusCode: resp.StatusCode, Response: resp, Value: func() string { body, _ := ioutil.ReadAll(resp.Body) return string(body) }()} default: return TimesGetByIdResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } type TimesPatchResponse struct { StatusCode int Response *http.Response Error error Time Time Value string } func TimesPatch(client Client, timePatchEntry []time.patch.Entry) TimesPatchResponse { requestUrl := fmt.Sprintf("%s/times", client.BaseUrl) bodyDocument, err := json.Marshal(timePatchEntry) if err != nil { return TimesPatchResponse{Error: err} } body := ClientRequestBody{contentType: "application/json", bytes: bytes.NewReader(bodyDocument)} request, err := buildRequest(client, "PATCH", requestUrl, body) if err != nil { return TimesPatchResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return TimesPatchResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 200: return TimesPatchResponse{StatusCode: resp.StatusCode, Response: resp, Time: TimeFromJson(resp.Body)} case 403: return TimesPatchResponse{StatusCode: resp.StatusCode, Response: resp, Value: func() string { body, _ := ioutil.ReadAll(resp.Body) return string(body) }()} default: return TimesPatchResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } type TimesDeleteByIdResponse struct { StatusCode int Response *http.Response Error error Time Time Value string } func TimesDeleteById(client Client, id int32) TimesDeleteByIdResponse { requestUrl := fmt.Sprintf("%s/times/%s", client.BaseUrl, strconv.FormatInt(id, 10)) request, err := buildRequest(client, "DELETE", requestUrl, ClientRequestBody{}) if err != nil { return TimesDeleteByIdResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return TimesDeleteByIdResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 200: return TimesDeleteByIdResponse{StatusCode: resp.StatusCode, Response: resp, Time: TimeFromJson(resp.Body)} case 403: return TimesDeleteByIdResponse{StatusCode: resp.StatusCode, Response: resp, Value: func() string { body, _ := ioutil.ReadAll(resp.Body) return string(body) }()} case 404: return TimesDeleteByIdResponse{StatusCode: resp.StatusCode, Response: resp, Value: func() string { body, _ := ioutil.ReadAll(resp.Body) return string(body) }()} default: return TimesDeleteByIdResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } type UsersPostAuthenticateResponse struct { StatusCode int Response *http.Response Error error Value string } func UsersPostAuthenticate(client Client, userMinimal user.Minimal) UsersPostAuthenticateResponse { requestUrl := fmt.Sprintf("%s/users/authenticate", client.BaseUrl) bodyDocument, err := json.Marshal(userMinimal) if err != nil { return UsersPostAuthenticateResponse{Error: err} } body := ClientRequestBody{contentType: "application/json", bytes: bytes.NewReader(bodyDocument)} request, err := buildRequest(client, "POST", requestUrl, body) if err != nil { return UsersPostAuthenticateResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return UsersPostAuthenticateResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 201: return UsersPostAuthenticateResponse{StatusCode: resp.StatusCode, Response: resp, Value: func() string { body, _ := ioutil.ReadAll(resp.Body) return string(body) }()} case 403: return UsersPostAuthenticateResponse{StatusCode: resp.StatusCode, Response: resp, Value: func() string { body, _ := ioutil.ReadAll(resp.Body) return string(body) }()} default: return UsersPostAuthenticateResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } type UsersPostResponse struct { StatusCode int Response *http.Response Error error User User Value string } func UsersPost(client Client, userMinimal user.Minimal) UsersPostResponse { requestUrl := fmt.Sprintf("%s/users", client.BaseUrl) bodyDocument, err := json.Marshal(userMinimal) if err != nil { return UsersPostResponse{Error: err} } body := ClientRequestBody{contentType: "application/json", bytes: bytes.NewReader(bodyDocument)} request, err := buildRequest(client, "POST", requestUrl, body) if err != nil { return UsersPostResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return UsersPostResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 201: return UsersPostResponse{StatusCode: resp.StatusCode, Response: resp, User: UserFromJson(resp.Body)} case 400: return UsersPostResponse{StatusCode: resp.StatusCode, Response: resp, Value: func() string { body, _ := ioutil.ReadAll(resp.Body) return string(body) }()} case 403: return UsersPostResponse{StatusCode: resp.StatusCode, Response: resp, Value: func() string { body, _ := ioutil.ReadAll(resp.Body) return string(body) }()} default: return UsersPostResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } type UsersGetResponse struct { StatusCode int Response *http.Response Error error Users []User Value string } func UsersGet(client Client) UsersGetResponse { requestUrl := fmt.Sprintf("%s/users", client.BaseUrl) request, err := buildRequest(client, "GET", requestUrl, ClientRequestBody{}) if err != nil { return UsersGetResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return UsersGetResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 200: return UsersGetResponse{StatusCode: resp.StatusCode, Response: resp, Users: func() []User { var tmp []interface{} json.NewDecoder(resp.Body).Decode(&tmp) var all []User for _, el := range tmp { all = append(all, UserFromMap(el)) } return all }()} case 403: return UsersGetResponse{StatusCode: resp.StatusCode, Response: resp, Value: func() string { body, _ := ioutil.ReadAll(resp.Body) return string(body) }()} default: return UsersGetResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } type UsersGetByIdResponse struct { StatusCode int Response *http.Response Error error User User Value string } func UsersGetById(client Client, id int32) UsersGetByIdResponse { requestUrl := fmt.Sprintf("%s/users/%s", client.BaseUrl, strconv.FormatInt(id, 10)) request, err := buildRequest(client, "GET", requestUrl, ClientRequestBody{}) if err != nil { return UsersGetByIdResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return UsersGetByIdResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 200: return UsersGetByIdResponse{StatusCode: resp.StatusCode, Response: resp, User: UserFromJson(resp.Body)} case 403: return UsersGetByIdResponse{StatusCode: resp.StatusCode, Response: resp, Value: func() string { body, _ := ioutil.ReadAll(resp.Body) return string(body) }()} case 404: return UsersGetByIdResponse{StatusCode: resp.StatusCode, Response: resp, Value: func() string { body, _ := ioutil.ReadAll(resp.Body) return string(body) }()} default: return UsersGetByIdResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } type UsersPatchResponse struct { StatusCode int Response *http.Response Error error User User Value string } func UsersPatch(client Client, userPatchEntry []user.patch.Entry) UsersPatchResponse { requestUrl := fmt.Sprintf("%s/users", client.BaseUrl) bodyDocument, err := json.Marshal(userPatchEntry) if err != nil { return UsersPatchResponse{Error: err} } body := ClientRequestBody{contentType: "application/json", bytes: bytes.NewReader(bodyDocument)} request, err := buildRequest(client, "PATCH", requestUrl, body) if err != nil { return UsersPatchResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return UsersPatchResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 200: return UsersPatchResponse{StatusCode: resp.StatusCode, Response: resp, User: UserFromJson(resp.Body)} case 403: return UsersPatchResponse{StatusCode: resp.StatusCode, Response: resp, Value: func() string { body, _ := ioutil.ReadAll(resp.Body) return string(body) }()} default: return UsersPatchResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } type UsersDeleteByIdResponse struct { StatusCode int Response *http.Response Error error User User Value string } func UsersDeleteById(client Client, id int32) UsersDeleteByIdResponse { requestUrl := fmt.Sprintf("%s/users/%s", client.BaseUrl, strconv.FormatInt(id, 10)) request, err := buildRequest(client, "DELETE", requestUrl, ClientRequestBody{}) if err != nil { return UsersDeleteByIdResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return UsersDeleteByIdResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 200: return UsersDeleteByIdResponse{StatusCode: resp.StatusCode, Response: resp, User: UserFromJson(resp.Body)} case 403: return UsersDeleteByIdResponse{StatusCode: resp.StatusCode, Response: resp, Value: func() string { body, _ := ioutil.ReadAll(resp.Body) return string(body) }()} case 404: return UsersDeleteByIdResponse{StatusCode: resp.StatusCode, Response: resp, Value: func() string { body, _ := ioutil.ReadAll(resp.Body) return string(body) }()} default: return UsersDeleteByIdResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } func buildRequest(client Client, method, urlStr string, body ClientRequestBody) (*http.Request, error) { request, err := http.NewRequest(method, urlStr, body.bytes) if err != nil { return nil, err } request.Header.Add("User-Agent", UserAgent) request.Header.Add("X-Apidoc-Version", Version) request.Header.Add("X-Apidoc-Version-Major", strconv.Itoa(VersionMajor)) if client.Headers != nil { for key, values := range client.Headers { for _, value := range values { request.Header.Add(key, value) } } } if body.contentType != "" { request.Header.Add("Content-Type", body.contentType) } if client.Username != "" { request.SetBasicAuth(client.Username, client.Password) } return request, nil }