public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { int idImagePlace = (int)value; if (ApiClient.NONE_IMAGE != idImagePlace) { return(APIResources.buildImageURI(idImagePlace)); } else { return(""); } }
public async Task <ImageItem> PublishMediaFile(MediaFile file) { Console.WriteLine("Publishing image..."); // prepare access token HttpClient client = new HttpClient(); byte[] imageData = MediaFileToByteArray(file); SessionStorage storage = SessionStorage.GetStorage(); string accessToken = storage.Get(ApiClient.ACCESS_TOKEN) as string; HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, APIResources.buildImagePublicationURI()); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); MultipartFormDataContent requestContent = new MultipartFormDataContent(); var imageContent = new ByteArrayContent(imageData); imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg"); // Le deuxième paramètre doit absolument être "file" ici sinon ça ne fonctionnera pas requestContent.Add(imageContent, "file", "file.jpg"); request.Content = requestContent; HttpResponseMessage response = await client.SendAsync(request); string result = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { ApiClient apiClient = new ApiClient(); Response <ImageItem> imageResponse = await apiClient.ReadFromResponse <Response <ImageItem> >(response); ImageItem item = imageResponse.Data; Console.WriteLine("Image Uploded !"); return(item); } else { Console.WriteLine("Publishing image failure: Invalid status code: " + response.StatusCode); } return(new ImageItem()); }
static void ConsoleOnlyTest() { string testUser = "******"; Console.WriteLine("Creating Object!"); var serverAPIURL = "http://callumcarmicheal.com/LCAPI/"; // LINK TO WHERE YOU UPLOADED AND SETUP THE PHP SCRIPT Request apiRequest = new Request(new ServResources(serverAPIURL)); Console.WriteLine("Requesting a GUID!"); var guidReqResponse = apiRequest.getNewGUID(); // Print our information from the guidReqResponse Console.WriteLine("Recieved Object Back!"); Console.WriteLine("Guid: " + guidReqResponse.GUID); Console.WriteLine("Url: " + guidReqResponse.URL); Console.WriteLine("Err: " + guidReqResponse.Error); Console.WriteLine("ErrMSG: " + guidReqResponse.Error_Message); Console.WriteLine(""); Console.WriteLine("Requesting a Authorization GUID and URL"); var apiSetupResponse = apiRequest.SetupAPIRequest(false); // When using our own checking method, we just check to see if its "RETURN" if (apiSetupResponse.getState() != ResponseState.RETURN) { Console.WriteLine("Unexpected Response (" + apiSetupResponse.getState().ToString() + "): " + apiSetupResponse.getMessage()); Console.ReadKey(); return; } // Get our token state GUID_Status guidStatus; Console.WriteLine("Calling API To Check Token!"); int i = 0; while (true) { // Now we play the waiting game! //Console.WriteLine("State: " + state.ToString() + "\n"); // Check our GUID State guidStatus = apiRequest.getGUIDState(); if (guidStatus == GUID_Status.STATE_ID_VALID) { ConColF(ConsoleColor.Green); Console.WriteLine("Token recieved."); ConColF(ConsoleColor.Black); break; } // Wait 0.5 seconds before checking // for a token! System.Threading.Thread.Sleep(500); i++; if (i <= 5) { i = 0; ConColF(ConsoleColor.Yellow); Console.WriteLine("Still waiting for token."); ConColF(ConsoleColor.Black); } } ConColF(ConsoleColor.Cyan); Console.Write("Requesting Token: "); ConColF(ConsoleColor.Green); var apiAccess = new APIAccess(apiRequest); var apiToken = apiAccess.getBearerCode(); if (apiToken.Error) { ConColF(ConsoleColor.Red); Console.WriteLine("Failed."); Console.WriteLine("EMsg: " + apiToken.Error_Message); Console.ReadKey(); return; } Console.WriteLine("Success."); ConColF(ConsoleColor.White); Console.WriteLine("Bearer Code: " + apiToken.Token); // Call the API Console.Write("Test Calling the API: "); oAuthAuth oaCreds = new oAuthAuth(apiToken.Token); var aReq = new APIRequestHandler(); var jsonString = aReq.getAPIJson(APIResources.getUser(testUser), oaCreds, true); Console.WriteLine("JSON STRING: "); Console.WriteLine(jsonString + "\n\n"); Console.Write("Attempting to serialize json: "); { var eng = new Engine(oaCreds); var ser = new LiveCodingTV.API.Wrappers.Serializer(); User user = new User(); try { // Surround with a try and catch! user = eng.User.getUser(testUser); } catch (Exception ex) { ConColF(ConsoleColor.Red); Console.WriteLine("Error."); Console.WriteLine("EMsg: " + ex.Message); Console.ReadKey(); return; } // Set the console color and print Success ConColF(ConsoleColor.Green); Console.WriteLine("Success"); ConColF(ConsoleColor.DarkMagenta); Console.WriteLine("============================\tUser Information"); // Print some information about the user ConPrintCol("Username", user.Username); ConPrintCol("Country", user.Country); ConPrintCol("Fav Line", user.FavoriteLineOfCode); } Console.ReadKey(); }