Skip to content

Commit 2ece81a

Browse files
Implement Cbor response and structure unmarshallers generators
1 parent b362d7c commit 2ece81a

21 files changed

+1412
-350
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
using Amazon.Runtime;
16+
using Amazon.Runtime.Internal;
17+
using Amazon.Runtime.Internal.Transform;
18+
using Amazon.Runtime.Internal.Util;
19+
using Amazon.Util;
20+
using System;
21+
using System.Collections.Generic;
22+
using System.Formats.Cbor;
23+
using System.IO;
24+
25+
namespace AWSSDK.Extensions.CborProtocol.Internal.Transform
26+
{
27+
/// <summary>
28+
/// Class for unmarshalling CBOR service responses.
29+
/// </summary>
30+
public class CborErrorResponseUnmarshaller : ICborUnmarshaller<ErrorResponse, CborUnmarshallerContext>
31+
{
32+
/// <summary>
33+
/// Build an ErrorResponse from Cbor
34+
/// </summary>
35+
/// <param name="context">The Cbor parsing context.
36+
/// Usually an <c>Amazon.Runtime.Internal.CborUnmarshallerContext</c>.</param>
37+
/// <returns>An <c>ErrorResponse</c> object.</returns>
38+
public ErrorResponse Unmarshall(CborUnmarshallerContext context)
39+
{
40+
// Placeholder until CBOR exception implementation.
41+
return null;
42+
}
43+
44+
private static CborErrorResponseUnmarshaller instance;
45+
46+
/// <summary>
47+
/// Return an instance of CborErrorResponseUnmarshaller.
48+
/// </summary>
49+
/// <returns></returns>
50+
public static CborErrorResponseUnmarshaller GetInstance()
51+
{
52+
if (instance == null)
53+
instance = new CborErrorResponseUnmarshaller();
54+
55+
return instance;
56+
}
57+
}
58+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
using Amazon.Runtime;
17+
using Amazon.Runtime.Internal;
18+
using Amazon.Runtime.Internal.Transform;
19+
using Amazon.Runtime.Internal.Util;
20+
using Amazon.Util;
21+
using System;
22+
using System.Collections.Generic;
23+
using System.Formats.Cbor;
24+
using System.IO;
25+
26+
namespace AWSSDK.Extensions.CborProtocol.Internal.Transform
27+
{
28+
public class CborMarshallerContext : MarshallerContext
29+
{
30+
public CborWriter Writer { get; private set; }
31+
32+
public CborMarshallerContext(IRequest request, CborWriter writer)
33+
: base(request)
34+
{
35+
Writer = writer;
36+
}
37+
}
38+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
using Amazon.Runtime;
17+
using Amazon.Runtime.Internal;
18+
using Amazon.Runtime.Internal.Transform;
19+
using Amazon.Runtime.Internal.Util;
20+
using Amazon.Util;
21+
using System;
22+
using System.Collections.Generic;
23+
using System.Formats.Cbor;
24+
using System.IO;
25+
using System.Net;
26+
27+
namespace AWSSDK.Extensions.CborProtocol.Internal.Transform
28+
{
29+
public abstract class CborResponseUnmarshaller : ResponseUnmarshaller
30+
{
31+
public override AmazonWebServiceResponse Unmarshall(UnmarshallerContext input)
32+
{
33+
CborUnmarshallerContext context = input as CborUnmarshallerContext;
34+
if (context == null)
35+
throw new InvalidOperationException("Unsupported UnmarshallerContext");
36+
37+
string requestId = context.ResponseData.GetHeaderValue(HeaderKeys.RequestIdHeader);
38+
try
39+
{
40+
var response = Unmarshall(context);
41+
response.ResponseMetadata = new ResponseMetadata();
42+
response.ResponseMetadata.RequestId = requestId;
43+
return response;
44+
}
45+
catch (Exception e)
46+
{
47+
throw new AmazonUnmarshallingException(requestId, context.CurrentPath, e, context.ResponseData.StatusCode);
48+
}
49+
}
50+
51+
public override AmazonServiceException UnmarshallException(UnmarshallerContext input, Exception innerException, HttpStatusCode statusCode)
52+
{
53+
CborUnmarshallerContext context = input as CborUnmarshallerContext;
54+
if (context == null)
55+
throw new InvalidOperationException("Unsupported UnmarshallerContext");
56+
var responseException = UnmarshallException(context, innerException, statusCode);
57+
responseException.RequestId = context.ResponseData.GetHeaderValue(HeaderKeys.RequestIdHeader);
58+
return responseException;
59+
}
60+
61+
public abstract AmazonWebServiceResponse Unmarshall(CborUnmarshallerContext input);
62+
63+
public abstract AmazonServiceException UnmarshallException(CborUnmarshallerContext input, Exception innerException, HttpStatusCode statusCode);
64+
65+
protected override UnmarshallerContext ConstructUnmarshallerContext(Stream responseStream, bool maintainResponseBody, IWebResponseData response, bool isException)
66+
{
67+
return new CborUnmarshallerContext(responseStream, maintainResponseBody, response, isException, null);
68+
}
69+
70+
protected override UnmarshallerContext ConstructUnmarshallerContext(Stream responseStream, bool maintainResponseBody, IWebResponseData response, bool isException, IRequestContext requestContext)
71+
{
72+
return new CborUnmarshallerContext(responseStream, maintainResponseBody, response, isException, requestContext);
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)