Public Class InternalAPIBase
    Inherits System.Web.UI.Page

    ReadOnly Property CurrentClient As Logic.ClientAccount
        Get
            If _CurrentClient Is Nothing Then
                Try
                    _CurrentClient = New Logic.ClientAccount(SessionVars.ClientId(Session))
                Catch ex As IndexOutOfRangeException
                End Try
            End If
            Return _CurrentClient
        End Get
    End Property
    Private _CurrentClient As Logic.ClientAccount

    Sub VerifyContractAccess(contractId As Integer)
        If CurrentClient Is Nothing Then
            Respond(401, "Login required.  Try refreshing the page or logging in again.")
        ElseIf Not CurrentClient.HasContractAccess(contractId) Then
            Respond(403, "No access to contract.")
        End If
    End Sub

    Sub Respond(code As Integer, text As String)
        Response.Clear()
        Response.StatusCode = code
        Response.Write(text)
        Response.End()
    End Sub

    Sub RespondSuccess(Optional text As String = "success")
        Respond(200, text)
    End Sub

    Sub RespondError(ex As Exception)
        Dim errorId = Logic.Error.Log(ex)
        Dim message = String.Format("An error has occurred and logged for review. If you contact tech support, use this number for reference: {0}", errorId)
        Respond(500, message)
    End Sub

End Class
