Class, constructor and inherance.

holo via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Oct 11 20:26:34 PDT 2015


On Monday, 12 October 2015 at 02:30:43 UTC, Meta wrote:
> On Monday, 12 October 2015 at 02:14:35 UTC, holo wrote:
>> class credential
>> {
>> 	auto accessKey = environment.get["AWS_ACCESS_KEY"];
>> 	auto secretKey = environment.get["AWS_SECRET_KEY"];
>> }
>>
>> class sigv4 : credential
>> {
>> 	private:
>> 		const algorithm = "AWS4-HMAC-SHA256";
>>
>> 		auto currentClock = Clock.currTime(UTC());
>> 		auto currentDate = cast(Date)currentClock;
>> 		auto curDateStr = currentDate.toISOString;
>> 		auto currentTime = cast(TimeOfDay)currentClock;
>> 		auto curTimeStr = currentTime.toISOString;
>> 		auto xamztime = curDateStr ~ "T" ~ curTimeStr ~ "Z";
>> }
>
> You should set these in the constructor rather than in the 
> class body (the only one that's okay to intialize here is 
> `algorithm`, as it's a const string).

I rewrite it to something like this:

module sigawsv4

import std.stdio, std.process;
import std.digest.sha, std.digest.hmac;
import std.string;
import std.conv;
import std.datetime;
import std.net.curl;

class credential
{
	auto accessKey = environment.get["AWS_ACCESS_KEY"];
	auto secretKey = environment.get["AWS_SECRET_KEY"];
}

class sigv4 : credential
{
	this(string URI = "/", string queryStr = 
"Action=DescribeInstances&Version=2013-10-15", string headerStr = 
"host:" ~ host ~ "\n" ~ "x-amz-date:" ~ xamztime ~ "\n", string 
headers = "host;x-amz-date")
	{
		auto currentClock = Clock.currTime(UTC());
		auto currentDate = cast(Date)currentClock;
		auto curDateStr = currentDate.toISOString;
		auto currentTime = cast(TimeOfDay)currentClock;
		auto curTimeStr = currentTime.toISOString;
		auto xamztime = curDateStr ~ "T" ~ curTimeStr ~ "Z";
		
		string canonicalURI = URI;
		string canonicalQueryString = queryStr;
	       	string canonicalHeaderString = headerStr;
	       	string signedHeaders = headers;

	}

	public:
		string method;
		string service;
		string host;
		string region;
		string endpoint;
		string request_parameters;
		string payload;

	private:
		const algorithm = "AWS4-HMAC-SHA256";

		auto currentClock = Clock.currTime(UTC());
		auto currentDate = cast(Date)currentClock;
		auto curDateStr = currentDate.toISOString;
		auto currentTime = cast(TimeOfDay)currentClock;
		auto curTimeStr = currentTime.toISOString;
		auto xamztime = curDateStr ~ "T" ~ curTimeStr ~ "Z";

		auto hmac_sha256(ubyte[] key, ubyte[] msg)
		{
			auto hmac = hmac!SHA256(key);
			hmac.put(msg);
			auto digest = hmac.finish;

			return digest;
		}

		auto getSignatureKey(string key, string dateStamp, string 
regionName, string serviceName)
		{
			ubyte[] kString = cast(ubyte[])("AWS4" ~ key);
			auto kDate = sign(kString, cast(ubyte[])dateStamp);
			auto kRegion = sign(kDate, cast(ubyte[])regionName);
			auto kService = sign(kRegion,  cast(ubyte[])serviceName);
			auto kSigning = sign(kService, cast(ubyte[])"aws4_request");

			return kSigning;
		}

		auto getCanonicalRequest(string canonicalURI, string 
canonicalQueryString, string canonicalHeaderString, string 
signedHeaders)
		{
			string payloadHash = sha256Of("").toHexString.toLower;
			string canonicalRequest = method ~ "\n" ~ canonicalURI ~ "\n" 
~ canonicalQueryString ~ "\n" ~ canonicalHeadersString ~ "\n" ~ 
signedHeaders ~ "\n" ~ payloadHash;
		}
}

Is right now that constructor ok? My question was too if now i 
will inherit that  class, will that default values be in child 
class available?


More information about the Digitalmars-d-learn mailing list