-
Notifications
You must be signed in to change notification settings - Fork 1
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
45 changed files
with
893 additions
and
451 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
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 @@ | ||
exclude_tags: manual |
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,71 @@ | ||
import 'dart:io'; | ||
|
||
import '../util/send_command.dart'; | ||
import 'logout.dart'; | ||
import 'onepub_token.dart'; | ||
import 'organisation.dart'; | ||
import 'status.dart'; | ||
|
||
class API { | ||
Future<Status> status() async { | ||
try { | ||
const endpoint = '/status'; | ||
|
||
final response = await sendCommand(command: endpoint, authorised: false); | ||
|
||
return Status(response.status, response.data['message']! as String); | ||
} on IOException { | ||
return Status(500, 'Connection failed'); | ||
} | ||
} | ||
|
||
Future<Logout> logout() async { | ||
const endpoint = '/member/logout'; | ||
final response = await sendCommand(command: endpoint); | ||
|
||
return Logout(response); | ||
} | ||
|
||
/// Fetches the [OnePubToken] for the member whos email | ||
/// address is [memberEmail]. | ||
/// The member must be logged in to the cli. | ||
Future<OnePubToken> fetchMemberToken(String memberEmail) async { | ||
final endpoint = 'member/token/$memberEmail'; | ||
final response = await sendCommand(command: endpoint); | ||
|
||
return OnePubToken(response); | ||
} | ||
|
||
/// Fetches the organisation details associated with the [onepubToken] | ||
Future<Organisation> fetchOrganisation(String onepubToken) async { | ||
// the import is an alternate (from login) form of getting | ||
// authorised but we have a chicken and egg problem | ||
// because the [sendCommand] expects the token to be | ||
// in the token store which it isn't | ||
// So we paass the auth header directly. | ||
final headers = <String, String>{}..addAll({'authorization': onepubToken}); | ||
|
||
const endpoint = '/organisation/token'; | ||
|
||
final response = await sendCommand( | ||
command: endpoint, authorised: false, headers: headers); | ||
|
||
return Organisation(response); | ||
} | ||
|
||
Future<Organisation> fetchOrganisationById(String obfuscatedId) async { | ||
final endpoint = 'organisation/$obfuscatedId'; | ||
final response = await sendCommand(command: endpoint); | ||
|
||
/// we push the id into the map so we can share a common | ||
/// constructor with [fetchOrganisation] | ||
response.data['obfuscatedId'] = obfuscatedId; | ||
return Organisation(response); | ||
} | ||
|
||
/// Creates a (empty) package owned by [team] | ||
Future<void> createPackage(String packageName, String team) async { | ||
final endpoint = 'package/create/$packageName/team/$team'; | ||
await sendCommand(command: endpoint); | ||
} | ||
} |
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,30 @@ | ||
import '../util/send_command.dart'; | ||
|
||
class Logout { | ||
Logout(EndpointResponse response) { | ||
success = response.success; | ||
|
||
if (!success) { | ||
errorMessage = response.data['message']! as String; | ||
// if we failed because we were already logged out | ||
// we still report success. | ||
if (errorMessage!.startsWith('Your token is no longer valid') || | ||
errorMessage! | ||
.startsWith('You must be logged in to run this command.')) { | ||
wasAlreadyLoggedOut = true; | ||
errorMessage = null; | ||
success = true; | ||
} | ||
} else { | ||
wasAlreadyLoggedOut = false; | ||
} | ||
} | ||
|
||
late final bool success; | ||
|
||
/// If the call failed this contains the error message. | ||
late final String? errorMessage; | ||
|
||
/// If the user was already logged out when we called logout. | ||
late final bool wasAlreadyLoggedOut; | ||
} |
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,18 @@ | ||
import '../util/send_command.dart'; | ||
|
||
class OnePubToken { | ||
OnePubToken(EndpointResponse response) { | ||
if (response.success) { | ||
token = response.data['onepubToken'] as String?; | ||
} | ||
|
||
if (token == null) { | ||
errorMessage = response.data['message']! as String; | ||
} | ||
} | ||
|
||
bool get success => token != null; | ||
String? token; | ||
|
||
String? errorMessage; | ||
} |
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,34 @@ | ||
import 'dart:io'; | ||
|
||
import '../util/send_command.dart'; | ||
|
||
class Organisation { | ||
Organisation(EndpointResponse response) { | ||
_success = response.success; | ||
|
||
if (response.status == HttpStatus.notFound) { | ||
notFound = true; | ||
} | ||
|
||
if (!response.success) { | ||
errorMessage = response.data['message']! as String; | ||
} | ||
|
||
name = response.data['organisationName']! as String; | ||
obfuscatedId = response.data['obfuscatedId']! as String; | ||
} | ||
|
||
late final bool _success; | ||
late final String name; | ||
late final String obfuscatedId; | ||
|
||
bool get success => _success; | ||
|
||
/// If success is false then you can check this field | ||
/// to see if it failed because the organisation wasn't found | ||
/// if this is false then a more serious error occured | ||
bool notFound = false; | ||
|
||
/// if [success] is false this will contain the error message. | ||
late final String? errorMessage; | ||
} |
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,6 @@ | ||
class Status { | ||
Status(this.statusCode, this.message); | ||
|
||
int statusCode; | ||
String message; | ||
} |
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
Oops, something went wrong.