@require_setting('LINENOTIFY_CLIENT_ID')
@login_required
def add_linenotify_complete(request):
    if ('add_linenotify' not in request.session):
        return HttpResponseForbidden()
    (state, code) = request.session.pop('add_linenotify')
    if (request.GET.get('state') != state):
        return HttpResponseForbidden()
    project = _get_rw_project_for_user(request, code)
    if (request.GET.get('error') == 'access_denied'):
        messages.warning(request, 'LINE Notify setup was cancelled.')
        return redirect('hc-channels', project.code)
    result = requests.post('https://notify-bot.line.me/oauth/token', {'grant_type': 'authorization_code', 'code': request.GET.get('code'), 'redirect_uri': (settings.SITE_ROOT + reverse(add_linenotify_complete)), 'client_id': settings.LINENOTIFY_CLIENT_ID, 'client_secret': settings.LINENOTIFY_CLIENT_SECRET})
    doc = result.json()
    if (doc.get('status') != 200):
        messages.warning(request, 'Something went wrong.')
        return redirect('hc-channels', project.code)
    token = doc['access_token']
    result = requests.get('https://notify-api.line.me/api/status', headers={'Authorization': ('Bearer %s' % token)})
    doc = result.json()
    channel = Channel(kind='linenotify', project=project)
    channel.name = doc.get('target')
    channel.value = token
    channel.save()
    channel.assign_all_checks()
    messages.success(request, 'The LINE Notify integration has been added!')
    return redirect('hc-channels', project.code)
@require_setting('LINENOTIFY_CLIENT_ID')
@login_required
def add_linenotify_complete(request):
    if ('add_linenotify' not in request.session):
        return HttpResponseForbidden()
    (state, code) = request.session.pop('add_linenotify')
    if (request.GET.get('state') != state):
        return HttpResponseForbidden()
    project = _get_rw_project_for_user(request, code)
    if (request.GET.get('error') == 'access_denied'):
        messages.warning(request, 'LINE Notify setup was cancelled.')
        return redirect('hc-channels', project.code)
    result = requests.post('https://notify-bot.line.me/oauth/token', {'grant_type': 'authorization_code', 'code': request.GET.get('code'), 'redirect_uri': (settings.SITE_ROOT + reverse(add_linenotify_complete)), 'client_id': settings.LINENOTIFY_CLIENT_ID, 'client_secret': settings.LINENOTIFY_CLIENT_SECRET})
    doc = result.json()
    if (doc.get('status') != 200):
        messages.warning(request, 'Something went wrong.')
        return redirect('hc-channels', project.code)
    token = doc['access_token']
    result = requests.get('https://notify-api.line.me/api/status', headers={'Authorization': ('Bearer %s' % token)})
    doc = result.json()
    channel = Channel(kind='linenotify', project=project)
    channel.name = doc.get('target')
    channel.value = token
    channel.save()
    channel.assign_all_checks()
    messages.success(request, 'The LINE Notify integration has been added!')
    return redirect('hc-channels', project.code)