UnmarshalJSON is custom JSON marshalling implementation that gives us more control over the fields we want to deserialize. See MarshalJSON for a detailed explanation, why this is necessary.
(data []byte)
| 188 | // the fields we want to deserialize. See MarshalJSON for a detailed explanation, why this is |
| 189 | // necessary. |
| 190 | func (s *Session) UnmarshalJSON(data []byte) (err error) { |
| 191 | v := struct { |
| 192 | URL string `json:"url"` |
| 193 | Token *oauth2.Token `json:"token"` |
| 194 | Config *oauth2.Config `json:"oauth2"` |
| 195 | }{} |
| 196 | |
| 197 | if err = json.Unmarshal(data, &v); err != nil { |
| 198 | return |
| 199 | } |
| 200 | |
| 201 | s.URL = v.URL |
| 202 | |
| 203 | // Mark this session as dirty, if the token is not valid (anymore) |
| 204 | s.dirty = !v.Token.Valid() |
| 205 | s.Config = v.Config |
| 206 | |
| 207 | // Check, if oauth config is missing or invalid |
| 208 | if s.Config == nil { |
| 209 | return errors.New("missing oauth2 config") |
| 210 | } |
| 211 | |
| 212 | s.authorizer = api.NewOAuthAuthorizerFromConfig(s.Config, v.Token) |
| 213 | return |
| 214 | } |
| 215 | |
| 216 | // HandleResponse handles the response and error message of an gRPC call |
| 217 | func (*Session) HandleResponse(msg proto.Message, err error) error { |
no test coverage detected