public static void Error <T>(ErrorToken <T> token, string source) { lock (Guarder) { _error(token, source); } }
/// <summary> /// Connect allow you to have full access on Endpoints by get the tokens. /// </summary> /// <remarks> /// You can get the code at : redirect_uri/?code=XXXXX /// Refer to : <see href="https://api.voxity.fr/doc/#api-Token-PostDecision"/> /// </remarks> /// <param name="code"> /// This parameter set when the user allow your app tu use there personnal informations. /// It's necessary to get <see cref="AccessToken"/> and <see cref="RefreshToken"/>. /// </param> public AccessToken Connect(string code) { Dictionary <string, string> values = new Dictionary <string, string>(); values.Add("redirect_uri", UriRedirect); values.Add("client_secret", ClientSecret); values.Add("client_id", ClientId); values.Add("code", code); values.Add("grant_type", "authorization_code"); HttpResponseMessage response = null; string url = apiPrefixe + "oauth/token"; using (HttpClient client = new HttpClient()) { response = PostRequest(client, url, values, isJson: false); } if (response.IsSuccessStatusCode) { // Affect access_token, refresh_token values AccessToken respToken = JsonConvert.DeserializeObject <AccessToken>((response.Content.ReadAsStringAsync().Result)); AccessToken = respToken.access_token; RefreshToken = respToken.refresh_token; TokenExpirationDate = DateTime.Now.AddSeconds(respToken.expires_in); return(respToken); } else { switch (response.StatusCode) { case HttpStatusCode.Unauthorized: throw new ClientIdSecretException(ClientId, ClientSecret); case HttpStatusCode.Forbidden: // too many request Console.WriteLine(response.Content.ReadAsStringAsync().Result); throw new RefreshTokenException(RefreshToken); case HttpStatusCode.NotImplemented: ErrorToken errorToken = JsonConvert.DeserializeObject <ErrorToken>((response.Content.ReadAsStringAsync().Result)); throw new ApiErrorResponseException(errorToken.error, errorToken.error_description); // Really bad case... NginX answers 502 error when unsuported case is test case HttpStatusCode.BadGateway: throw new ApiSessionException("Unknown error response. Check if you pass the correct code. If persist, please contact Voxity support."); default: throw new HttpRequestException(response.StatusCode.ToString()); } } }
/// <summary/> public RefreshToken AutoRefresh() { if (RefreshToken == null) { throw new ApiSessionException("REFRESH_TOKEN not set."); } Dictionary <string, string> values = new Dictionary <string, string>(); values.Add("client_secret", ClientSecret); values.Add("client_id", ClientId); values.Add("refresh_token", RefreshToken); values.Add("grant_type", "refresh_token"); HttpResponseMessage response = null; string url = apiPrefixe + "oauth/token"; using (HttpClient client = new HttpClient()) { response = PostRequest(client, url, values, isJson: false); } if (response.IsSuccessStatusCode) { // Affect access_token, refresh_token values RefreshToken respToken = JsonConvert.DeserializeObject <RefreshToken>((response.Content.ReadAsStringAsync().Result)); AccessToken = respToken.access_token; TokenExpirationDate = DateTime.Now.AddSeconds(respToken.expires_in); return(respToken); } else { switch (response.StatusCode) { case HttpStatusCode.Unauthorized: throw new ClientIdSecretException(ClientId, ClientSecret); case HttpStatusCode.Forbidden: throw new RefreshTokenException(response.Content.ReadAsStringAsync().Result); case HttpStatusCode.NotImplemented: ErrorToken errorToken = JsonConvert.DeserializeObject <ErrorToken>((response.Content.ReadAsStringAsync().Result)); throw new ApiErrorResponseException(errorToken.error, errorToken.error_description); default: throw new HttpRequestException(response.StatusCode.ToString()); } } }
public void ErrorToken_Equality_ConsidersTheMessage_AsWellAsValue() { var token = new ErrorToken("{", 0, "message"); Assert.That(token.Equals(null), Is.False); Assert.That(token.Equals("identifier"), Is.False); Assert.That(token.Equals(new Token(Symbol.Identifier, "{", 0)), Is.False); Assert.That(token.Equals(token), Is.True); Assert.That(token.Equals(new ErrorToken("{", 0, "different message")), Is.False); Assert.That(token.Equals(new ErrorToken("/", 0, "message")), Is.False); Assert.That(token.Equals(new ErrorToken("{", 0, "message")), Is.True); }
/* code : First Time - code provide by redirect listen. * Other time, code is refresh token. * grant_type : First connection or refresh token expired : use "authorization_code" value * Other time, use "refresh_token" if you have the refresh token */ /// <summary> /// Get the first Request to ask access token and refresh token. Refer to : <see href="https://api.voxity.fr/doc/#api-Token-PostToken"/> /// </summary> /// <param name="code">Code provide by previous Request (User Decision). The code appeared in the redirect URI params : <c>redirect_uri/?code=XXXXX</c></param> /// <returns><see cref="AccessToken"/> object.</returns> public AccessToken GetAccessToken(string code) { Dictionary <string, string> values = new Dictionary <string, string>(); values.Add("redirect_uri", tokenSession.UriRedirect); values.Add("client_secret", tokenSession.ClientSecret); values.Add("client_id", tokenSession.ClientId); values.Add("code", code); values.Add("grant_type", "authorization_code"); HttpResponseMessage response = tokenSession.Request(ApiSession.HttpMethod.Post, "oauth/token", urlParams: values, isTokenRequired: false); if (response.IsSuccessStatusCode) { // Affect access_token, refresh_token values AccessToken respToken = JsonConvert.DeserializeObject <AccessToken>((response.Content.ReadAsStringAsync().Result)); tokenSession.AccessToken = respToken.access_token; tokenSession.RefreshToken = respToken.refresh_token; tokenSession.TokenExpirationDate = DateTime.Now.AddSeconds(respToken.expires_in); return(respToken); } else { switch (response.StatusCode) { case HttpStatusCode.Unauthorized: throw new ClientIdSecretException(tokenSession.ClientId, tokenSession.ClientSecret); case HttpStatusCode.Forbidden: throw new RefreshTokenException(tokenSession.RefreshToken); case HttpStatusCode.NotImplemented: ErrorToken errorToken = JsonConvert.DeserializeObject <ErrorToken>((response.Content.ReadAsStringAsync().Result)); throw new ApiErrorResponseException(errorToken.error, errorToken.error_description); // Really bad case... NginX answers 502 error when unsuported case is test case HttpStatusCode.BadGateway: throw new ApiSessionException("Unknown error response. Check if you pass the correct code. If persist, please contact Voxity support."); default: throw new HttpRequestException(response.StatusCode.ToString(response.StatusCode.ToString())); } } }
/// <summary> /// Get Request to ask new access token by refresh token. Refer to : <see href="https://api.voxity.fr/doc/#api-Token-PostToken" /> /// </summary> /// <returns><see cref="AccessToken"/> object.</returns> public RefreshToken RefreshAccessToken() { if (tokenSession.RefreshToken == null) { throw new ApiSessionException("REFRESH_TOKEN not set."); } Dictionary <string, string> values = new Dictionary <string, string>(); values.Add("client_secret", tokenSession.ClientSecret); values.Add("client_id", tokenSession.ClientId); values.Add("refresh_token", tokenSession.RefreshToken); values.Add("grant_type", "refresh_token"); HttpResponseMessage response = tokenSession.Request(ApiSession.HttpMethod.Post, "oauth/token", urlParams: values, isTokenRequired: false); if (response.IsSuccessStatusCode) { // Affect access_token, refresh_token values RefreshToken respToken = JsonConvert.DeserializeObject <RefreshToken>((response.Content.ReadAsStringAsync().Result)); tokenSession.AccessToken = respToken.access_token; tokenSession.TokenExpirationDate = DateTime.Now.AddSeconds(respToken.expires_in); return(respToken); } else { switch (response.StatusCode) { case HttpStatusCode.Unauthorized: throw new ClientIdSecretException(tokenSession.ClientId, tokenSession.ClientSecret); case HttpStatusCode.Forbidden: throw new RefreshTokenException(tokenSession.RefreshToken); case HttpStatusCode.NotImplemented: ErrorToken errorToken = JsonConvert.DeserializeObject <ErrorToken>((response.Content.ReadAsStringAsync().Result)); throw new ApiErrorResponseException(errorToken.error, errorToken.error_description); default: throw new HttpRequestException(response.StatusCode.ToString()); } } }
private static void _error <T>(ErrorToken <T> token, string source) { var col = token.ErrorResult.Remainder.Column; var lin = token.ErrorResult.Remainder.Line; var exp = token.ErrorResult.Expectations.First(); var rem = token.ErrorResult.Remainder.Current; var nestedLine = source.Split('\n')[lin - 1]; var f**k = getFromMiddle(nestedLine, col, nestedLine.Length - col, true); var startOffset = source.IndexOf(nestedLine, StringComparison.InvariantCultureIgnoreCase); var nameOffset = (startOffset + col - 1); var doc2 = new StringDocument("", source); var highlightRegion = new SourceRegion(new SourceSpan(doc2, startOffset, nestedLine.Length)); var focusRegion = new SourceRegion( new SourceSpan(doc2, nameOffset, f**k.Length)); var title = $"{token.ErrorResult.getWarningCode().To<string>().Pastel(Color.Orange)}"; var message = $"character '{exp}' expected".Pastel(Color.Orange); string Render(MarkupNode node, params NodeRenderer[] extraRenderers)