private static async Task EnumerateObjects(string project) { var auth = new GoogleOAuth2("https://www.googleapis.com/auth/devstorage.read_write"); var token = await auth.Authenticate(""); var defaults = new DynamicRestClientDefaults() { AuthScheme = "OAuth", AuthToken = token }; dynamic google = new DynamicRestClient("https://www.googleapis.com/", defaults); dynamic bucketEndPoint = google.storage.v1.b; dynamic buckets = await bucketEndPoint.get(project: project); foreach (var bucket in buckets.items) { Console.WriteLine("bucket {0}: {1}", bucket.id, bucket.name); dynamic contents = await bucketEndPoint(bucket.id).o.get(); foreach (var item in contents.items) { Console.WriteLine("\tid: {0}", item.id); Console.WriteLine("\tname: {0}", item.name); Console.WriteLine("\tcontent type: {0}", item.contentType); Console.WriteLine("\t-----"); } } }
internal DynamicRestClient(string baseUrl, RestProxy parent, string name, DynamicRestClientDefaults defaults, Func <HttpRequestMessage, CancellationToken, Task> configure) : base(parent, name) { _baseUrl = baseUrl; _defaults = defaults ?? new DynamicRestClientDefaults(); _configureRequest = configure; }
private async Task Authorize() { dynamic keys = await GetAppCredentials("Facebook"); string clientId = keys.client_id; IDeviceOAuth2Stepwise auth = new DeviceOAuth(EndPointInfo.Facebook, "public_profile", clientId); var info = await auth.StartAuthorization(); var msg = $"Navigate to {info.VerificationUri} \nEnter this code: {info.UserCode}"; Notify.Text = msg; var token = await auth.WaitForUserConsent(info); var defaults = new DynamicRestClientDefaults() { AuthScheme = "Bearer", AuthToken = token.AccessToken }; dynamic client = new DynamicRestClient("https://graph.facebook.com/v2.3/me", defaults); dynamic v = await client.get(fields: "name"); Notify.Text = ""; UserName.Text = v.name; }
public async Task UploadPhoto() { var key = CredentialStore.RetrieveObject("flickr.key.json"); var defaults = new DynamicRestClientDefaults(); defaults.DefaultParameters.Add("format", "json"); defaults.DefaultParameters.Add("api_key", key.Key); defaults.DefaultParameters.Add("nojsoncallback", "1"); using (dynamic client = new DynamicRestClient("https://up.flickr.com/services/", MockInitialization.Handler, false, defaults)) { dynamic result = await client.upload.post(photo: File.OpenRead(@"D:\temp\test.png")); Assert.IsNotNull(result); } }
public async Task FindUserByName() { var key = CredentialStore.RetrieveObject("flickr.key.json"); var defaults = new DynamicRestClientDefaults(); defaults.DefaultParameters.Add("format", "json"); defaults.DefaultParameters.Add("api_key", key.Key); defaults.DefaultParameters.Add("nojsoncallback", "1"); using (dynamic client = new DynamicRestClient("https://api.flickr.com/services/rest/", MockInitialization.Handler, false, defaults)) { dynamic result = await client.get(method: "flickr.people.findByUsername", username: "******"); Assert.IsNotNull(result); Assert.AreEqual("9039518@N03", result.user.id); } }
public static HttpClient CreateClient(Uri baseUri, HttpMessageHandler handler, bool disposeHandler, DynamicRestClientDefaults defaults) { if (handler == null) throw new ArgumentNullException("handler"); var client = new HttpClient(handler, disposeHandler); client.BaseAddress = baseUri; #if !WINDOWS_UWP if (handler is HttpClientHandler && ((HttpClientHandler)handler).SupportsTransferEncodingChunked()) { client.DefaultRequestHeaders.TransferEncodingChunked = true; } #endif client.DefaultRequestHeaders.Accept.Clear(); if (defaults == null || !defaults.DefaultHeaders.ContainsKey("Accept")) { client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/json")); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/x-json")); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/javascript")); } if (defaults != null) { ProductInfoHeaderValue productHeader = null; if (!string.IsNullOrEmpty(defaults.UserAgent) && ProductInfoHeaderValue.TryParse(defaults.UserAgent, out productHeader)) { client.DefaultRequestHeaders.UserAgent.Clear(); client.DefaultRequestHeaders.UserAgent.Add(productHeader); } foreach (var kvp in defaults.DefaultHeaders) { client.DefaultRequestHeaders.Add(kvp.Key, kvp.Value); } if (!string.IsNullOrEmpty(defaults.AuthToken) && !string.IsNullOrEmpty(defaults.AuthScheme)) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(defaults.AuthScheme, defaults.AuthToken); } } return client; }
public async Task UploadObject() { var auth = MockInitialization.GetAuthClient("email profile https://www.googleapis.com/auth/devstorage.read_write"); _token = await auth.Authenticate(_token); Assert.IsNotNull(_token, "auth failed"); var defaults = new DynamicRestClientDefaults() { AuthScheme = "OAuth", AuthToken = _token }; using (dynamic google = new DynamicRestClient("https://www.googleapis.com/", MockInitialization.Handler, false, defaults)) using (var stream = new StreamInfo(File.OpenRead(@"D:\temp\test.png"), "image/png")) { dynamic result = await google.upload.storage.v1.b.unit_tests.o.post(stream, name: new PostUrlParam("test_object"), uploadType: new PostUrlParam("media")); Assert.IsNotNull(result); } }
[Ignore] // drive scopes don't work with device pin based oauth2 - ignore for now public async Task UploadFile() { var auth = MockInitialization.GetAuthClient("email profile https://www.googleapis.com/auth/devstorage.read_write"); _token = await auth.Authenticate(_token); Assert.IsNotNull(_token, "auth failed"); var defaults = new DynamicRestClientDefaults() { AuthScheme = "OAuth", AuthToken = _token }; using (dynamic google = new DynamicRestClient("https://www.googleapis.com/", MockInitialization.Handler, false, defaults)) { dynamic result = await google.upload.drive.v2.files.post(File.OpenRead(@"D:\temp\test.png"), uploadType: "media", title: "unit_test.jpg"); Assert.IsNotNull(result); } }
/// <summary> /// ctor /// </summary> /// <param name="baseUrl">The root url for all requests</param> /// <param name="defaults">Default values to add to all requests</param> /// <param name="configure">A callback function that will be called just before any request is sent</param> public DynamicRestClient(string baseUrl, DynamicRestClientDefaults defaults = null, Func <HttpRequestMessage, CancellationToken, Task> configure = null) : this(baseUrl, null, "", defaults, configure) { }
/// <summary> /// ctor /// </summary> /// <param name="baseUri">The root url for all requests</param> /// <param name="handler">HttpMessageHandler to use for communication</param> /// <param name="disposeHandler">Flag indicating whether to take ownership of the handler instance and dispose of when this instance is disposed</param> /// <param name="defaults">Default values to add to all requests</param> /// <param name="configure">A callback function that will be called just before any request is sent</param> public DynamicRestClient(Uri baseUri, HttpMessageHandler handler, bool disposeHandler = false, DynamicRestClientDefaults defaults = null, Func <HttpRequestMessage, CancellationToken, Task> configure = null) : this(HttpClientFactory.CreateClient(baseUri, handler, disposeHandler, defaults), null, null, "", configure, true) { _defaultParameters = defaults != null ? defaults.DefaultParameters : null; }
public async Task UploadInt() { var auth = MockInitialization.GetAuthClient("email profile https://www.googleapis.com/auth/devstorage.read_write"); _token = await auth.Authenticate(_token); Assert.IsNotNull(_token, "auth failed"); var defaults = new DynamicRestClientDefaults() { AuthScheme = "OAuth", AuthToken = _token }; using (dynamic google = new DynamicRestClient("https://www.googleapis.com/", defaults)) { dynamic result = await google.upload.storage.v1.b.unit_tests.o.post(42, name: new PostUrlParam("int_object"), uploadType: new PostUrlParam("media")); Assert.IsNotNull(result); } }
public static HttpClient CreateClient(Uri baseUri, DynamicRestClientDefaults defaults) { return(CreateClient(baseUri, new HttpClientHandler(), true, defaults));; }
public Congress(string apiKey) { var defaults = new DynamicRestClientDefaults(); defaults.DefaultHeaders.Add("X-APIKEY", apiKey); _client = new DynamicRestClient("https://congress.api.sunlightfoundation.com/", defaults); }
private void DefineRESTDefaults() { restClientDefaults = new DynamicRestClientDefaults(); restClientDefaults.DefaultHeaders.Add(PlexHeaders.X_PLEX_PLATFORM, "Windows"); restClientDefaults.DefaultHeaders.Add(PlexHeaders.X_PLEX_PLATFORM_VERSION, System.Environment.Version.ToString()); restClientDefaults.DefaultHeaders.Add(PlexHeaders.X_PLEX_PROVIDES, "player"); restClientDefaults.DefaultHeaders.Add(PlexHeaders.X_PLEX_CLIENT_IDENTIFIER, "38fc8a22-6fc5-46f2-8c6b-818e2758cfa2"); restClientDefaults.DefaultHeaders.Add(PlexHeaders.X_PLEX_PRODUCT, "Plex2Sonos"); restClientDefaults.DefaultHeaders.Add(PlexHeaders.X_PLEX_VERSION, "0.0.0.1"); restClientDefaults.DefaultHeaders.Add(PlexHeaders.X_PLEX_DEVICE, System.Environment.MachineName); restClientDefaults.DefaultHeaders.Add("Accept", "application/json"); }
/// <summary> /// Returns the user's endpoint profile using <see cref="EndPointInfo.ProfileUri"/> /// </summary> /// <param name="token">The token</param> /// <param name="cancelToken">A cancellation token</param> /// <returns>User's profile</returns> public async Task<dynamic> GetProfile(TokenInfo token, CancellationToken cancelToken) { if (token == null) throw new ArgumentNullException("token"); var defaults = new DynamicRestClientDefaults() { AuthScheme = EndPoint.Scheme, AuthToken = token.AccessToken }; using (dynamic profileEndpoint = new DynamicRestClient(_endPoint.ProfileUri, defaults)) { return await profileEndpoint.get(cancelToken); } }
public static HttpClient CreateClient(Uri baseUri, DynamicRestClientDefaults defaults) { return CreateClient(baseUri, new HttpClientHandler(), true, defaults); ; }
/// <summary> /// ctor /// </summary> /// <param name="baseUri">The root url for all requests</param> /// <param name="defaults">Default values to add to all requests</param> /// <param name="configure">A callback function that will be called just before any request is sent</param> public DynamicRestClient(string baseUri, DynamicRestClientDefaults defaults = null, Func <HttpRequestMessage, CancellationToken, Task> configure = null) : this(new Uri(baseUri, UriKind.Absolute), defaults, configure) { }
public static HttpClient CreateClient(Uri baseAddress, HttpMessageHandler handler, bool disposeHandler, DynamicRestClientDefaults defaults) { if (baseAddress == null) { throw new ArgumentNullException("baseAddress"); } if (handler == null) { throw new ArgumentNullException("handler"); } var client = new HttpClient(handler, disposeHandler); client.BaseAddress = baseAddress; client.DefaultRequestHeaders.Accept.Clear(); if (defaults == null || !defaults.DefaultHeaders.ContainsKey("Accept")) { client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/json")); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/x-json")); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/javascript")); } if (defaults != null) { ProductInfoHeaderValue productHeader = null; if (!string.IsNullOrEmpty(defaults.UserAgent) && ProductInfoHeaderValue.TryParse(defaults.UserAgent, out productHeader)) { client.DefaultRequestHeaders.UserAgent.Clear(); client.DefaultRequestHeaders.UserAgent.Add(productHeader); } foreach (var kvp in defaults.DefaultHeaders) { client.DefaultRequestHeaders.Add(kvp.Key, kvp.Value); } if (!string.IsNullOrEmpty(defaults.AuthToken) && !string.IsNullOrEmpty(defaults.AuthScheme)) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(defaults.AuthScheme, defaults.AuthToken); } } return(client); }
/// <summary> /// ctor /// </summary> /// <param name="baseUri">The root url for all requests</param> /// <param name="handler">HttpMessageHandler to use for communication</param> /// <param name="disposeHandler">Flag indicating whether to take ownership of the handler instance and dispose of when this instance is disposed</param> /// <param name="defaults">Default values to add to all requests</param> /// <param name="configure">A callback function that will be called just before any request is sent</param> public DynamicRestClient(string baseUri, HttpMessageHandler handler, bool disposeHandler = false, DynamicRestClientDefaults defaults = null, Func <HttpRequestMessage, CancellationToken, Task> configure = null) : this(new Uri(baseUri, UriKind.Absolute), handler, disposeHandler, defaults, configure) { }
public RequestBuilder(RestProxy proxy, DynamicRestClientDefaults defaults) { Debug.Assert(proxy != null); _proxy = proxy; _defaults = defaults; }
/// <summary> /// ctor /// </summary> /// <param name="baseUri">The root url for all requests</param> /// <param name="defaults">Default values to add to all requests</param> /// <param name="configureRequest">A callback function that will be called just before any request is sent</param> public DynamicRestClient(Uri baseUri, DynamicRestClientDefaults defaults = null, Func <HttpRequestMessage, CancellationToken, Task> configureRequest = null) : this(HttpClientFactory.CreateClient(baseUri, defaults), null, null, "", configureRequest, true) => _defaultParameters = defaults?.DefaultParameters;