package hipchatsnsbridge /** * Generated by API Builder - https://www.apibuilder.io * Service version: 0.0.1-dev * User agent: apibuilder app.apibuilder.io/teambytes/hipchat-sns-bridge/0.0.1-dev/go_1_5_client */ import ( "bytes" "encoding/json" "errors" "fmt" "html" "io" "io/ioutil" "net/http" "net/url" "strconv" "strings" ) const BaseUrl = "http://hipchat-sns-bridge.common.giltaws.com" const UserAgent = "apibuilder app.apibuilder.io/teambytes/hipchat-sns-bridge/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 } /** * Your add-on must declare the scopes it requires based on which API endpoints it * needs to use, via its descriptor. */ type HipchatScope string const ( HipchatScopeSendMessage HipchatScope = "send_message" HipchatScopeSendNotification = "send_notification" HipchatScopeUNDEFINED = "UNDEFINED" ) func HipchatScopeFromString(value string) HipchatScope { switch strings.TrimSpace(strings.ToLower(value)) { case "send_message": return HipchatScopeSendMessage case "send_notification": return HipchatScopeSendNotification default: return HipchatScopeUNDEFINED } } /** * A request semantic failure reason. */ type Error struct { Description string `json:"description"` } func ErrorFromMap(data interface{}) Error { b, err := json.Marshal(data) if err == nil { return ErrorFromJson(bytes.NewReader(b)) } else { panic(err) } } func ErrorFromJson(bytes io.Reader) Error { var error_ Error json.NewDecoder(bytes).Decode(&error_) return error_ } type HipchatApiConsumer struct { Scopes []HipchatScope `json:"scopes"` FromName string `json:"fromName,omitempty"` } func HipchatApiConsumerFromMap(data interface{}) HipchatApiConsumer { b, err := json.Marshal(data) if err == nil { return HipchatApiConsumerFromJson(bytes.NewReader(b)) } else { panic(err) } } func HipchatApiConsumerFromJson(bytes io.Reader) HipchatApiConsumer { var hipchatApiConsumer HipchatApiConsumer json.NewDecoder(bytes).Decode(&hipchatApiConsumer) return hipchatApiConsumer } type HipchatCapabilities struct { Configurable HipchatConfigurationPage `json:"configurable,omitempty"` HipchatApiConsumer HipchatApiConsumer `json:"hipchatApiConsumer,omitempty"` Installable HipchatInstallable `json:"installable,omitempty"` Oauth2Provider HipchatOauth2Provider `json:"oauth2Provider,omitempty"` } func HipchatCapabilitiesFromMap(data interface{}) HipchatCapabilities { b, err := json.Marshal(data) if err == nil { return HipchatCapabilitiesFromJson(bytes.NewReader(b)) } else { panic(err) } } func HipchatCapabilitiesFromJson(bytes io.Reader) HipchatCapabilities { var hipchatCapabilities HipchatCapabilities json.NewDecoder(bytes).Decode(&hipchatCapabilities) return hipchatCapabilities } type HipchatCapabilitiesDescriptor struct { Name string `json:"name"` Description string `json:"description"` Key string `json:"key"` ApiVersion string `json:"apiVersion,omitempty"` Capabilities HipchatCapabilities `json:"capabilities,omitempty"` Links HipchatLinks `json:"links"` Vendor HipchatVendor `json:"vendor,omitempty"` } func HipchatCapabilitiesDescriptorFromMap(data interface{}) HipchatCapabilitiesDescriptor { b, err := json.Marshal(data) if err == nil { return HipchatCapabilitiesDescriptorFromJson(bytes.NewReader(b)) } else { panic(err) } } func HipchatCapabilitiesDescriptorFromJson(bytes io.Reader) HipchatCapabilitiesDescriptor { var hipchatCapabilitiesDescriptor HipchatCapabilitiesDescriptor json.NewDecoder(bytes).Decode(&hipchatCapabilitiesDescriptor) return hipchatCapabilitiesDescriptor } type HipchatConfigurationPage struct { AllowAccessToRoomAdmins string `json:"allowAccessToRoomAdmins,omitempty"` Url string `json:"url"` } func HipchatConfigurationPageFromMap(data interface{}) HipchatConfigurationPage { b, err := json.Marshal(data) if err == nil { return HipchatConfigurationPageFromJson(bytes.NewReader(b)) } else { panic(err) } } func HipchatConfigurationPageFromJson(bytes io.Reader) HipchatConfigurationPage { var hipchatConfigurationPage HipchatConfigurationPage json.NewDecoder(bytes).Decode(&hipchatConfigurationPage) return hipchatConfigurationPage } type HipchatInstallData struct { CapabilitiesUrl string `json:"capabilitiesUrl"` OauthId string `json:"oauthId"` OauthSecret string `json:"oauthSecret"` GroupId int64 `json:"groupId"` RoomId string `json:"roomId,omitempty"` } func HipchatInstallDataFromMap(data interface{}) HipchatInstallData { b, err := json.Marshal(data) if err == nil { return HipchatInstallDataFromJson(bytes.NewReader(b)) } else { panic(err) } } func HipchatInstallDataFromJson(bytes io.Reader) HipchatInstallData { var hipchatInstallData HipchatInstallData json.NewDecoder(bytes).Decode(&hipchatInstallData) return hipchatInstallData } type HipchatInstallable struct { CallbackUrl string `json:"callbackUrl,omitempty"` InstalledUrl string `json:"installedUrl,omitempty"` AllowGlobal string `json:"allowGlobal,omitempty"` AllowRoom string `json:"allowRoom,omitempty"` } func HipchatInstallableFromMap(data interface{}) HipchatInstallable { b, err := json.Marshal(data) if err == nil { return HipchatInstallableFromJson(bytes.NewReader(b)) } else { panic(err) } } func HipchatInstallableFromJson(bytes io.Reader) HipchatInstallable { var hipchatInstallable HipchatInstallable json.NewDecoder(bytes).Decode(&hipchatInstallable) return hipchatInstallable } type HipchatLinks struct { Homepage string `json:"homepage,omitempty"` Api string `json:"api,omitempty"` Self string `json:"self"` } func HipchatLinksFromMap(data interface{}) HipchatLinks { b, err := json.Marshal(data) if err == nil { return HipchatLinksFromJson(bytes.NewReader(b)) } else { panic(err) } } func HipchatLinksFromJson(bytes io.Reader) HipchatLinks { var hipchatLinks HipchatLinks json.NewDecoder(bytes).Decode(&hipchatLinks) return hipchatLinks } type HipchatOauth2Provider struct { TokenUrl string `json:"tokenUrl,omitempty"` AuthorizationUrl string `json:"authorizationUrl,omitempty"` } func HipchatOauth2ProviderFromMap(data interface{}) HipchatOauth2Provider { b, err := json.Marshal(data) if err == nil { return HipchatOauth2ProviderFromJson(bytes.NewReader(b)) } else { panic(err) } } func HipchatOauth2ProviderFromJson(bytes io.Reader) HipchatOauth2Provider { var hipchatOauth2Provider HipchatOauth2Provider json.NewDecoder(bytes).Decode(&hipchatOauth2Provider) return hipchatOauth2Provider } type HipchatVendor struct { Name string `json:"name"` Url string `json:"url"` } func HipchatVendorFromMap(data interface{}) HipchatVendor { b, err := json.Marshal(data) if err == nil { return HipchatVendorFromJson(bytes.NewReader(b)) } else { panic(err) } } func HipchatVendorFromJson(bytes io.Reader) HipchatVendor { var hipchatVendor HipchatVendor json.NewDecoder(bytes).Decode(&hipchatVendor) return hipchatVendor } type ServiceInfo struct { Name string `json:"name"` Version string `json:"version"` ScalaVersion string `json:"scala_version"` SbtVersion string `json:"sbt_version"` BuildTime string `json:"build_time"` } func ServiceInfoFromMap(data interface{}) ServiceInfo { b, err := json.Marshal(data) if err == nil { return ServiceInfoFromJson(bytes.NewReader(b)) } else { panic(err) } } func ServiceInfoFromJson(bytes io.Reader) ServiceInfo { var serviceInfo ServiceInfo json.NewDecoder(bytes).Decode(&serviceInfo) return serviceInfo } type SnsNotification struct { Type string MessageId string TopicArn string Message string Timestamp string SignatureVersion string Signature string SigningCertURL string Token string `json:"omitempty"` Subject string `json:"omitempty"` SubscribeURL string `json:"omitempty"` UnsubscribeURL string `json:"omitempty"` } func SnsNotificationFromMap(data interface{}) SnsNotification { b, err := json.Marshal(data) if err == nil { return SnsNotificationFromJson(bytes.NewReader(b)) } else { panic(err) } } func SnsNotificationFromJson(bytes io.Reader) SnsNotification { var snsNotification SnsNotification json.NewDecoder(bytes).Decode(&snsNotification) return snsNotification } type HipchatCapabilitiesDescriptorsGetParams struct { RedirectToHipchatInstall string } type HipchatCapabilitiesDescriptorsGetResponse struct { StatusCode int Response *http.Response Error error HipchatCapabilitiesDescriptor HipchatCapabilitiesDescriptor } func HipchatCapabilitiesDescriptorsGet(client Client, params HipchatCapabilitiesDescriptorsGetParams) HipchatCapabilitiesDescriptorsGetResponse { requestUrl := fmt.Sprintf("%s/hipchat/capabilities", client.BaseUrl) urlValues := url.Values{} if "" == params.RedirectToHipchatInstall { urlValues.Add("redirect_to_hipchat_install", "false") } else { urlValues.Add("redirect_to_hipchat_install", params.RedirectToHipchatInstall) } encodedValues := urlValues.Encode() if encodedValues != "" { requestUrl += "?" + encodedValues } request, err := buildRequest(client, "GET", requestUrl, ClientRequestBody{}) if err != nil { return HipchatCapabilitiesDescriptorsGetResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return HipchatCapabilitiesDescriptorsGetResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 200: return HipchatCapabilitiesDescriptorsGetResponse{StatusCode: resp.StatusCode, Response: resp, HipchatCapabilitiesDescriptor: HipchatCapabilitiesDescriptorFromJson(resp.Body)} default: return HipchatCapabilitiesDescriptorsGetResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } type HipchatInstallDataGetParams struct { InstallableUrl string RedirectUrl string } type HipchatInstallDataGetResponse struct { StatusCode int Response *http.Response Error error HipchatCapabilitiesDescriptor HipchatCapabilitiesDescriptor } func HipchatInstallDataGet(client Client, params HipchatInstallDataGetParams) HipchatInstallDataGetResponse { requestUrl := fmt.Sprintf("%s/hipchat/installable", client.BaseUrl) urlValues := url.Values{} if "" != params.InstallableUrl { urlValues.Add("installable_url", params.InstallableUrl) } if "" != params.RedirectUrl { urlValues.Add("redirect_url", params.RedirectUrl) } encodedValues := urlValues.Encode() if encodedValues != "" { requestUrl += "?" + encodedValues } request, err := buildRequest(client, "GET", requestUrl, ClientRequestBody{}) if err != nil { return HipchatInstallDataGetResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return HipchatInstallDataGetResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 200: return HipchatInstallDataGetResponse{StatusCode: resp.StatusCode, Response: resp, HipchatCapabilitiesDescriptor: HipchatCapabilitiesDescriptorFromJson(resp.Body)} default: return HipchatInstallDataGetResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } type HipchatInstallDataPostResponse struct { StatusCode int Response *http.Response Error error } func HipchatInstallDataPost(client Client, hipchatInstallData HipchatInstallData) HipchatInstallDataPostResponse { requestUrl := fmt.Sprintf("%s/hipchat/installable", client.BaseUrl) bodyDocument, err := json.Marshal(hipchatInstallData) if err != nil { return HipchatInstallDataPostResponse{Error: err} } body := ClientRequestBody{contentType: "application/json", bytes: bytes.NewReader(bodyDocument)} request, err := buildRequest(client, "POST", requestUrl, body) if err != nil { return HipchatInstallDataPostResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return HipchatInstallDataPostResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 200: return HipchatInstallDataPostResponse{StatusCode: resp.StatusCode, Response: resp} default: return HipchatInstallDataPostResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } type HipchatInstallDataDeleteByOauthIdResponse struct { StatusCode int Response *http.Response Error error } func HipchatInstallDataDeleteByOauthId(client Client, oauthId string) HipchatInstallDataDeleteByOauthIdResponse { requestUrl := fmt.Sprintf("%s/hipchat/installable/%s", client.BaseUrl, html.EscapeString(oauthId)) request, err := buildRequest(client, "DELETE", requestUrl, ClientRequestBody{}) if err != nil { return HipchatInstallDataDeleteByOauthIdResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return HipchatInstallDataDeleteByOauthIdResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 200: return HipchatInstallDataDeleteByOauthIdResponse{StatusCode: resp.StatusCode, Response: resp} default: return HipchatInstallDataDeleteByOauthIdResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } type ServiceInfoGetResponse struct { StatusCode int Response *http.Response Error error ServiceInfo ServiceInfo } func ServiceInfoGet(client Client) ServiceInfoGetResponse { requestUrl := fmt.Sprintf("%s/info", client.BaseUrl) request, err := buildRequest(client, "GET", requestUrl, ClientRequestBody{}) if err != nil { return ServiceInfoGetResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return ServiceInfoGetResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 200: return ServiceInfoGetResponse{StatusCode: resp.StatusCode, Response: resp, ServiceInfo: ServiceInfoFromJson(resp.Body)} default: return ServiceInfoGetResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } type SnsNotificationsPostResponse struct { StatusCode int Response *http.Response Error error Value string } func SnsNotificationsPost(client Client, roomId string, snsNotification SnsNotification) SnsNotificationsPostResponse { requestUrl := fmt.Sprintf("%s/hipchat/room/%s/notification", client.BaseUrl, html.EscapeString(roomId)) bodyDocument, err := json.Marshal(snsNotification) if err != nil { return SnsNotificationsPostResponse{Error: err} } body := ClientRequestBody{contentType: "application/json", bytes: bytes.NewReader(bodyDocument)} request, err := buildRequest(client, "POST", requestUrl, body) if err != nil { return SnsNotificationsPostResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return SnsNotificationsPostResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 200: return SnsNotificationsPostResponse{StatusCode: resp.StatusCode, Response: resp, Value: func() string { body, _ := ioutil.ReadAll(resp.Body) return string(body) }()} default: return SnsNotificationsPostResponse{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 }