package competitionapifromswagger /** * Generated by API Builder - https://www.apibuilder.io * Service version: 0.0.1-swagger * User agent: apibuilder app.apibuilder.io/danitest/competition-api-from-swagger/0.0.1-swagger/go_1_5_client */ import ( "bytes" "encoding/json" "errors" "fmt" "html" "io" "net/http" "strconv" "strings" ) const UserAgent = "apibuilder app.apibuilder.io/danitest/competition-api-from-swagger/0.0.1-swagger/go_1_5_client" const Version = "0.0.1-swagger" 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 } type CompetitionStatus string const ( CompetitionStatusPending CompetitionStatus = "pending" CompetitionStatusActive = "active" CompetitionStatusClosed = "closed" CompetitionStatusUNDEFINED = "UNDEFINED" ) func CompetitionStatusFromString(value string) CompetitionStatus { switch strings.TrimSpace(strings.ToLower(value)) { case "pending": return CompetitionStatusPending case "active": return CompetitionStatusActive case "closed": return CompetitionStatusClosed default: return CompetitionStatusUNDEFINED } } /** * A competition is a race game for the competitors to collect more vote points. */ type Competition struct { Guid string `json:"guid"` Name string `json:"name,omitempty"` Status CompetitionStatus `json:"status"` StartAt string `json:"start_at"` EndAt string `json:"end_at"` CreatedAt string `json:"created_at"` UpdatedAt string `json:"updated_at"` } func CompetitionFromMap(data interface{}) Competition { b, err := json.Marshal(data) if err == nil { return CompetitionFromJson(bytes.NewReader(b)) } else { panic(err) } } func CompetitionFromJson(bytes io.Reader) Competition { var competition Competition json.NewDecoder(bytes).Decode(&competition) return competition } /** * The competition participant is a competitor who is playing in a specific * competition and he/she be able to win that. */ type CompetitionParticipant struct { Guid string `json:"guid"` CompetitionId string `json:"competition_id"` CompetitorId string `json:"competitor_id"` } func CompetitionParticipantFromMap(data interface{}) CompetitionParticipant { b, err := json.Marshal(data) if err == nil { return CompetitionParticipantFromJson(bytes.NewReader(b)) } else { panic(err) } } func CompetitionParticipantFromJson(bytes io.Reader) CompetitionParticipant { var competitionParticipant CompetitionParticipant json.NewDecoder(bytes).Decode(&competitionParticipant) return competitionParticipant } /** * The competitor can who can participate in a competition. */ type Competitor struct { Guid string `json:"guid"` Name string `json:"name"` } func CompetitorFromMap(data interface{}) Competitor { b, err := json.Marshal(data) if err == nil { return CompetitorFromJson(bytes.NewReader(b)) } else { panic(err) } } func CompetitorFromJson(bytes io.Reader) Competitor { var competitor Competitor json.NewDecoder(bytes).Decode(&competitor) return competitor } /** * The vote is created by voter and it concern for a competition_participant */ type Vote struct { Guid string `json:"guid"` CompetitionParticipantId string `json:"competition_participant_id"` VoterId string `json:"voter_id"` Type string `json:"type"` Points int32 `json:"points"` } func VoteFromMap(data interface{}) Vote { b, err := json.Marshal(data) if err == nil { return VoteFromJson(bytes.NewReader(b)) } else { panic(err) } } func VoteFromJson(bytes io.Reader) Vote { var vote Vote json.NewDecoder(bytes).Decode(&vote) return vote } /** * The voter can vote for competition_participant. */ type Voter struct { Guid string `json:"guid"` Name string `json:"name"` } func VoterFromMap(data interface{}) Voter { b, err := json.Marshal(data) if err == nil { return VoterFromJson(bytes.NewReader(b)) } else { panic(err) } } func VoterFromJson(bytes io.Reader) Voter { var voter Voter json.NewDecoder(bytes).Decode(&voter) return voter } type CompetitorsPostResponse struct { StatusCode int Response *http.Response Error error Competitor Competitor } func CompetitorsPost(client Client, competitor Competitor) CompetitorsPostResponse { requestUrl := fmt.Sprintf("%s/competitors", client.BaseUrl) bodyDocument, err := json.Marshal(competitor) if err != nil { return CompetitorsPostResponse{Error: err} } body := ClientRequestBody{contentType: "application/json", bytes: bytes.NewReader(bodyDocument)} request, err := buildRequest(client, "POST", requestUrl, body) if err != nil { return CompetitorsPostResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return CompetitorsPostResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 201: return CompetitorsPostResponse{StatusCode: resp.StatusCode, Response: resp, Competitor: CompetitorFromJson(resp.Body)} default: return CompetitorsPostResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } type CompetitorsGetByIdResponse struct { StatusCode int Response *http.Response Error error Competitor Competitor } func CompetitorsGetById(client Client, id string) CompetitorsGetByIdResponse { requestUrl := fmt.Sprintf("%s/competitors/%s", client.BaseUrl, html.EscapeString(id)) request, err := buildRequest(client, "GET", requestUrl, ClientRequestBody{}) if err != nil { return CompetitorsGetByIdResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return CompetitorsGetByIdResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 200: return CompetitorsGetByIdResponse{StatusCode: resp.StatusCode, Response: resp, Competitor: CompetitorFromJson(resp.Body)} default: return CompetitorsGetByIdResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } type CompetitionsPostResponse struct { StatusCode int Response *http.Response Error error Competition Competition } func CompetitionsPost(client Client, competition Competition) CompetitionsPostResponse { requestUrl := fmt.Sprintf("%s/competitions", client.BaseUrl) bodyDocument, err := json.Marshal(competition) if err != nil { return CompetitionsPostResponse{Error: err} } body := ClientRequestBody{contentType: "application/json", bytes: bytes.NewReader(bodyDocument)} request, err := buildRequest(client, "POST", requestUrl, body) if err != nil { return CompetitionsPostResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return CompetitionsPostResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 201: return CompetitionsPostResponse{StatusCode: resp.StatusCode, Response: resp, Competition: CompetitionFromJson(resp.Body)} default: return CompetitionsPostResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } type CompetitionsGetByIdResponse struct { StatusCode int Response *http.Response Error error Competition Competition } func CompetitionsGetById(client Client, id string) CompetitionsGetByIdResponse { requestUrl := fmt.Sprintf("%s/competitions/%s", client.BaseUrl, html.EscapeString(id)) request, err := buildRequest(client, "GET", requestUrl, ClientRequestBody{}) if err != nil { return CompetitionsGetByIdResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return CompetitionsGetByIdResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 200: return CompetitionsGetByIdResponse{StatusCode: resp.StatusCode, Response: resp, Competition: CompetitionFromJson(resp.Body)} default: return CompetitionsGetByIdResponse{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 }