-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: cleaned up endpoints and websockets
- Loading branch information
1 parent
57ef468
commit ac10cdf
Showing
11 changed files
with
197 additions
and
32 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 +1,17 @@ | ||
Future<void> main() async {} | ||
import 'package:market_client/market_client.dart'; | ||
|
||
Future<void> main() async { | ||
final httpClient = MarketHttpClient(); | ||
final marketWebsocket = MarketWebsocket.openWebsocket(httpClient.platform); | ||
|
||
final client = MarketClient(httpClient, marketWebsocket); | ||
final recentOrders = await client.orders.getMostRecentOrders(); | ||
|
||
for (final order in recentOrders.sellOrders) { | ||
// ignore: avoid_print | ||
print(order.id); | ||
} | ||
|
||
// ignore: avoid_print | ||
client.orders.newOrders().listen((event) => print(event.id)); | ||
} |
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
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
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,3 +1,4 @@ | ||
export 'auth.dart'; | ||
export 'items.dart'; | ||
export 'orders.dart'; | ||
export 'websockets.dart'; |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import 'package:market_client/market_client.dart'; | ||
import 'package:market_client/src/models/recent_orders.dart'; | ||
|
||
/// {@template orders_endpoint} | ||
/// Contains functions to retrive new and recent orders. | ||
/// {@endtemplate} | ||
class OrdersEndpoint { | ||
/// {@macro orders_endpoint} | ||
const OrdersEndpoint(MarketHttpClient client, MarketWebsocket marketWebsocket) | ||
: _client = client, | ||
_marketWebsocket = marketWebsocket; | ||
|
||
final MarketHttpClient _client; | ||
final MarketWebsocket _marketWebsocket; | ||
|
||
Map<String, dynamic> _parsePayload(Map<String, dynamic> event) { | ||
final payload = event['payload'] as Map<String, dynamic>; | ||
|
||
return payload['order'] as Map<String, dynamic>; | ||
} | ||
|
||
/// Initializes and listens for new orders. | ||
Stream<OrderFull> newOrders() { | ||
_marketWebsocket.send(MarketWebsocketTypes.subscribeToNewOrders); | ||
|
||
return _marketWebsocket.messages | ||
.where((e) => e['type'] == MarketWebsocketTypes.newOrderEvent) | ||
.map(_parsePayload) | ||
.map(OrderFull.fromJson); | ||
} | ||
|
||
/// Retrives a list of the most recent orders. | ||
/// | ||
/// Not sure up till when so expect a long list. | ||
Future<MostRecentOrders> getMostRecentOrders() async { | ||
final response = await _client.get('/most_recent'); | ||
final payload = HttpHelpers.parseResponse(response.body); | ||
|
||
return MostRecentOrders.fromJson(payload); | ||
} | ||
} |
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
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
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import 'package:market_client/market_client.dart'; | ||
|
||
/// {@template market_client} | ||
/// Base Market client that exposes other endpoints. | ||
/// {@endtemplate} | ||
class MarketClient { | ||
/// {@macro market_client} | ||
const MarketClient(MarketHttpClient client, MarketWebsocket marketWebsocket) | ||
: _client = client, | ||
_marketWebsocket = marketWebsocket; | ||
|
||
final MarketHttpClient _client; | ||
final MarketWebsocket _marketWebsocket; | ||
|
||
/// endpoints to retrieve publicly available orders and new orders. | ||
OrdersEndpoint get orders => OrdersEndpoint(_client, _marketWebsocket); | ||
|
||
/// Get orders on market items. | ||
/// | ||
/// Can also retrieve a list of items that warframe market currently allows. | ||
ItemsEndpoint get items => ItemsEndpoint(_client); | ||
|
||
/// User specfic endpoints. | ||
AuthEndpoint get auth => AuthEndpoint(_client); | ||
|
||
/// Get and updated count of online and registered users. | ||
Stream<OnlineCount> get onlineCount => _marketWebsocket.onlineCount(); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:json_annotation/json_annotation.dart'; | ||
import 'package:market_client/market_client.dart'; | ||
|
||
part 'recent_orders.g.dart'; | ||
|
||
/// {@template most_recent_orders} | ||
/// Instance containing both sell and buy orders. | ||
/// {@endtemplate} | ||
@JsonSerializable() | ||
class MostRecentOrders extends Equatable { | ||
/// {@macro most_recent_orders} | ||
const MostRecentOrders({required this.sellOrders, required this.buyOrders}); | ||
|
||
/// Creates an instance of [MostRecentOrders] from a json object. | ||
factory MostRecentOrders.fromJson(Map<String, dynamic> json) { | ||
return _$MostRecentOrdersFromJson(json); | ||
} | ||
|
||
/// A list of most recent sell orders. | ||
@JsonKey(name: 'sell_orders') | ||
final List<OrderFull> sellOrders; | ||
|
||
/// a list of most recent buy orders. | ||
@JsonKey(name: 'buy_orders') | ||
final List<OrderFull> buyOrders; | ||
|
||
/// Creates a json object from the current [MostRecentOrders]. | ||
Map<String, dynamic> toJson() => _$MostRecentOrdersToJson(this); | ||
|
||
@override | ||
List<Object?> get props => [sellOrders, buyOrders]; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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