These are chat archives for Ruhrpottpatriot/GW2.NET

6th
Oct 2015
Steven Liekens
@StevenLiekens
Oct 06 2015 09:23
so how do you think it'll work?
I don't like cliffhangers
Robert Logiewa
@Ruhrpottpatriot
Oct 06 2015 10:34
This message was deleted
Robert Logiewa
@Ruhrpottpatriot
Oct 06 2015 10:48
Two things:
  1. We cannot and probably should not do json serialisation in the HttpMessageHandler class.
  2. We have to overwrite the HttpContent class and with that we also need to overwrite the SerializeToStreamAsync method, so serialising in HttpMessageHandler is not going to work well
This message was deleted
but here is what we can do: We can overwrite the HttpContent class and provide a, say ReadAsDto<TDto>, method which does the serialisation for us
Robert Logiewa
@Ruhrpottpatriot
Oct 06 2015 12:25
This message was deleted
This message was deleted
This message was deleted
This message was deleted
goodnews_zpsqelj8ux7.jpg
i got it to work. I'll upload a gist to show the basics later
of course we need to optimize a lot, but that shouldn't be a big problem
Steven Liekens
@StevenLiekens
Oct 06 2015 16:27
okay cool :)
I'm taking a little break from gw2.net
need some time to think
Robert Logiewa
@Ruhrpottpatriot
Oct 06 2015 16:36
ok
Just so you know how it is done:
use the HttpMessageHandler, to process Headers. Then pass the response down to a class deriving from HttpContent. This class is then used to deserialize and validate the response into data contract objects. After that is done, we process like we do now
Steven Liekens
@StevenLiekens
Oct 06 2015 20:49
but no matter what, you have to cast to custom types in the repository class?
that's the part I'm not so sure about
Robert Logiewa
@Ruhrpottpatriot
Oct 06 2015 20:50
no
currently in a game
will explain later
Steven Liekens
@StevenLiekens
Oct 06 2015 20:50
ok
sounds like you got this
Robert Logiewa
@Ruhrpottpatriot
Oct 06 2015 20:52
the beauty of extension methods:
public async static Task<TDto> ReadAsDtoAsync<TDto>(this HttpContent httpContent)
        {
            using (Stream responseStream = await httpContent.ReadAsStreamAsync())
            {
                JsonSerializer serializer = new JsonSerializer();

                using (var streamReader = new StreamReader(responseStream))
                {
                    using (var jsonTextReader = new JsonTextReader(streamReader))
                    {
                        return serializer.Deserialize<TDto>(jsonTextReader);
                    }
                }
            }
        }
as of now we don't inherit from httpcontent
the thing is: I'm not sure what to do about the headers
Currently I leverage more work to the converters
and pass the appropriate headers via the converter state
public sealed class BuildConverter : IConverter<BuildDTO, Build>
    {
        /// <inheritdoc />
        public Build Convert(BuildDTO value, object state)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if (state == null)
            {
                throw new ArgumentNullException("state", "Precondition: state is IResponse<BuildDTO>");
            }

            return new Build
            {
                BuildId = value.BuildId,
                Timestamp = (DateTimeOffset)state
            };
        }
But I'm sure, if i have this in place, you'll be back and find ways to improve it :)
Steven Liekens
@StevenLiekens
Oct 06 2015 21:38
'state' should actually be called 'context' now that I think of it
it's only ever used to pass in a parent object
Robert Logiewa
@Ruhrpottpatriot
Oct 06 2015 21:39
Now I use it to pass the headers that need to be added to the object
since in the repository the HttpContent contains all the headers from the response
Steven Liekens
@StevenLiekens
Oct 06 2015 21:40
can't you just pass in the entire HttpContent object?
and let the converter extract what it needs from it
Robert Logiewa
@Ruhrpottpatriot
Oct 06 2015 21:40
that would be possible too
I'm still juggling with the possibilities
Steven Liekens
@StevenLiekens
Oct 06 2015 21:44
some converters will need more than one header
Robert Logiewa
@Ruhrpottpatriot
Oct 06 2015 21:45
then I pass in a Key/Value pair collection
it's possible
Steven Liekens
@StevenLiekens
Oct 06 2015 21:45
mhm