<commit_before>use v6;

use JSON::Tiny;
use Digest::MD5;
use LWP::Simple;
use URI::Encode;
# use HTTP::UserAgent

class X::WebService::Lastfm is Exception {
	has $.code;
	has $.text;
	method message() {
		"Last.fm API error $.code: $.text"
	}
}

class WebService::Lastfm {
	has $.api-key;
	has $.api-secret;

	has $.endpoint = 'http://ws.audioscrobbler.com/2.0/';

#	has $.ua = HTTP::UserAgent.new();

	#= Common parameters we want on all requests, factored out from request()
	# and write()
	method !expand-params($method, %args is rw) {
		%args<method> = $method;
		%args<format> = 'json';
		%args<api_key> = $.api-key;
	}

	#= Convert a hash to a query-string-like format (k=v&k2=v2)
	method !to-params(*%args) {
		return join "&", %args.map({
			join "=", uri_encode(.key), uri_encode(.value),
		});
	}

	my sub check-errors($result) {
		if $result<error> {
			X::WebService::Lastfm.new(
				code => $result<error>,
				text => $result<message>,
			).throw;
		}
	}

	method !sign-params(%args) {
		my $raw = '';
		for %args.keys.sort -> $k {
			next if $k eq 'format';
			$raw ~= "$k%args{$k}";
		}
		$raw ~= $.api-secret;<commit_msg>Fix compilation of Lastfm.p6<commit_after>use v6;

use JSON::Tiny;
use Digest::MD5;
use LWP::Simple;
use URI::Encode;
# use HTTP::UserAgent

class X::WebService::Lastfm is Exception {
	has $.code;
	has $.text;
	method message() {
		"Last.fm API error $.code: $.text"
	}
}

class WebService::Lastfm {
	has $.api-key;
	has $.api-secret;

	has $.endpoint = 'http://ws.audioscrobbler.com/2.0/';

#	has $.ua = HTTP::UserAgent.new();

	#= Common parameters we want on all requests, factored out from request()
	# and write()
	method !expand-params($method, %args) {
		%args<method> = $method;
		%args<format> = 'json';
		%args<api_key> = $.api-key;
	}

	#= Convert a hash to a query-string-like format (k=v&k2=v2)
	method !to-params(*%args) {
		return join "&", %args.map({
			join "=", uri_encode(.key), uri_encode(.value),
		});
	}

	my sub check-errors($result) {
		if $result<error> {
			X::WebService::Lastfm.new(
				code => $result<error>,
				text => $result<message>,
			).throw;
		}
	}

	method !sign-params(%args) {
		my $raw = '';
		for %args.keys.sort -> $k {
			next if $k eq 'format';
			$raw ~= "$k%args{$k}";
		}
		$raw ~= $.api-secret;
{'fraction_non_alphanumeric': 0.20087527352297593, 'fraction_numerical': 0.0056892778993435445, 'mean_word_length': 3.5267326732673268, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2941125', 'n_tokens_mistral': 965, 'n_tokens_neox': 897, 'n_words': 212}