AnyEvent-Mattermost

 view release on metacpan or  search on metacpan

lib/AnyEvent/Mattermost.pm  view on Meta::CPAN

        # Ensure that we got the channel we were looking for.
        croak "channel $channel_name was not found"
            unless exists $self->{'channels'}{$channel_name};
    }

    return $self->{'channels'}{$channel_name};
}

sub _get {
    my ($self, $path) = @_;

    my $furl = $self->{'furl'};
    my $res = $furl->get($self->{'host'} . $path, $self->_headers);

    my $data = try {
        decode_json($res->content);
    } catch {
        my $status = $res->status;
        my $message = $res->content;
        croak "unable to call $path: $status $message";
    };

    return $data;
}

sub _post {
    my ($self, $path, $postdata) = @_;

    my $furl = $self->{'furl'};

    my $res = try {
        $furl->post($self->{'host'} . $path, $self->_headers, encode_json($postdata));
    } catch {
        croak "unable to post to mattermost api: $_";
    };

    # Check for session token and update if it was present in response.
    if (my $token = $res->header('Token')) {
        $self->{'token'} = $token;
    }

    my $data = try {
        decode_json($res->content);
    } catch {
        my $status = $res->status;
        my $message = $res->content;
        croak "unable to call $path: $status $message";
    };

    return $data;
}

sub _headers {
    my ($self) = @_;

    my $headers = [
        'Content-Type'      => 'application/json',
        'X-Requested-With'  => 'XMLHttpRequest',
    ];

    # initial_load is fine with just the Cookie, other endpoints like channels/
    # require Authorization. We'll just always include both to be sure.
    if (exists $self->{'token'}) {
        push(@{$headers},
            'Cookie'        => 'MMAUTHTOKEN=' . $self->{'token'},
            'Authorization' => 'Bearer ' . $self->{'token'},
        );
    }

    return $headers;
}

1;



( run in 1.570 second using v1.01-cache-2.11-cpan-39bf76dae61 )