-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
88 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,49 @@ | ||
/// A http Client extends [oauth.client] | ||
/// A http Client extends [oauth.client] | ||
library twitter.client; | ||
|
||
import 'dart:async'; | ||
|
||
import 'dart:convert'; | ||
|
||
import 'package:http/http.dart' as http; | ||
|
||
import 'package:oauth/oauth.dart' as oauth; | ||
|
||
class Client extends oauth.Client { | ||
|
||
/// Constructs a new Client with [oauth.Tokens] | ||
Client(oauth.Tokens tokens) | ||
: super(tokens); | ||
|
||
/// Send an HTTP request and Return [http.Response] with the given headers and body to the given | ||
/// URL, which can be a [Uri] or a [String] and [method]. | ||
/// | ||
Future<http.Response> request(String method, url, {Map<String,String> headers, body, Encoding encoding}) { | ||
if (url is String) url = Uri.parse(url); | ||
var request = new http.Request(method, url); | ||
|
||
if (headers != null) request.headers.addAll(headers); | ||
if (encoding != null) request.encoding = encoding; | ||
if (body != null){ | ||
if (body is String){ | ||
request.body = body; | ||
} else if (body is List){ | ||
request.bodyBytes = body; | ||
} else if (body is Map){ | ||
request.bodyFields = body; | ||
} else { | ||
throw new ArgumentError('Invalid request body "$body".'); | ||
} | ||
} | ||
return super.send(request).then(http.Response.fromStream); | ||
} | ||
} | ||
|
||
/// Constructs a new Client with [oauth.Tokens] | ||
Client(oauth.Tokens tokens) : super(tokens); | ||
|
||
/// Send an HTTP request and Return [http.Response] with the given headers and body to the given | ||
/// URL, which can be a [Uri] or a [String] and [method]. | ||
/// | ||
Future<http.Response> request(String method, url, | ||
{Map<String, String> headers, body, Encoding encoding}) { | ||
if (url is String) url = Uri.parse(url); | ||
|
||
var request = new http.Request(method, url); | ||
|
||
if (headers != null) request.headers.addAll(headers); | ||
|
||
if (encoding != null) request.encoding = encoding; | ||
|
||
if (body != null) { | ||
if (body is String) { | ||
request.body = body; | ||
} else if (body is List) { | ||
request.bodyBytes = body; | ||
} else if (body is Map) { | ||
request.bodyFields = body; | ||
} else { | ||
throw new ArgumentError('Invalid request body "$body".'); | ||
} | ||
} | ||
|
||
return super.send(request).then(http.Response.fromStream); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters